| 32 | |
| 33 | |
| 34 | class JsonHighlighter(QSyntaxHighlighter): |
| 35 | def __init__(self, *args, **kwargs): |
| 36 | super().__init__(*args, **kwargs) |
| 37 | self._rules = [ |
| 38 | # numbers |
| 39 | HighlightingRule(QRegExp('([-0-9.]+)(?!([^"]*"[\\s]*\\:))'), Qt.darkRed), |
| 40 | # key |
| 41 | HighlightingRule(QRegExp('("[^"]*")\\s*\\:'), Qt.darkBlue), |
| 42 | # value |
| 43 | HighlightingRule(QRegExp(':+(?:[: []*)("[^"]*")'), Qt.darkGreen), |
| 44 | ] |
| 45 | |
| 46 | def highlightBlock(self, text: str) -> None: |
| 47 | for rule in self._rules: |
| 48 | index = rule.pattern.indexIn(text) |
| 49 | while index >= 0: |
| 50 | length = rule.pattern.matchedLength() |
| 51 | self.setFormat(index, length, rule.format) |
| 52 | index = rule.pattern.indexIn(text, index + length) |
| 53 | |
| 54 | |
| 55 | class TestWindow(QWidget, Ui_SerializeWidget): |