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

Conversation

hanjinliu
Copy link
Contributor

I noticed that CodeSyntaxHighlight sometimes fails to highlight the texts. This PR fixes the KeyError just by replacing the __getitem__ with get. I encountered this bug when I tried to highlight my pre-commit-config.yaml file, so I cannot tell when this happens.

from superqt.utils import CodeSyntaxHighlight
from qtpy import QtWidgets as QtW

w = QtW.QPlainTextEdit()
highlight = CodeSyntaxHighlight(w.document(), "yaml", "default")
w.setPlainText("ci:\n  autoupdate_schedule: monthly")

Output:

KeyError                                  Traceback (most recent call last)
File ~\Desktop\code\python\superqt\src\superqt\utils\_code_syntax_highlight.py:73, in CodeSyntaxHighlight.highlightBlock(self, text)
     68 def highlightBlock(self, text):
     69     # dirty, dirty hack
     70     # The core problem is that pygemnts by default use string streams,
     71     # that will not handle QTextCharFormat, so we need use `data` property to
     72     # work around this.
---> 73     highlight(text, self.lexer, self.formatter)
     74     for i in range(len(text)):
     75         self.setFormat(i, 1, self.formatter.data[i])

File ~\mambaforge\envs\mt\Lib\site-packages\pygments\__init__.py:82, in highlight(code, lexer, formatter, outfile)
     77 def highlight(code, lexer, formatter, outfile=None):
     78     """
     79     This is the most high-level highlighting function. It combines `lex` and
     80     `format` in one function.
     81     """
---> 82     return format(lex(code, lexer), formatter, outfile)

File ~\mambaforge\envs\mt\Lib\site-packages\pygments\__init__.py:64, in format(tokens, formatter, outfile)
     62 if not outfile:
     63     realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
---> 64     formatter.format(tokens, realoutfile)
     65     return realoutfile.getvalue()
     66 else:

File ~\Desktop\code\python\superqt\src\superqt\utils\_code_syntax_highlight.py:52, in QFormatter.format(self, tokensource, outfile)
     49 self.data = []
     51 for token, value in tokensource:
---> 52     self.data.extend([self._style[token]] * len(value))

KeyError: Token.Literal.Scalar.Plain

@Czaki
Copy link
Contributor

Czaki commented Nov 7, 2024

For me, it looks like a bug in pigments.

@tlambert03
Copy link
Member

For me, it looks like a bug in pigments.

what do you suggest then?

@Czaki
Copy link
Contributor

Czaki commented Dec 4, 2024

I will check this in the following days. I am sorry that I forgot about this issue.

@Czaki
Copy link
Contributor

Czaki commented Dec 12, 2024

So yes, it is error reported in pygments pygments/pygments#2149

Looking into rich code

https://github.com/Textualize/rich/blob/43d3b04725ab9731727fb1126e35980c62f32377/rich/syntax.py#L152

This PR may be a good approach. Hovewer I prefer to add Token.Literal.Scalar.Plain to self._style.

        self._style = {name: get_text_char_format(style) for name, style in self.style}

I feel like using get may mask some other problem. But rich author decided to mask (with adding to dict for performance), so maybe it is not a bad idea?

I cannot restart CI. Maybe too long time since last commit?

Copy link

codecov bot commented Dec 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.98%. Comparing base (8a40170) to head (6728c02).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #258      +/-   ##
==========================================
- Coverage   87.13%   86.98%   -0.15%     
==========================================
  Files          46       46              
  Lines        3443     3443              
==========================================
- Hits         3000     2995       -5     
- Misses        443      448       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@Czaki Czaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superqt still supports python 3.8 so need to use types from typing, not builtins

src/superqt/utils/_code_syntax_highlight.py Show resolved Hide resolved
src/superqt/utils/_code_syntax_highlight.py Show resolved Hide resolved
@tlambert03
Copy link
Member

superqt still supports python 3.8

happy to drop it now that it's EOL. will open new issue

@tlambert03
Copy link
Member

Hovewer I prefer to add Token.Literal.Scalar.Plain to self._style.

thanks @Czaki. is this something you want to see done in this PR before merge?

@tlambert03 tlambert03 merged commit df00846 into pyapp-kit:main Dec 14, 2024
41 checks passed
@tlambert03 tlambert03 added the bug Something isn't working label Dec 14, 2024
@tlambert03 tlambert03 changed the title Fix KeyError in CodeSyntaxHighlight fix: KeyError in CodeSyntaxHighlight Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

3 participants