How to get the current line text in TextArea (without knowing the line number)? #4936
-
Hello. I'm trying to implement smart indent when the Enter key is pressed. Currently, I'm able to mimic the current indentation level using this code: class ExtendedTextArea(TextArea):
def _on_key(self, event):
if event.key == 'enter':
n = self.get_cursor_line_start_location(smart_home=True)[1]
self.insert(f'\n{" " * n}')
event.prevent_default() The above code gets the location of the first non-whitespace character and inserts that many spaces on the new line. I couldn't figure out how to get the current line's content. That'd help me to add another level of indentation if the current line is a control structure. PS: If this kinda smart indent is already available in TextArea, let me know 😅 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Use You can use |
Beta Was this translation helpful? Give feedback.
Use
cursor_line_index, cursor_column_index = self.cursor_location
to get the cursor location indices.You can use
self.document.get_line(line_index)
to get the line content as a string.