From b60e81067e953633a96e7611207c7acb7356f23f Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Thu, 7 Nov 2024 13:21:17 +0900 Subject: [PATCH 1/3] use dict.get --- src/superqt/utils/_code_syntax_highlight.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/superqt/utils/_code_syntax_highlight.py b/src/superqt/utils/_code_syntax_highlight.py index d990f8a1..60587287 100644 --- a/src/superqt/utils/_code_syntax_highlight.py +++ b/src/superqt/utils/_code_syntax_highlight.py @@ -36,7 +36,7 @@ def get_text_char_format(style): class QFormatter(Formatter): def __init__(self, **kwargs): super().__init__(**kwargs) - self.data = [] + self.data: list[QtGui.QTextCharFormat] = [] self._style = {name: get_text_char_format(style) for name, style in self.style} def format(self, tokensource, outfile): @@ -49,7 +49,9 @@ def format(self, tokensource, outfile): self.data = [] for token, value in tokensource: - self.data.extend([self._style[token]] * len(value)) + self.data.extend( + [self._style.get(token, QtGui.QTextCharFormat())] * len(value) + ) class CodeSyntaxHighlight(QtGui.QSyntaxHighlighter): From 68b49c98a4fbdda3bf0e5294bf03cbdb74e7fd04 Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Thu, 7 Nov 2024 15:36:39 +0900 Subject: [PATCH 2/3] typing --- src/superqt/utils/_code_syntax_highlight.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/superqt/utils/_code_syntax_highlight.py b/src/superqt/utils/_code_syntax_highlight.py index 60587287..aee923a7 100644 --- a/src/superqt/utils/_code_syntax_highlight.py +++ b/src/superqt/utils/_code_syntax_highlight.py @@ -9,7 +9,9 @@ # https://pygments.org/docs/formatterdevelopment/#html-3-2-formatter -def get_text_char_format(style): +def get_text_char_format( + style: dict[str, QtGui.QTextCharFormat], +) -> QtGui.QTextCharFormat: text_char_format = QtGui.QTextCharFormat() if hasattr(text_char_format, "setFontFamilies"): text_char_format.setFontFamilies(["monospace"]) From 6728c02e55443d3bdcf795937ca09de5429cf4b8 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 14 Dec 2024 11:53:48 -0500 Subject: [PATCH 3/3] Update src/superqt/utils/_code_syntax_highlight.py Co-authored-by: Grzegorz Bokota --- src/superqt/utils/_code_syntax_highlight.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/superqt/utils/_code_syntax_highlight.py b/src/superqt/utils/_code_syntax_highlight.py index aee923a7..73f2ef15 100644 --- a/src/superqt/utils/_code_syntax_highlight.py +++ b/src/superqt/utils/_code_syntax_highlight.py @@ -51,6 +51,8 @@ def format(self, tokensource, outfile): self.data = [] for token, value in tokensource: + # using get method to workaround not defined style for plain token + # https://github.com/pygments/pygments/issues/2149 self.data.extend( [self._style.get(token, QtGui.QTextCharFormat())] * len(value) )