Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: KeyError in CodeSyntaxHighlight #258

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/superqt/utils/_code_syntax_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Czaki marked this conversation as resolved.
Show resolved Hide resolved
) -> QtGui.QTextCharFormat:
text_char_format = QtGui.QTextCharFormat()
if hasattr(text_char_format, "setFontFamilies"):
text_char_format.setFontFamilies(["monospace"])
Expand All @@ -36,7 +38,7 @@ def get_text_char_format(style):
class QFormatter(Formatter):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.data = []
self.data: list[QtGui.QTextCharFormat] = []
Czaki marked this conversation as resolved.
Show resolved Hide resolved
self._style = {name: get_text_char_format(style) for name, style in self.style}

def format(self, tokensource, outfile):
Expand All @@ -49,7 +51,11 @@ def format(self, tokensource, outfile):
self.data = []

for token, value in tokensource:
self.data.extend([self._style[token]] * len(value))
# 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)
)
tlambert03 marked this conversation as resolved.
Show resolved Hide resolved


class CodeSyntaxHighlight(QtGui.QSyntaxHighlighter):
Expand Down
Loading