~ubuntu-branches/ubuntu/wily/spyder/wily

« back to all changes in this revision

Viewing changes to spyderlib/pygments_patch.py

  • Committer: Package Import Robot
  • Author(s): Benjamin Drung
  • Date: 2015-01-15 12:20:11 UTC
  • mfrom: (18.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20150115122011-cc7j5dhy2h9uo13m
Tags: 2.3.2+dfsg-1ubuntu1
Backport patch to support pylint3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright © 2014 The Spyder development team
 
4
# Licensed under the terms of the MIT License
 
5
# (see spyderlib/__init__.py for details)
 
6
 
 
7
"""
 
8
Patching pygments to avoid errors with IPython console
 
9
"""
 
10
 
 
11
def apply():
 
12
    """
 
13
    Monkey patching pygments
 
14
    See Issue 2042 and https://github.com/ipython/ipython/pull/6878
 
15
    """
 
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'):
 
19
          return
 
20
    
 
21
    # Patching IPython's patch of RegLexer (Oh God!!)
 
22
    from pygments.lexer import _TokenType, Text, Error
 
23
    try:
 
24
        from IPython.qt.console.pygments_highlighter import RegexLexer
 
25
    except ImportError:
 
26
        from IPython.frontend.qt.console.pygments_highlighter import RegexLexer
 
27
    def get_tokens_unprocessed(self, text, stack=('root',)):
 
28
        pos = 0
 
29
        tokendefs = self._tokens
 
30
        if hasattr(self, '_saved_state_stack'):
 
31
            statestack = list(self._saved_state_stack)
 
32
        else:
 
33
            statestack = list(stack)
 
34
        statetokens = tokendefs[statestack[-1]]
 
35
        while 1:
 
36
            for rexmatch, action, new_state in statetokens:
 
37
                m = rexmatch(text, pos)
 
38
                if m:
 
39
                    if action is not None:
 
40
                        if type(action) is _TokenType:
 
41
                            yield pos, action, m.group()
 
42
                        else:
 
43
                            for item in action(self, m):
 
44
                                yield item
 
45
                    pos = m.end()
 
46
                    if new_state is not None:
 
47
                        # state transition
 
48
                        if isinstance(new_state, tuple):
 
49
                            for state in new_state:
 
50
                                if state == '#pop':
 
51
                                    statestack.pop()
 
52
                                elif state == '#push':
 
53
                                    statestack.append(statestack[-1])
 
54
                                else:
 
55
                                    statestack.append(state)
 
56
                        elif isinstance(new_state, int):
 
57
                            # pop
 
58
                            del statestack[new_state:]
 
59
                        elif new_state == '#push':
 
60
                            statestack.append(statestack[-1])
 
61
                        else:
 
62
                            assert False, "wrong state def: %r" % new_state
 
63
                        statetokens = tokendefs[statestack[-1]]
 
64
                    break
 
65
            else:
 
66
                try:
 
67
                    if text[pos] == '\n':
 
68
                        # at EOL, reset state to "root"
 
69
                        pos += 1
 
70
                        statestack = ['root']
 
71
                        statetokens = tokendefs['root']
 
72
                        yield pos, Text, u'\n'
 
73
                        continue
 
74
                    yield pos, Error, text[pos]
 
75
                    pos += 1
 
76
                except IndexError:
 
77
                    break
 
78
        self._saved_state_stack = list(statestack)
 
79
 
 
80
    RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed