gui.utils.yaml_highlighter

 1from PySide6.QtGui import QSyntaxHighlighter, QTextCharFormat, QColor, QFont
 2from PySide6.QtCore import QRegularExpression
 3
 4
 5def _fmt(color: str, bold: bool = False, italic: bool = False) -> QTextCharFormat:
 6    f = QTextCharFormat()
 7    f.setForeground(QColor(color))
 8    if bold:
 9        f.setFontWeight(QFont.Weight.Bold)
10    if italic:
11        f.setFontItalic(True)
12    return f
13
14
15_PALETTES = {
16    'dark': {
17        'key':     ('#7dd3fc', True,  False),
18        'string':  ('#86efac', False, False),
19        'number':  ('#fb923c', False, False),
20        'bool':    ('#c084fc', False, False),
21        'comment': ('#6b7280', False, True),
22        'dash':    ('#fbbf24', False, False),
23        'anchor':  ('#f9a8d4', False, False),
24    },
25    'light': {
26        'key':     ('#0369a1', True,  False),
27        'string':  ('#166534', False, False),
28        'number':  ('#c2410c', False, False),
29        'bool':    ('#7e22ce', False, False),
30        'comment': ('#6b7280', False, True),
31        'dash':    ('#b45309', False, False),
32        'anchor':  ('#be185d', False, False),
33    },
34}
35
36_RULES_DEFS = [
37    ('key',     r'(?:^|(?<=[\s\-]))([A-Za-z_][\w\-]*)(?=\s*:)', 1),
38    ('string',  r'"[^"\\]*(?:\\.[^"\\]*)*"',                     0),
39    ('string',  r"'[^']*'",                                      0),
40    ('number',  r'(?<![:\w])-?\b\d+(?:\.\d+)?\b',               0),
41    ('bool',    r'\b(?:true|false|yes|no|null|True|False|Yes|No|Null|~)\b', 0),
42    ('dash',    r'^\s*-(?=\s|$)',                                 0),
43    ('anchor',  r'[&*][A-Za-z_][\w\-]*',                         0),
44    ('comment', r'#.*$',                                          0),
45]
46
47
48class YamlHighlighter(QSyntaxHighlighter):
49    def __init__(self, document, theme: str = 'dark'):
50        super().__init__(document)
51        self._rules: list[tuple] = []
52        self.set_theme(theme)
53
54    def set_theme(self, theme: str) -> None:
55        palette = _PALETTES.get(theme, _PALETTES['dark'])
56        self._rules = []
57        for role, pattern, group in _RULES_DEFS:
58            color, bold, italic = palette[role]
59            self._rules.append((
60                QRegularExpression(pattern, QRegularExpression.PatternOption.MultilineOption),
61                _fmt(color, bold, italic),
62                group,
63            ))
64        self.rehighlight()
65
66    def highlightBlock(self, text: str) -> None:
67        for pattern, fmt, group in self._rules:
68            it = pattern.globalMatch(text)
69            while it.hasNext():
70                m = it.next()
71                start  = m.capturedStart(group)
72                length = m.capturedLength(group)
73                if start >= 0 and length > 0:
74                    self.setFormat(start, length, fmt)
class YamlHighlighter(PySide6.QtGui.QSyntaxHighlighter):
49class YamlHighlighter(QSyntaxHighlighter):
50    def __init__(self, document, theme: str = 'dark'):
51        super().__init__(document)
52        self._rules: list[tuple] = []
53        self.set_theme(theme)
54
55    def set_theme(self, theme: str) -> None:
56        palette = _PALETTES.get(theme, _PALETTES['dark'])
57        self._rules = []
58        for role, pattern, group in _RULES_DEFS:
59            color, bold, italic = palette[role]
60            self._rules.append((
61                QRegularExpression(pattern, QRegularExpression.PatternOption.MultilineOption),
62                _fmt(color, bold, italic),
63                group,
64            ))
65        self.rehighlight()
66
67    def highlightBlock(self, text: str) -> None:
68        for pattern, fmt, group in self._rules:
69            it = pattern.globalMatch(text)
70            while it.hasNext():
71                m = it.next()
72                start  = m.capturedStart(group)
73                length = m.capturedLength(group)
74                if start >= 0 and length > 0:
75                    self.setFormat(start, length, fmt)

QSyntaxHighlighter(self, parent: PySide6.QtGui.QTextDocument, /) -> None QSyntaxHighlighter(self, parent: PySide6.QtCore.QObject, /) -> None

YamlHighlighter(document, theme: str = 'dark')
50    def __init__(self, document, theme: str = 'dark'):
51        super().__init__(document)
52        self._rules: list[tuple] = []
53        self.set_theme(theme)
def set_theme(self, theme: str) -> None:
55    def set_theme(self, theme: str) -> None:
56        palette = _PALETTES.get(theme, _PALETTES['dark'])
57        self._rules = []
58        for role, pattern, group in _RULES_DEFS:
59            color, bold, italic = palette[role]
60            self._rules.append((
61                QRegularExpression(pattern, QRegularExpression.PatternOption.MultilineOption),
62                _fmt(color, bold, italic),
63                group,
64            ))
65        self.rehighlight()
def highlightBlock(self, text: str) -> None:
67    def highlightBlock(self, text: str) -> None:
68        for pattern, fmt, group in self._rules:
69            it = pattern.globalMatch(text)
70            while it.hasNext():
71                m = it.next()
72                start  = m.capturedStart(group)
73                length = m.capturedLength(group)
74                if start >= 0 and length > 0:
75                    self.setFormat(start, length, fmt)
staticMetaObject = PySide6.QtCore.QMetaObject("YamlHighlighter" inherits "QSyntaxHighlighter": )