Skip to content

Commit 53aafe5

Browse files
committed
[Refactor] Organize syntax highlighting rules into separate initialization functions for better readability and maintainability
1 parent b12e995 commit 53aafe5

File tree

1 file changed

+89
-43
lines changed

1 file changed

+89
-43
lines changed

src/Syntax.cpp

Lines changed: 89 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,95 @@
11
#include "Syntax.h"
2+
#include <QColor>
3+
#include <QFont>
24

3-
Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
5+
Syntax::Syntax(QTextDocument *parent)
6+
: QSyntaxHighlighter(parent)
47
{
5-
keywordFormat.setForeground(Qt::blue);
6-
keywordFormat.setFontWeight(QFont::Bold);
7-
QStringList keywordPatterns;
8-
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
9-
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
10-
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
11-
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
12-
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
13-
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
14-
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
15-
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
16-
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
17-
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bforeach\\b";
18-
foreach (const QString &pattern, keywordPatterns)
19-
{
20-
addPattern(pattern, keywordFormat);
21-
}
8+
initKeywordRules();
9+
initFunctionRules();
10+
initParenthesisRules();
11+
initCharRules();
2212

23-
// Single line comment format expression
24-
singleLineCommentFormat.setForeground(Qt::darkGray);
25-
addPattern("//[^\n]*", singleLineCommentFormat);
13+
// Keep these two calls are at the end
14+
// to correctly apply the rules for strings and comments
15+
initCommentRules();
16+
initQuotationRules();
17+
}
18+
19+
void Syntax::initKeywordRules()
20+
{
21+
// Keyword format
22+
keywordFormat.setForeground(Qt::blue);
23+
keywordFormat.setFontWeight(QFont::Bold);
24+
25+
QStringList keywordPatterns = {
26+
"\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
27+
"\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b",
28+
"\\bfriend\\b", "\\binline\\b", "\\bint\\b",
29+
"\\blong\\b", "\\bnamespace\\b", "\\boperator\\b",
30+
"\\bprivate\\b", "\\bprotected\\b", "\\bpublic\\b",
31+
"\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b",
32+
"\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b",
33+
"\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b",
34+
"\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b",
35+
"\\bvoid\\b", "\\bvolatile\\b", "\\bforeach\\b"};
36+
37+
for (const QString &pattern : keywordPatterns)
38+
{
39+
addPattern(pattern, keywordFormat);
40+
}
2641

27-
// Double quotation mark for string
28-
quotationMark.setForeground(Qt::darkGreen);
29-
addPattern("\".*\"", quotationMark);
42+
iterationFormat.setForeground(Qt::darkMagenta);
43+
iterationFormat.setFontWeight(QFont::Bold);
44+
QStringList iterationPatterns = {
45+
"\\bfor\\b", "\\bwhile\\b", "\\bdo\\b", "\\bif\\b", "\\belse\\b"};
46+
for (const QString &pattern : iterationPatterns)
47+
{
48+
addPattern(pattern, iterationFormat);
49+
}
50+
}
3051

31-
// Function format expression
32-
functionFormat.setFontItalic(true);
33-
functionFormat.setForeground(Qt::darkYellow);
34-
addPattern("\\b[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()", functionFormat);
52+
void Syntax::initCommentRules()
53+
{
54+
// Single line comment format expression
55+
singleLineCommentFormat.setForeground(Qt::darkGray);
56+
addPattern("//[^\n]*", singleLineCommentFormat);
3557

36-
// Color pattern for parenthesis
37-
QColor parenthesisColor("#6495ED");
38-
parenthesisFormat.setForeground(parenthesisColor);
39-
addPattern("[()]", parenthesisFormat);
58+
// TO-DO: Add multi-line comment support
59+
}
4060

41-
// Regex for single character format 'a', '\n', etc
42-
charFormat.setForeground(Qt::darkCyan);
43-
addPattern("'(\\\\.|[^'])'", charFormat);
61+
void Syntax::initQuotationRules()
62+
{
63+
// Double quotation mark for string
64+
quotationMark.setForeground(Qt::darkGreen);
65+
addPattern("\"(\\\\.|[^\"\\\\])*\"", quotationMark);
66+
67+
// TO-DO: Add single quotation mark for character
68+
}
69+
70+
void Syntax::initFunctionRules()
71+
{
72+
// Function format expression
73+
functionFormat.setFontItalic(true);
74+
functionFormat.setForeground(Qt::darkYellow);
75+
addPattern("\\b(?!for\\b|if\\b|else\\b|while\\b|do\\b)[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()", functionFormat);
76+
}
77+
78+
void Syntax::initParenthesisRules()
79+
{
80+
// Color pattern for parenthesis
81+
QColor parenthesisColor("#6495ED");
82+
parenthesisFormat.setForeground(parenthesisColor);
83+
addPattern("[()]", parenthesisFormat);
84+
}
85+
86+
// Regex for single character format 'a', '\n', etc.
87+
void Syntax::initCharRules()
88+
{
89+
charFormat.setForeground(Qt::darkCyan);
90+
addPattern("'(\\\\.|[^'])'", charFormat);
4491
}
4592

46-
// Add syntax highlighting patterns
4793
void Syntax::addPattern(const QString &pattern, const QTextCharFormat &format)
4894
{
4995
SyntaxRule rule;
@@ -54,13 +100,13 @@ void Syntax::addPattern(const QString &pattern, const QTextCharFormat &format)
54100

55101
void Syntax::highlightBlock(const QString &text)
56102
{
57-
foreach (const SyntaxRule &rule, syntaxRules)
58-
{
59-
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
60-
while (matchIterator.hasNext())
103+
for (const SyntaxRule &rule : syntaxRules)
61104
{
62-
QRegularExpressionMatch match = matchIterator.next();
63-
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
105+
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
106+
while (matchIterator.hasNext())
107+
{
108+
QRegularExpressionMatch match = matchIterator.next();
109+
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
110+
}
64111
}
65-
}
66112
}

0 commit comments

Comments
 (0)