Skip to content

Commit

Permalink
Prevent computing full document content highlight and only parse curr…
Browse files Browse the repository at this point in the history
…ent block content for performance (#246)
  • Loading branch information
dalthviz authored May 28, 2024
1 parent 0ec5cd3 commit 7b1aefd
Showing 1 changed file with 2 additions and 15 deletions.
17 changes: 2 additions & 15 deletions src/superqt/utils/_code_syntax_highlight.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from itertools import takewhile

from pygments import highlight
from pygments.formatter import Formatter
from pygments.lexers import find_lexer_class, get_lexer_by_name
Expand Down Expand Up @@ -68,21 +66,10 @@ def background_color(self):
return self.formatter.style.background_color

def highlightBlock(self, text):
cb = self.currentBlock()
p = cb.position()
text_ = self.document().toPlainText() + "\n"
highlight(text_, self.lexer, self.formatter)

enters = sum(1 for _ in takewhile(lambda x: x == "\n", text_))
# pygments lexer ignore leading empty lines, so we need to do correction
# here calculating the number of empty lines.

# dirty, dirty hack
# The core problem is that pygemnts by default use string streams,
# that will not handle QTextCharFormat, so we need use `data` property to
# work around this.
highlight(text, self.lexer, self.formatter)
for i in range(len(text)):
try:
self.setFormat(i, 1, self.formatter.data[p + i - enters])
except IndexError: # pragma: no cover
pass
self.setFormat(i, 1, self.formatter.data[i])

0 comments on commit 7b1aefd

Please sign in to comment.