Debian Codegraph @

src/librssguard/gui/reusable/jssyntaxhighlighter.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/reusable/jssyntaxhighlighter.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "definitions/definitions.h"
7
8 JsSyntaxHighlighter::JsSyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {
9 HighlightingRule rule;
10
11 m_keywordFormat.setForeground(Qt::GlobalColor::magenta);
12 m_keywordFormat.setFontWeight(QFont::Weight::Bold);
13
14 QStringList keywords = jsKeywords();
15 auto std_keywords = boolinq::from(keywords.begin(), keywords.end())
16 .select([](const QString& kw) {
17 return QSL("\\b%1\\b").arg(kw);
18 })
19 .toStdList();
20
21 keywords = FROM_STD_LIST(QStringList, std_keywords);
22
23 for (const QString& pattern : std::as_const(keywords)) {
24 rule.m_pattern = QRegularExpression(pattern);
25 rule.m_format = m_keywordFormat;
26
27 m_highlightingRules.append(rule);
28 }
29
30 m_classFormat.setFontWeight(QFont::Weight::Bold);
31 m_classFormat.setForeground(Qt::GlobalColor::darkMagenta);
32 rule.m_pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b"));
33 rule.m_format = m_classFormat;
34
35 m_highlightingRules.append(rule);
36
37 m_singleLineCommentFormat.setForeground(Qt::GlobalColor::red);
38 rule.m_pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
39 rule.m_format = m_singleLineCommentFormat;
40
41 m_highlightingRules.append(rule);
42
43 m_multiLineCommentFormat.setForeground(Qt::GlobalColor::red);
44
45 m_quotationFormat.setForeground(Qt::GlobalColor::darkGreen);
46 rule.m_pattern = QRegularExpression(QStringLiteral("\".*\""));
47 rule.m_format = m_quotationFormat;
48
49 m_highlightingRules.append(rule);
50
51 m_functionFormat.setFontItalic(true);
52 m_functionFormat.setForeground(Qt::GlobalColor::green);
53 rule.m_pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()"));
54 rule.m_format = m_functionFormat;
55
56 m_highlightingRules.append(rule);
57
58 m_commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
59 m_commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
60 }
61
62 QStringList JsSyntaxHighlighter::jsKeywords() const {
63 return {QSL("abstract"), QSL("arguments"), QSL("await\\*"), QSL("boolean"), QSL("break"), QSL("byte"),
64 QSL("case"), QSL("catch"), QSL("char"), QSL("class\\*"), QSL("const"), QSL("continue"),
65 QSL("debugger"), QSL("default"), QSL("delete"), QSL("do"), QSL("double"), QSL("else"),
66 QSL("enum\\*"), QSL("eval"), QSL("export\\*"), QSL("extends\\*"), QSL("false"), QSL("final"),
67 QSL("finally"), QSL("float"), QSL("for"), QSL("function"), QSL("goto"), QSL("if"),
68 QSL("implements"), QSL("import\\*"), QSL("in"), QSL("instanceof"), QSL("int"), QSL("interface"),
69 QSL("let\\*"), QSL("long"), QSL("native"), QSL("new"), QSL("null"), QSL("package"),
70 QSL("private"), QSL("protected"), QSL("public"), QSL("return"), QSL("short"), QSL("static"),
71 QSL("super\\*"), QSL("switch"), QSL("synchronized"), QSL("this"), QSL("throw"), QSL("throws"),
72 QSL("transient"), QSL("true"), QSL("try"), QSL("typeof"), QSL("var"), QSL("void"),
73 QSL("volatile"), QSL("while"), QSL("with"), QSL("yield")};
74 }
75
76 void JsSyntaxHighlighter::highlightBlock(const QString& text) {
77 for (const HighlightingRule& rule : std::as_const(m_highlightingRules)) {
78 QRegularExpressionMatchIterator matchIterator = rule.m_pattern.globalMatch(text);
79 while (matchIterator.hasNext()) {
80 QRegularExpressionMatch match = matchIterator.next();
81 setFormat(match.capturedStart(), match.capturedLength(), rule.m_format);
82 }
83 }
84
85 setCurrentBlockState(0);
86
87 int start_index = 0;
88 if (previousBlockState() != 1) {
89 start_index = text.indexOf(m_commentStartExpression);
90 }
91
92 while (start_index >= 0) {
93 QRegularExpressionMatch match = m_commentEndExpression.match(text, start_index);
94 int end_index = match.capturedStart();
95 int comment_length = 0;
96
97 if (end_index == -1) {
98 setCurrentBlockState(1);
99 comment_length = text.length() - start_index;
100 }
101 else {
102 comment_length = end_index - start_index + match.capturedLength();
103 }
104
105 setFormat(start_index, comment_length, m_multiLineCommentFormat);
106 start_index = text.indexOf(m_commentStartExpression, start_index + comment_length);
107 }
108 }