Monkeypatch RegexLexer only while in use by qtconsole.
Qtconsole highlight lines one line at a time so needs to remember state.
Other code may use the lexer; So patch only when relevant.
Closes
@@ -74,7 +74,19 @@ def get_tokens_unprocessed(self, text, stack=('root',)):
# Monkeypatch!
+from contextlib import contextmanager
+
+
+@contextmanager
+def _lexpatch():
+
+ try:
+ orig = RegexLexer.get_tokens_unprocessed
RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed
+ yield
+ finally:
+ pass
+ RegexLexer.get_tokens_unprocessed = orig
class PygmentsBlockUserData(QtGui.QTextBlockUserData):
@@ -117,9 +129,10 @@ class PygmentsHighlighter(QtGui.QSyntaxHighlighter):
""" Highlight a block of text.
"""
prev_data = self.currentBlock().previous().userData()
+ with _lexpatch():
if prev_data is not None:
self._lexer._saved_state_stack = prev_data.syntax_stack
- elif hasattr(self._lexer, '_saved_state_stack'):
+ elif hasattr(self._lexer, "_saved_state_stack"):
del self._lexer._saved_state_stack
# Lex the text using Pygments
@@ -129,9 +142,10 @@ class PygmentsHighlighter(QtGui.QSyntaxHighlighter):
self.setFormat(index, length, self._get_format(token))
index += length
- if hasattr(self._lexer, '_saved_state_stack'):
+ if hasattr(self._lexer, "_saved_state_stack"):
data = PygmentsBlockUserData(
- syntax_stack=self._lexer._saved_state_stack)
+ syntax_stack=self._lexer._saved_state_stack
+ )
self.currentBlock().setUserData(data)
# Clean up for the next go-round.
del self._lexer._saved_state_stack
