@@ -19,42 +19,39 @@ Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
19
19
<< " \\ bvoid\\ b" << " \\ bvolatile\\ b" << " \\ bforeach\\ b" ;
20
20
foreach (const QString &pattern, keywordPatterns)
21
21
{
22
- rule.pattern = QRegularExpression (pattern);
23
- rule.format = keywordFormat;
24
- syntaxRules.append (rule);
22
+ addPattern (pattern, keywordFormat);
25
23
}
26
24
27
25
// Single line comment format expression
28
26
singleLineCommentFormat.setForeground (Qt::darkGray);
29
- rule.pattern = QRegularExpression (QStringLiteral (" //[^\n ]*" ));
30
- rule.format = singleLineCommentFormat;
31
- syntaxRules.append (rule);
27
+ addPattern (" //[^\n ]*" , singleLineCommentFormat);
32
28
33
29
// Double quotation mark for string
34
30
quotationMark.setForeground (Qt::darkGreen);
35
- rule.pattern = QRegularExpression (QStringLiteral (" \" .*\" " ));
36
- rule.format = quotationMark;
37
- syntaxRules.append (rule);
31
+ addPattern (" \" .*\" " , quotationMark);
38
32
39
33
// Function format expression
40
34
functionFormat.setFontItalic (true );
41
35
functionFormat.setForeground (Qt::darkYellow);
42
- rule.pattern = QRegularExpression (QStringLiteral (" \\ b[a-zA-Z_][a-zA-Z0-9_]*(?=\\ s*\\ ()" ));
43
- rule.format = functionFormat;
44
- syntaxRules.append (rule);
36
+ addPattern (" \\ b[a-zA-Z_][a-zA-Z0-9_]*(?=\\ s*\\ ()" , functionFormat);
45
37
46
38
// Color pattern for parenthesis
47
39
QColor parenthesisColor (" #6495ED" );
48
40
parenthesisFormat.setForeground (parenthesisColor);
49
- rule.pattern = QRegularExpression (QStringLiteral (" [()]" ));
50
- rule.format = parenthesisFormat;
51
- syntaxRules.append (rule);
41
+ addPattern (" [()]" , parenthesisFormat);
52
42
53
43
// Regex for single character format 'a', '\n', etc
54
44
charFormat.setForeground (Qt::darkCyan);
55
- rule.pattern = QRegularExpression (QStringLiteral (" '(\\\\ .|[^'])'" ));
56
- rule.format = charFormat;
57
- syntaxRules.append (rule);
45
+ addPattern (" '(\\\\ .|[^'])'" , charFormat);
46
+ }
47
+
48
+ // Add syntax highlighting patterns
49
+ void Syntax::addPattern (const QString &pattern, const QTextCharFormat &format)
50
+ {
51
+ SyntaxRule rule;
52
+ rule.pattern = QRegularExpression (pattern);
53
+ rule.format = format;
54
+ syntaxRules.append (rule);
58
55
}
59
56
60
57
void Syntax::highlightBlock (const QString &text)
0 commit comments