1
# -*- coding: utf-8 -*-
3
# Copyright © 2014 The Spyder development team
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
8
Patching pygments to avoid errors with IPython console
13
Monkey patching pygments
14
See Issue 2042 and https://github.com/ipython/ipython/pull/6878
16
from spyderlib.utils.programs import is_module_installed
17
if is_module_installed('pygments', '<2.0') and \
18
is_module_installed('IPython', '>3.0'):
21
# Patching IPython's patch of RegLexer (Oh God!!)
22
from pygments.lexer import _TokenType, Text, Error
24
from IPython.qt.console.pygments_highlighter import RegexLexer
26
from IPython.frontend.qt.console.pygments_highlighter import RegexLexer
27
def get_tokens_unprocessed(self, text, stack=('root',)):
29
tokendefs = self._tokens
30
if hasattr(self, '_saved_state_stack'):
31
statestack = list(self._saved_state_stack)
33
statestack = list(stack)
34
statetokens = tokendefs[statestack[-1]]
36
for rexmatch, action, new_state in statetokens:
37
m = rexmatch(text, pos)
39
if action is not None:
40
if type(action) is _TokenType:
41
yield pos, action, m.group()
43
for item in action(self, m):
46
if new_state is not None:
48
if isinstance(new_state, tuple):
49
for state in new_state:
52
elif state == '#push':
53
statestack.append(statestack[-1])
55
statestack.append(state)
56
elif isinstance(new_state, int):
58
del statestack[new_state:]
59
elif new_state == '#push':
60
statestack.append(statestack[-1])
62
assert False, "wrong state def: %r" % new_state
63
statetokens = tokendefs[statestack[-1]]
68
# at EOL, reset state to "root"
71
statetokens = tokendefs['root']
72
yield pos, Text, u'\n'
74
yield pos, Error, text[pos]
78
self._saved_state_stack = list(statestack)
80
RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed