From f41582e67b2b69cdad2b54ad83051243e9e3df21 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Mon, 10 Apr 2023 16:29:03 -0400 Subject: [PATCH 1/6] First commit --- chlorophyll/codeview.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 628a183..900df01 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -254,8 +254,22 @@ 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()) self.vertical_scroll(*self.yview()) + + def hightlight_area(self, start_line: int, end_line: int) -> None: + for i in range(start_line, end_line + 1): + for tag in self.tag_names(index=None): + if tag.startswith("Token"): + self.tag_remove(tag, f"{i}.0", f"{i}.end") + + line_text = self.get(f"{i}.0", f"{i}.end") + start_col = 0 + + for token, text in pygments.lex(line_text, self._lexer()): + end_col = start_col + len(text) + self.tag_add(str(token), f"{i}.{start_col}", f"{i}.{end_col}") + start_col = end_col \ No newline at end of file From 60ef1bda36b80da91387edb88e3cec575febd1c2 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Mon, 10 Apr 2023 16:38:26 -0400 Subject: [PATCH 2/6] Move it up --- chlorophyll/codeview.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 900df01..2a98266 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -180,6 +180,20 @@ def highlight_all(self) -> None: self.tag_add(token, start_index, end_index) start_index = end_index + def hightlight_area(self, start_line: int, end_line: int) -> None: + for i in range(start_line, end_line + 1): + for tag in self.tag_names(index=None): + if tag.startswith("Token"): + self.tag_remove(tag, f"{i}.0", f"{i}.end") + + line_text = self.get(f"{i}.0", f"{i}.end") + start_col = 0 + + for token, text in pygments.lex(line_text, self._lexer()): + end_col = start_col + len(text) + self.tag_add(str(token), f"{i}.{start_col}", f"{i}.{end_col}") + start_col = end_col + def _set_color_scheme( self, color_scheme: dict[str, dict[str, str | int]] | str | None ) -> None: @@ -259,17 +273,3 @@ def vertical_scroll(self, first: str | float, last: str | float) -> CodeView: def scroll_line_update(self, event: Event | None = None) -> CodeView: self.horizontal_scroll(*self.xview()) self.vertical_scroll(*self.yview()) - - def hightlight_area(self, start_line: int, end_line: int) -> None: - for i in range(start_line, end_line + 1): - for tag in self.tag_names(index=None): - if tag.startswith("Token"): - self.tag_remove(tag, f"{i}.0", f"{i}.end") - - line_text = self.get(f"{i}.0", f"{i}.end") - start_col = 0 - - for token, text in pygments.lex(line_text, self._lexer()): - end_col = start_col + len(text) - self.tag_add(str(token), f"{i}.{start_col}", f"{i}.{end_col}") - start_col = end_col \ No newline at end of file From 822a161c037bc14f33fe436fe24d0e561ed988b6 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Mon, 10 Apr 2023 16:48:54 -0400 Subject: [PATCH 3/6] Misspelling Lol Co-authored-by: rdbende --- chlorophyll/codeview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 2a98266..95190c5 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -180,7 +180,7 @@ def highlight_all(self) -> None: self.tag_add(token, start_index, end_index) start_index = end_index - def hightlight_area(self, start_line: int, end_line: int) -> None: + def highlight_area(self, start_line: int, end_line: int) -> None: for i in range(start_line, end_line + 1): for tag in self.tag_names(index=None): if tag.startswith("Token"): From 4b0adb8c0147ebbb3ad1d7049ab2d749febfcff7 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Mon, 10 Apr 2023 16:58:34 -0400 Subject: [PATCH 4/6] Better? --- chlorophyll/codeview.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 95190c5..320c703 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -181,18 +181,20 @@ def highlight_all(self) -> None: start_index = end_index def highlight_area(self, start_line: int, end_line: int) -> None: - for i in range(start_line, end_line + 1): - for tag in self.tag_names(index=None): - if tag.startswith("Token"): - self.tag_remove(tag, f"{i}.0", f"{i}.end") - - line_text = self.get(f"{i}.0", f"{i}.end") - start_col = 0 - - for token, text in pygments.lex(line_text, self._lexer()): - end_col = start_col + len(text) - self.tag_add(str(token), f"{i}.{start_col}", f"{i}.{end_col}") - start_col = end_col + 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") + lexer = self._lexer() + tokens = pygments.lex(text, lexer) + start_index = f"{start_line}.0" + for token, text in tokens: + 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 From f4479f261a75c6e7d253286e41504178b80971b2 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Mon, 10 Apr 2023 17:05:28 -0400 Subject: [PATCH 5/6] Cleaner Co-authored-by: rdbende --- chlorophyll/codeview.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 320c703..d9ff59b 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -183,12 +183,11 @@ def highlight_all(self) -> None: 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") - lexer = self._lexer() - tokens = pygments.lex(text, lexer) + self.tag_remove(tag, f"{start_line} linestart", f"{end_line} lineend") + + text = self.get(f"{start_line} linestart", f"{end_line} lineend") start_index = f"{start_line}.0" - for token, text in tokens: + 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"}: From b35a51982d5f6800f59aba04a3f7d6d9519f3f30 Mon Sep 17 00:00:00 2001 From: rdbende Date: Mon, 10 Apr 2023 23:07:14 +0200 Subject: [PATCH 6/6] Fix'd --- chlorophyll/codeview.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index d9ff59b..fb3c5bd 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -183,9 +183,9 @@ def highlight_all(self) -> None: 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} linestart", f"{end_line} lineend") + self.tag_remove(tag, f"{start_line}.0", f"{end_line}.end") - text = self.get(f"{start_line} linestart", f"{end_line} lineend") + 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) @@ -194,7 +194,6 @@ def highlight_area(self, start_line: int, end_line: int) -> None: 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: