Skip to content

Commit

Permalink
fix: Selection methods in text editor
Browse files Browse the repository at this point in the history
- ctrl+shift horizontal for selecting words
- selection tag will have priority over current word, current line, hover tags
  • Loading branch information
tomlin7 committed Feb 15, 2024
1 parent 13e7dd8 commit b61b569
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions biscuit/core/components/editors/texteditor/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def config_tags(self):
self.tag_config("currentline", background=self.base.theme.editors.currentline)
self.tag_config("hover", background=self.base.theme.editors.hovertag)

self.tag_raise(tk.SEL, "hover")
self.tag_raise(tk.SEL, "currentline")
self.tag_raise(tk.SEL, "currentword")
self.tag_raise("currentword", "currentline")

self.tag_config("activebracket", background=self.base.theme.editors.activebracket)
self.tag_config("red", foreground="red")
for i in self.base.theme.editors.bracket_colors:
Expand All @@ -91,6 +96,8 @@ def config_bindings(self):
self.bind("<Control-g>", lambda _: self.base.palette.show_prompt(':'))
self.bind("<Control-Left>", lambda _: self.handle_ctrl_hmovement())
self.bind("<Control-Right>", lambda _: self.handle_ctrl_hmovement(True))
self.bind("<Control-Shift-Left>", lambda _: self.handle_ctrl_shift_hmovement())
self.bind("<Control-Shift-Right>", lambda _: self.handle_ctrl_shift_hmovement(True))

self.bind("<Shift-Alt-Up>", self.event_copy_line_up)
self.bind("<Shift-Alt-Down>", self.event_copy_line_down)
Expand Down Expand Up @@ -132,6 +139,7 @@ def config_bindings(self):
self.bind("<Unmap>", self.event_unmapped)
self.bind("<Destroy>", self.event_destroy)
self.bind("<Motion>", self.request_hover)
self.bind("<<Selection>>", self.on_selection)
self.bind("<Control-KeyPress>", lambda _: self.set_ctrl_key(True))
self.bind("<Control-KeyRelease>", lambda _: self.set_ctrl_key(False))
self.bind("<Control-Button-1>", self.request_goto_definition)
Expand Down Expand Up @@ -416,7 +424,7 @@ def request_goto_definition(self, e: tk.Event):
self.base.language_server_manager.request_goto_definition(self)

def request_hover(self, _):
if not self.lsp:
if not self.lsp or self.tag_ranges(tk.SEL):
return

index = self.index(tk.CURRENT)
Expand Down Expand Up @@ -519,12 +527,6 @@ def get_current_fullword(self) -> str | None:
word = self.get(start, end).strip()
return word if self.is_identifier(word) else None

def move_to_next_word(self):
self.mark_set(tk.INSERT, self.index("insert+1c wordend"))

def move_to_previous_word(self):
self.mark_set(tk.INSERT, self.index("insert-1c wordstart"))

def handle_ctrl_hmovement(self, delta=False):
if delta:
self.move_to_next_word()
Expand All @@ -533,6 +535,31 @@ def handle_ctrl_hmovement(self, delta=False):

return "break"

def handle_ctrl_shift_hmovement(self, delta=False):
if delta:
self.select_to_next_word()
else:
self.select_to_previous_word()

return "break"

def select_to_next_word(self):
self.tag_add(tk.SEL, "insert wordstart", "insert+1c wordend")
self.mark_set(tk.INSERT, "insert+1c wordend")

def select_to_previous_word(self):
self.tag_add(tk.SEL, "insert-1c wordstart", "insert wordend")
self.mark_set(tk.INSERT, "insert-1c wordstart")

def move_to_next_word(self):
self.mark_set(tk.INSERT, self.index("insert+1c wordend"))

def move_to_previous_word(self):
self.mark_set(tk.INSERT, self.index("insert-1c wordstart"))

def on_selection(self, *args):
self.tag_remove("hover", 1.0, tk.END)

def update_current_indent(self):
line = self.get("insert linestart", "insert lineend")
match = re.match(r'^(\s+)', line)
Expand Down Expand Up @@ -922,7 +949,8 @@ def highlight_current_word(self):

if word := re.findall(r"\w+", self.get("insert wordstart", "insert wordend")):
# TODO: do not highlight keywords, parts of strings, etc.
self.highlight_pattern(f"\\y{word[0]}\\y", "currentword", regexp=True)
self.highlight_pattern(f"\\y{word[0]}\\y", "currentword", regexp=True)
# self.highlight_pattern(f"\\y{word[0]}\\y", "currentword", start="insert wordend", regexp=True)

def highlight_pattern(self, pattern, tag, start="1.0", end=tk.END, regexp=False):
start = self.index(start)
Expand Down

0 comments on commit b61b569

Please sign in to comment.