Skip to content

Commit

Permalink
highlight_area() (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: rdbende <[email protected]>
  • Loading branch information
Moosems and rdbende authored Apr 10, 2023
1 parent e3b51ba commit 7255c3c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion chlorophyll/codeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ def highlight_all(self) -> None:
self.tag_add(token, start_index, end_index)
start_index = end_index

def highlight_area(self, start_line: int, end_line: int) -> None:
for tag in self.tag_names(index=None):
if tag.startswith("Token"):
self.tag_remove(tag, f"{start_line}.0", f"{end_line}.end")

text = self.get(f"{start_line}.0", f"{end_line}.end")
start_index = f"{start_line}.0"
for token, text in pygments.lex(text, self._lexer()):
token = str(token)
end_index = self.index(f"{start_index} + {len(text)} indices")
if token not in {"Token.Text.Whitespace", "Token.Text"}:
self.tag_add(token, start_index, end_index)
start_index = end_index

def _set_color_scheme(
self, color_scheme: dict[str, dict[str, str | int]] | str | None
) -> None:
Expand Down Expand Up @@ -254,7 +268,7 @@ def horizontal_scroll(self, first: str | float, last: str | float) -> CodeView:

def vertical_scroll(self, first: str | float, last: str | float) -> CodeView:
self._vs.set(first, last)
self._line_numbers.reload(self.cget("font"))
self._line_numbers.reload()

def scroll_line_update(self, event: Event | None = None) -> CodeView:
self.horizontal_scroll(*self.xview())
Expand Down

0 comments on commit 7255c3c

Please sign in to comment.