From 4a4841d0db0304e736462b4848681da929979632 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Fri, 20 May 2022 15:29:24 +0100 Subject: [PATCH] Suggestion autocomplete function now returns full suggestion, Textual calculates suffix --- sandbox/file_search.py | 7 ++++--- sandbox/input.py | 2 +- src/textual/widgets/text_input.py | 22 ++++++++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/sandbox/file_search.py b/sandbox/file_search.py index 40fe8047c..62b4fa85d 100644 --- a/sandbox/file_search.py +++ b/sandbox/file_search.py @@ -42,13 +42,14 @@ class FileTable(Widget): grid = Table.grid() grid.add_column() for file in self.filtered_files: - file_text = Text(" " + file.name) - file_text.highlight_regex(self.filter, "black on yellow") + file_text = Text(f" {file.name}") + if self.filter: + file_text.highlight_regex(self.filter, "black on yellow") grid.add_row(file_text) return grid -class FileSearchApp(App[str]): +class FileSearchApp(App): dark = True def on_mount(self) -> None: diff --git a/sandbox/input.py b/sandbox/input.py index 1ca8ff179..aa516812c 100644 --- a/sandbox/input.py +++ b/sandbox/input.py @@ -24,7 +24,7 @@ def word_autocompleter(value: str) -> str | None: # word completions for word in words: if word.startswith(value): - return word[len(value) :] + return word return None diff --git a/src/textual/widgets/text_input.py b/src/textual/widgets/text_input.py index da10fd5bc..bedf5cf2f 100644 --- a/src/textual/widgets/text_input.py +++ b/src/textual/widgets/text_input.py @@ -137,7 +137,7 @@ class TextInput(TextWidgetBase, can_focus=True): self._editor = TextEditorBackend(initial, 0) self.visible_range: tuple[int, int] | None = None self.autocompleter = autocompleter - self._suggestion = "" + self._suggestion_suffix = "" self._cursor_blink_visible = True self._cursor_blink_timer: Timer | None = None @@ -226,8 +226,8 @@ class TextInput(TextWidgetBase, can_focus=True): visible_text = self._editor.get_range(start, end) display_text = Text(visible_text, no_wrap=True, overflow="ignore") - if self._suggestion: - display_text.append(self._suggestion, "dim") + if self._suggestion_suffix: + display_text.append(self._suggestion_suffix, "dim") if show_cursor: display_text = self._apply_cursor_to_text( @@ -310,9 +310,9 @@ class TextInput(TextWidgetBase, can_focus=True): # If the user has hit the scroll limit self.app.bell() - if self._suggestion and self._editor.cursor_at_end: - self._editor.insert_at_cursor(self._suggestion) - self._suggestion = "" + if self._suggestion_suffix and self._editor.cursor_at_end: + self._editor.insert_at_cursor(self._suggestion_suffix) + self._suggestion_suffix = "" self._reset_visible_range() elif key == "left": if cursor_index == start: @@ -363,9 +363,15 @@ class TextInput(TextWidgetBase, can_focus=True): event.prevent_default() super().on_key(event) if self.value: - self._suggestion = self.autocompleter(self.value) + start, end = self.visible_range + full_suggestion = self.autocompleter(self.value) + if full_suggestion: + suffix = full_suggestion[len(self._editor.get_range(start, end)) :] + self._suggestion_suffix = suffix + else: + self._suggestion_suffix = None else: - self._suggestion = None + self._suggestion_suffix = None def _toggle_cursor_visible(self): """Manages the blinking of the cursor - ensuring blinking only starts when the