-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.py
42 lines (33 loc) · 1.41 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
from PySide6.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QColor
import re
class Highlighter(QSyntaxHighlighter):
def __init__(self, document=None):
super().__init__(document)
self._mappings = {}
def add_mapping(self, pattern, style):
self._mappings[pattern] = style
def highlightBlock(self, text):
for pattern, style in self._mappings.items():
for match in re.finditer(pattern, text):
start, end = match.span()
self.setFormat(start, end - start, style)
def setup(self):
value_format = QTextCharFormat()
value_format.setFontItalic(True)
value_format.setForeground(QColor("cyan"))
pattern = r':\s*.*;*$'
self.add_mapping(pattern, value_format)
property_format = QTextCharFormat()
property_format.setFontWeight(QFont.Bold)
property_format.setForeground(QColor("orange"))
pattern = r'^\s*[^:]*'
self.add_mapping(pattern, property_format)
selector_format = QTextCharFormat()
selector_format.setFontWeight(QFont.Bold)
selector_format.setForeground(QColor("magenta"))
pattern = r'^\s*.*{'
self.add_mapping(pattern, selector_format)
punctuation_format = QTextCharFormat()
pattern = r'[;:{}]'
punctuation_format.setForeground(QColor("white"))
self.add_mapping(pattern, punctuation_format)