Skip to content

Commit

Permalink
Merge pull request #233 from DasAnurag31/main
Browse files Browse the repository at this point in the history
feat: Bracket pair colorization
  • Loading branch information
tomlin7 authored Feb 13, 2024
2 parents bec3579 + 4c7015c commit c0e0adf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
50 changes: 47 additions & 3 deletions biscuit/core/components/editors/texteditor/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from ...utils import textutils
from .highlighter import Highlighter

BRACKET_MAP = {"(": ")", "{": "}", "[": "]"}
OPENING_BRACKETS = ("(", "{", "[")
CLOSING_BRACKETS = (")", "}", "]")

class Text(BaseText):
"""Improved Text widget"""
Expand Down Expand Up @@ -75,6 +78,10 @@ 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_config("red", foreground="red")
for i in self.base.theme.editors.bracket_colors:
self.tag_config(i, foreground=f"#{i}")

def config_bindings(self):
self.bind("<KeyRelease>", self.key_release_events)

Expand All @@ -95,9 +102,14 @@ def config_bindings(self):
self.bind_all('<<Modified>>', self._been_modified)

# pair completion
self.bind("<braceleft>", self.complete_pair)
self.bind("<bracketleft>", self.complete_pair)
self.bind("<parenleft>", self.complete_pair)
self.bind("<parenleft>", self.open_bracket)
self.bind("<braceleft>", self.open_bracket)
self.bind("<bracketleft>", self.open_bracket)

self.bind("<parenright>", self.close_bracket)
self.bind("<braceright>", self.close_bracket)
self.bind("<bracketright>", self.close_bracket)

self.bind("<apostrophe>", self.complete_pair)
self.bind("<quotedbl>", self.complete_pair)
self.bind("<BackSpace>", self.remove_pair)
Expand Down Expand Up @@ -149,6 +161,38 @@ def key_release_events(self, event: tk.Event):

self.update_words_list()

def open_bracket(self, e: tk.Event):
text = self.get("1.0", "insert")
i = 0
for ch in text:
if ch in OPENING_BRACKETS:
i += 1
elif ch in CLOSING_BRACKETS:
if i > 0:
i -= 1
self.insert(tk.INSERT, e.char, self.base.theme.editors.bracket_colors[(i%3)])

return "break"

def close_bracket(self, e: tk.Event):
text = self.get("1.0", "insert")
i = -1
stack = []
for ch in text:
if ch in OPENING_BRACKETS:
i += 1
stack.append(BRACKET_MAP[ch])
elif ch in CLOSING_BRACKETS:
if i > -1:
i -= 1
if stack[-1] == ch:
stack.pop()
if stack and stack[-1] == e.char:
self.insert(tk.INSERT, stack.pop(), self.base.theme.editors.bracket_colors[(i%3)] if i > -1 else 'red')
else:
self.insert(tk.INSERT, e.char, 'red')
return "break"

def complete_pair(self, e: tk.Event):
end = {"(": ")", "{": "}", "[": "]", "\"": "\"", "'": "'"}.get(e.char)

Expand Down
2 changes: 1 addition & 1 deletion biscuit/core/layout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, base: App, *args, **kwargs) -> None:
grip_w.bind("<B1-Motion>", lambda _: self.base.resize('w'))
grip_w.pack(fill=tk.Y, side=tk.LEFT)

container = Frame(self)
container = Frame(self, bg=self.base.theme.border)
container.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)

grip_e = tk.Frame(self, bg=self.base.theme.primary_background, cursor='right_side')
Expand Down

0 comments on commit c0e0adf

Please sign in to comment.