-
Notifications
You must be signed in to change notification settings - Fork 2
/
highlighter.py
189 lines (148 loc) · 6.52 KB
/
highlighter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
#-*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import Qt, QRegExp
from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QPalette, QSyntaxHighlighter
from PyQt5.QtWidgets import QMainWindow, QApplication, QPlainTextEdit
def format(color, style=''):
word_color = QColor()
word_color.setNamedColor(color)
word_format = QTextCharFormat()
word_format.setForeground(word_color)
if 'italic' in style:
word_format.setFontItalic(True)
elif 'bold' in style:
word_format.setFontWeight(QFont.Bold)
return word_format
STYLE = {
'keywords': format('#F92672'),
'booleans': format('#C1FF00'),
'operators': format('#F92672'),
'braces': format('#BABAB6'),
'comments': format('#9EAF9F'),
'def_class': format('#66D9EF'),
'string': format('#F2C560'),
'doc_string': format('#F2C560'),
'self': format('#FF9721', 'italic'),
'numbers': format('#66CB64'),
}
class PythonHighlighter(QSyntaxHighlighter):
keywords = [
'and', 'as', 'assert', 'break', 'class', 'continue', 'def',
'del', 'elif', 'else', 'except', 'exec', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'not', 'or', 'pass', 'print', 'raise', 'return', 'try',
'while', 'with', 'yield',
]
booleans = [
'True', 'False', 'None',
]
operators = [
'=',
'==', '!=', '<', '<=', '>', '>=',
'\+', '-', '\*', '/', '//', '\%', '\*\*',
'\+=', '-=', '\*=', '/=', '\%=',
'\^', '\|', '\&', '\~', '>>', '<<',
]
braces = [
'\{', '\}', '\[', '\]', '\(', '\)'
]
def __init__(self, document):
QSyntaxHighlighter.__init__(self, document)
self.triple_single = (QRegExp("\'\'\'"), 1, STYLE['doc_string'])
self.triple_double = (QRegExp('\"\"\"'), 2, STYLE['doc_string'])
# RULES
rules = []
# r'' regular expression
# [] used to indicate set of characters
# \b matches the empty string, but only at the beginning or end of the word
# \w matches any alphanumeric chars and the underscore
# \s matches any whitespace char
# * causes the resulting regexp to match 0 or more repetitions of the preceding regexp. ab* = a, ab, abb, abbb..
# + causes the resulting regexp to match 1 or more repetitions of the preceding regexp. ab+ = ab, abb, abbbb..
# ? causes the resulting regexp to match 0 or 1 repetitions of the preceding regexp. ab? = a, ab
# ^ matches the start of the string, and in multi line mode also matches immediately after each new line
# ?: A non-capturing version of regular parentheses
# QRegExp, int, STYLE
rules += [(r'\b%s\b' % keyword, 0, STYLE['keywords']) for keyword in PythonHighlighter.keywords]
rules += [(r'\b%s\b' % boolean, 0, STYLE['booleans']) for boolean in PythonHighlighter.booleans]
rules += [(r'%s' % operator, 0, STYLE['operators']) for operator in PythonHighlighter.operators]
rules += [(r'%s' % brace, 0, STYLE['braces']) for brace in PythonHighlighter.braces]
# Other rules:
rules += [
# self
(r'\bself\b', 0, STYLE['self']),
# string containing double-quote with escape sequence
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLE['string']),
# string containing single-quote with escape sequence
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLE['string']),
# def/class
(r'\bdef\b\s*(\w+)', 1, STYLE['def_class']),
(r'\bclass\b\s*(\w+)', 1, STYLE['def_class']),
# from # until new-line
(r'#[^\n]*', 0, STYLE['comments']),
# numbers
(r'\b[+-]?[0-9]+[lL]?\b', 0, STYLE['numbers']),
(r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLE['numbers']),
# decorator
(r'@[^\n', 0, STYLE['def_class']),
]
# Build QRegExp for each pattern
self.rules = [(QRegExp(pattern), index, fmt) for (pattern, index, fmt) in rules]
def highlightBlock(self, text):
for expression, nth, format in self.rules:
index = expression.indexIn(text, 0)
while index >= 0:
index = expression.pos(nth)
length = len(expression.cap(nth))
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)
self.setCurrentBlockState(0)
in_multiline = self.match_multiline(text, *self.triple_single)
if not in_multiline:
in_multiline = self.match_multiline(text, *self.triple_double)
def match_multiline(self, text, delimiter, in_state, style):
if self.previousBlockState() == in_state:
start = 0
add = 0
else:
start = delimiter.indexIn(text)
add = delimiter.matchedLength()
while start >= 0:
end = delimiter.indexIn(text, start + add)
if end >= add:
length = end - start + add + delimiter.matchedLength()
self.setCurrentBlockState(0)
else:
self.setCurrentBlockState(in_state)
length = len(text) - start + add
self.setFormat(start, length, style)
start = delimiter.indexIn(text, start + length)
if self.currentBlockState() == in_state:
return True
else:
return False
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.text_editor = QPlainTextEdit()
self.syntax = PythonHighlighter(self.text_editor.document())
self.init_ui()
def init_ui(self):
background_color = QColor()
background_color.setNamedColor('#282821')
color_palette = self.text_editor.palette()
color_palette.setColor(QPalette.Text, Qt.white)
color_palette.setColor(QPalette.Base, background_color)
self.text_editor.setPalette(color_palette)
default_font = self.text_editor.font()
default_font.setPointSize(9)
self.text_editor.setFont(default_font)
self.setWindowTitle('Example')
self.setCentralWidget(self.text_editor)
self.setGeometry(500, 500, 500, 500)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())