39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from dataclasses import dataclass
|
|
from PySide6 import QtWidgets, QtGui, QtCore
|
|
|
|
from TextFormat import TextFormat
|
|
|
|
@dataclass
|
|
class Rule:
|
|
name: str
|
|
enable: bool
|
|
format: TextFormat
|
|
regex: QtCore.QRegularExpression
|
|
|
|
def isEnable(self):
|
|
return self.enable
|
|
|
|
def getTextCharFormat(self):
|
|
return self.format.format
|
|
|
|
def setPattern(self, pattern: str):
|
|
self.regex.setPattern(pattern)
|
|
|
|
class Highlighter(QtGui.QSyntaxHighlighter):
|
|
def __init__(self, document):
|
|
super().__init__(document)
|
|
self.rules: list[Rule] = []
|
|
|
|
def setListOfRules(self, rules: list[Rule]):
|
|
self.rules = rules
|
|
|
|
def highlightBlock(self, text):
|
|
for rule in self.rules:
|
|
if not rule.enable:
|
|
continue
|
|
i = rule.regex.globalMatch(text)
|
|
while i.hasNext():
|
|
match = i.next()
|
|
# print(f"0: {match.captured(0)}, 1: {match.captured(1)}, start: {match.capturedStart(1)}, lenght: {match.capturedLength(1)}")
|
|
self.setFormat(match.capturedStart(1), match.capturedLength(1), rule.getTextCharFormat())
|