forked from yzt000000/device_config_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_highlighter.py
More file actions
55 lines (46 loc) · 2.43 KB
/
python_highlighter.py
File metadata and controls
55 lines (46 loc) · 2.43 KB
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
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QColor, QFont
from PyQt5.QtCore import QRegularExpression
class PythonHighlighter(QSyntaxHighlighter):
def __init__(self, parent=None):
super(PythonHighlighter, self).__init__(parent)
self.keywords = [
'and', 'as', 'assert', 'break', 'class', 'continue', 'def',
'del', 'elif', 'else', 'except', 'False', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'None',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'True',
'try', 'while', 'with', 'yield'
]
self.keyword_format = QTextCharFormat()
self.keyword_format.setForeground(QColor('blue'))
self.keyword_format.setFontWeight(QFont.Bold)
self.string_format = QTextCharFormat()
self.string_format.setForeground(QColor('magenta'))
self.comment_format = QTextCharFormat()
self.comment_format.setForeground(QColor('green'))
self.comment_format.setFontItalic(True)
def highlightBlock(self, text):
for word in self.keywords:
expression = QRegularExpression(r'\b' + word + r'\b')
index = expression.match(text).capturedStart()
while index != -1:
length = expression.match(text).capturedLength()
self.setFormat(index, length, self.keyword_format)
index = expression.match(text, index + length).capturedStart()
expression = QRegularExpression(r'".*?"')
index = expression.match(text).capturedStart()
while index != -1:
length = expression.match(text).capturedLength()
self.setFormat(index, length, self.string_format)
index = expression.match(text, index + length).capturedStart()
expression = QRegularExpression(r"'.*?'")
index = expression.match(text).capturedStart()
while index != -1:
length = expression.match(text).capturedLength()
self.setFormat(index, length, self.string_format)
index = expression.match(text, index + length).capturedStart()
expression = QRegularExpression(r'#.*')
index = expression.match(text).capturedStart()
while index != -1:
length = expression.match(text).capturedLength()
self.setFormat(index, length, self.comment_format)
index = expression.match(text, index + length).capturedStart()