Skip to content

Commit

Permalink
optimizations for autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Mar 29, 2022
1 parent 27b70fe commit 0c0d03f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
19 changes: 12 additions & 7 deletions components/autocomplete/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 3 additions & 5 deletions components/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")
Expand Down
14 changes: 4 additions & 10 deletions components/textw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -75,7 +69,7 @@ def config_tags(self):

def config_bindings(self):
self.bind("<Control-a>", self.master.select_all)
self.bind("<Control-d>", self.master.multi_selection)
self.bind("<Control-d>", self.multi_selection)

def _proxy(self, *args):
cmd = (self._orig,) + args
Expand Down

0 comments on commit 0c0d03f

Please sign in to comment.