Show More
@@ -10,14 +10,11 b' from pygments.lexers import PythonLexer' | |||
|
10 | 10 | from PyQt4 import QtCore, QtGui |
|
11 | 11 | import zmq |
|
12 | 12 | |
|
13 | # ETS imports | |
|
14 | from enthought.pyface.ui.qt4.code_editor.pygments_highlighter import \ | |
|
15 | PygmentsHighlighter | |
|
16 | ||
|
17 | 13 | # Local imports |
|
18 | 14 | from call_tip_widget import CallTipWidget |
|
19 | 15 | from completion_lexer import CompletionLexer |
|
20 | 16 | from console_widget import HistoryConsoleWidget |
|
17 | from pygments_highlighter import PygmentsHighlighter | |
|
21 | 18 | |
|
22 | 19 | |
|
23 | 20 | class FrontendReplyThread(Thread, QtCore.QObject): |
@@ -1,16 +1,5 b'' | |||
|
1 | #------------------------------------------------------------------------------ | |
|
2 | # Copyright (c) 2010, Enthought Inc | |
|
3 | # All rights reserved. | |
|
4 | # | |
|
5 | # This software is provided without warranty under the terms of the BSD license. | |
|
6 | ||
|
7 | # | |
|
8 | # Author: Enthought Inc | |
|
9 | # Description: <Enthought pyface code editor> | |
|
10 | #------------------------------------------------------------------------------ | |
|
11 | ||
|
1 | # System library imports. | |
|
12 | 2 | from PyQt4 import QtGui |
|
13 | ||
|
14 | 3 | from pygments.lexer import RegexLexer, _TokenType, Text, Error |
|
15 | 4 | from pygments.lexers import CLexer, CppLexer, PythonLexer |
|
16 | 5 | from pygments.styles.default import DefaultStyle |
@@ -24,8 +13,8 b" def get_tokens_unprocessed(self, text, stack=('root',)):" | |||
|
24 | 13 | """ |
|
25 | 14 | pos = 0 |
|
26 | 15 | tokendefs = self._tokens |
|
27 |
if hasattr(self, '_e |
|
|
28 |
statestack = list(self._e |
|
|
16 | if hasattr(self, '_saved_state_stack'): | |
|
17 | statestack = list(self._saved_state_stack) | |
|
29 | 18 | else: |
|
30 | 19 | statestack = list(stack) |
|
31 | 20 | statetokens = tokendefs[statestack[-1]] |
@@ -71,39 +60,12 b" def get_tokens_unprocessed(self, text, stack=('root',)):" | |||
|
71 | 60 | pos += 1 |
|
72 | 61 | except IndexError: |
|
73 | 62 | break |
|
74 |
self._e |
|
|
63 | self._saved_state_stack = list(statestack) | |
|
75 | 64 | |
|
76 | 65 | # Monkeypatch! |
|
77 | 66 | RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed |
|
78 | 67 | |
|
79 | 68 | |
|
80 | # Even with the above monkey patch to store state, multiline comments do not | |
|
81 | # work since they are stateless (Pygments uses a single multiline regex for | |
|
82 | # these comments, but Qt lexes by line). So we need to add a state for comments | |
|
83 | # to the C and C++ lexers. This means that nested multiline comments will appear | |
|
84 | # to be valid C/C++, but this is better than the alternative for now. | |
|
85 | ||
|
86 | def replace_pattern(tokens, new_pattern): | |
|
87 | """ Given a RegexLexer token dictionary 'tokens', replace all patterns that | |
|
88 | match the token specified in 'new_pattern' with 'new_pattern'. | |
|
89 | """ | |
|
90 | for state in tokens.values(): | |
|
91 | for index, pattern in enumerate(state): | |
|
92 | if isinstance(pattern, tuple) and pattern[1] == new_pattern[1]: | |
|
93 | state[index] = new_pattern | |
|
94 | ||
|
95 | # More monkeypatching! | |
|
96 | comment_start = (r'/\*', Comment.Multiline, 'comment') | |
|
97 | comment_state = [ (r'[^*/]', Comment.Multiline), | |
|
98 | (r'/\*', Comment.Multiline, '#push'), | |
|
99 | (r'\*/', Comment.Multiline, '#pop'), | |
|
100 | (r'[*/]', Comment.Multiline) ] | |
|
101 | replace_pattern(CLexer.tokens, comment_start) | |
|
102 | replace_pattern(CppLexer.tokens, comment_start) | |
|
103 | CLexer.tokens['comment'] = comment_state | |
|
104 | CppLexer.tokens['comment'] = comment_state | |
|
105 | ||
|
106 | ||
|
107 | 69 | class BlockUserData(QtGui.QTextBlockUserData): |
|
108 | 70 | """ Storage for the user data associated with each line. |
|
109 | 71 | """ |
@@ -141,9 +103,9 b' class PygmentsHighlighter(QtGui.QSyntaxHighlighter):' | |||
|
141 | 103 | prev_data = self.previous_block_data() |
|
142 | 104 | |
|
143 | 105 | if prev_data is not None: |
|
144 |
self._lexer._e |
|
|
145 |
elif hasattr(self._lexer, '_e |
|
|
146 |
del self._lexer._e |
|
|
106 | self._lexer._saved_state_stack = prev_data.syntax_stack | |
|
107 | elif hasattr(self._lexer, '_saved_state_stack'): | |
|
108 | del self._lexer._saved_state_stack | |
|
147 | 109 | |
|
148 | 110 | index = 0 |
|
149 | 111 | # Lex the text using Pygments |
@@ -154,11 +116,11 b' class PygmentsHighlighter(QtGui.QSyntaxHighlighter):' | |||
|
154 | 116 | self.setFormat(index, l, format) |
|
155 | 117 | index += l |
|
156 | 118 | |
|
157 |
if hasattr(self._lexer, '_e |
|
|
158 |
data = BlockUserData(syntax_stack=self._lexer._e |
|
|
119 | if hasattr(self._lexer, '_saved_state_stack'): | |
|
120 | data = BlockUserData(syntax_stack=self._lexer._saved_state_stack) | |
|
159 | 121 | self.currentBlock().setUserData(data) |
|
160 | 122 | # Clean up for the next go-round. |
|
161 |
del self._lexer._e |
|
|
123 | del self._lexer._saved_state_stack | |
|
162 | 124 | |
|
163 | 125 | def previous_block_data(self): |
|
164 | 126 | """ Convenience method for returning the previous block's user data. |
General Comments 0
You need to be logged in to leave comments.
Login now