5 from python_qt_binding.QtCore
import QRegExp, Qt
6 from python_qt_binding.QtGui
import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
9 """Return a QTextCharFormat with the given attributes. 12 if type(color) == QColor:
15 _color.setNamedColor(color)
17 _format = QTextCharFormat()
18 _format.setForeground(_color)
20 _format.setFontWeight(QFont.Bold)
22 _format.setFontItalic(
True)
31 'defaults':
format(
'black'),
35 'defclass':
format(
'black',
'bold'),
36 'string':
format(
'darkmagenta'),
37 'string2':
format(
'green'),
38 'comment':
format(
'darkGray',
'italic'),
39 'self':
format(
'black',
'italic'),
40 'numbers':
format(
'brown'),
44 'defaults':
format(
'white'),
45 'keyword':
format(
'lightBlue',
'bold'),
46 'operator':
format(
'orange'),
48 'defclass':
format(
'lightGreen',
'bold'),
49 'string':
format(
'darkmagenta'),
50 'string2':
format(
'green'),
51 'comment':
format(
'darkGray',
'italic'),
52 'self':
format(
'white',
'italic'),
53 'numbers':
format(
'brown'),
58 """Syntax highlighter for the Python language. 68 'and',
'assert',
'break',
'class',
'continue',
'def',
69 'del',
'elif',
'else',
'except',
'exec',
'finally',
70 'for',
'from',
'global',
'if',
'import',
'in',
71 'is',
'lambda',
'not',
'or',
'pass',
'print',
72 'raise',
'return',
'try',
'while',
'yield',
73 'None',
'True',
'False',
'str',
'as' 80 '==',
'!=',
'<',
'<=',
'>',
'>=',
82 '\+',
'-',
'\*',
'/',
'//',
'\%',
'\*\*',
84 '\+=',
'-=',
'\*=',
'/=',
'\%=',
86 '\^',
'\|',
'\&',
'\~',
'>>',
'<<',
91 '\{',
'\}',
'\(',
'\)',
'\[',
'\]',
93 def __init__(self, document, is_dark = False, default = None):
94 QSyntaxHighlighter.__init__(self, document)
96 def contents_changed():
99 document.contentsChanged.connect(contents_changed)
109 STYLES = BRIGHT_STYLES
112 STYLES[
'defaults'] =
reformat(*default)
122 rules += [(
r'%s' % w, 0, STYLES[
'defaults'])
123 for w
in PythonHighlighter.defaults]
125 rules += [(
r'\b%s\b' % w, 0, STYLES[
'keyword'])
126 for w
in PythonHighlighter.keywords]
127 rules += [(
r'%s' % o, 0, STYLES[
'operator'])
128 for o
in PythonHighlighter.operators]
129 rules += [(
r'%s' % b, 0, STYLES[
'brace'])
130 for b
in PythonHighlighter.braces]
135 (
r'\bself\b', 0, STYLES[
'self']),
138 (
r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES[
'string']),
140 (
r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES[
'string']),
143 (
r'\bdef\b\s*(\w+)', 1, STYLES[
'defclass']),
145 (
r'\bclass\b\s*(\w+)', 1, STYLES[
'defclass']),
148 (
r'#[^\n]*', 0, STYLES[
'comment']),
151 (
r'\b[+-]?[0-9]+[lL]?\b', 0, STYLES[
'numbers']),
152 (
r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, STYLES[
'numbers']),
153 (
r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES[
'numbers']),
157 self.
rules = [(QRegExp(pat), index, fmt)
158 for (pat, index, fmt)
in rules]
163 """Apply syntax highlighting to the given block of text. 172 STYLES = BRIGHT_STYLES
178 self.setFormat( 0, len(text), STYLES[
'defaults'])
181 for expression, nth, format
in self.
rules:
182 index = expression.indexIn(text, 0)
186 index = expression.pos(nth)
188 length = len(expression.cap(nth))
189 self.setFormat(index, length, format)
190 index = expression.indexIn(text, index + length)
192 self.setCurrentBlockState(0)
201 """Do highlighting of multi-line strings. ``delimiter`` should be a 202 ``QRegExp`` for triple-single-quotes or triple-double-quotes, and 203 ``in_state`` should be a unique integer to represent the corresponding 204 state changes when inside those strings. Returns True if we're still 205 inside a multi-line string when this function is finished. 208 if self.previousBlockState() == in_state:
213 start = delimiter.indexIn(text)
215 add = delimiter.matchedLength()
220 end = delimiter.indexIn(text, start + add)
223 length = end - start + add + delimiter.matchedLength()
224 self.setCurrentBlockState(0)
227 self.setCurrentBlockState(in_state)
228 length = len(text) - start + add
230 self.setFormat(start, length, style)
232 start = delimiter.indexIn(text, start + length)
235 if self.currentBlockState() == in_state:
def match_multiline(self, text, delimiter, in_state, style)
def __init__(self, document, is_dark=False, default=None)
def format(color, style='')
def highlightBlock(self, text)