diff --git a/components/autocomplete/autocomplete.py b/components/autocomplete/autocomplete.py index f16e1c6..ac5df90 100644 --- a/components/autocomplete/autocomplete.py +++ b/components/autocomplete/autocomplete.py @@ -5,7 +5,7 @@ from .item import AutoCompleteItem class AutoComplete(tk.Toplevel): - def __init__(self, master, geometry=None, items=None, state=False, *args, **kwargs): + def __init__(self, master, items=None, state=False, *args, **kwargs): super().__init__(master, *args, **kwargs) self.master = master self.base = master.base @@ -75,11 +75,14 @@ def add_all_items(self): self.active_items = self.menu_items self.refresh_selected() - def update_all_words(self): for word in self.master.get_all_words(): if word not in self.get_items_text(): - self.add_item(word) + self.add_item(word, "word") + + for word in self.get_items(): + if word.get_text() not in self.master.get_all_words() and word.get_kind() == "word": + self.remove_item(word) def configure_bindings(self): root = self.base.root @@ -95,6 +98,12 @@ def add_item(self, left, kind=None): self.menu_items.append(new_item) self.row += 1 + + def remove_item(self, item): + a = self.menu_items + item.grid_forget() + self.menu_items.remove(item) + self.row -= 1 def select(self, delta): self.selected += delta @@ -154,10 +163,6 @@ def hide(self, *args): self.master.completion_active = False self.reset() - def refresh_geometry(self, *args): - self.update_idletasks() - self.geometry("+{}+{}".format(*self.master.cursor_screen_location())) - def reset(self): self.reset_selection() diff --git a/components/text.py b/components/text.py index b0548ab..643934f 100644 --- a/components/text.py +++ b/components/text.py @@ -98,22 +98,21 @@ def show_autocomplete(self, event): return if self.textw.current_word.strip() not in ["{", "}", ":", "", None, "\""]: - print("ac state: ", self.completion_active) if not self.completion_active: if event.keysym in ["Left", "Right"]: return - print("showing autocomplete") pos = self.cursor_screen_location() self.auto_completion.show(pos) self.auto_completion.update_completions() else: - print("hiding autocomplete") self.auto_completion.update_completions() else: if self.completion_active: - print("hiding autocomplete") self.hide_autocomplete() + def update_completion_words(self): + self.auto_completion.update_all_words() + def update_completions(self): self.auto_completion.update_completions() @@ -140,7 +139,6 @@ def update_current_indent(self): line = self.textw.get("insert linestart", "insert lineend") match = re.match(r'^(\s+)', line) self.current_indent = len(match.group(0)) if match else 0 - print("indentation level updated to ", len(match.group(0)) if match else 0) def update_current_line(self): self.current_line = self.textw.get("insert linestart", "insert lineend") diff --git a/components/textw.py b/components/textw.py index 19c27c2..975da9b 100644 --- a/components/textw.py +++ b/components/textw.py @@ -41,10 +41,8 @@ def get_all_words(self, *args): return self.words def update_words(self): - for i in re.findall(r"\w+", self.get_all_text_ac()): - if i not in self.words: - self.words.append(i) - self.master.update_completions() + self.words = re.findall(r"\w+", self.get_all_text_ac()) + self.master.update_completion_words() def highlight_current_word(self): self.tag_remove("highlight", 1.0, tk.END) @@ -55,13 +53,9 @@ def highlight_current_word(self): self.master.highlighter.highlight_pattern(f"\\y{word[0]}\\y", "highlight", regexp=True) def on_change(self, *args): - if self.get("insert-1c wordstart").startswith("\n"): - self.current_word = self.get("insert-1c wordstart+1c", "insert") - else: - self.current_word = self.get("insert-1c wordstart", "insert") + self.current_word = self.get("insert-1c wordstart", "insert") self.update_words() self.highlight_current_word() - # print(f"CursorIsOn<{self.current_word.strip()}>") def config_appearance(self): self.config( @@ -75,7 +69,7 @@ def config_tags(self): def config_bindings(self): self.bind("", self.master.select_all) - self.bind("", self.master.multi_selection) + self.bind("", self.multi_selection) def _proxy(self, *args): cmd = (self._orig,) + args