Debian Codegraph @

src/internal/QGLSLHighlighter.cpp

raw symbols SCIP
1 // QCodeEditor
2 #include <QGLSLHighlighter>
3 #include <QLanguage>
4 #include <QSyntaxStyle>
5
6 // Qt
7 #include <QFile>
8 #include <QDebug>
9
10 QGLSLHighlighter::QGLSLHighlighter(QTextDocument* document) :
11 QStyleSyntaxHighlighter(document),
12 m_highlightRules (),
13 m_includePattern (QRegularExpression(R"(#include\s+([<"][a-zA-Z0-9*._]+[">]))")),
14 m_functionPattern (QRegularExpression(R"(\b([A-Za-z0-9_]+(?:\s+|::))*([A-Za-z0-9_]+)(?=\())")),
15 m_defTypePattern (QRegularExpression(R"(\b([A-Za-z0-9_]+)\s+[A-Za-z]{1}[A-Za-z0-9_]+\s*[;=])")),
16 m_commentStartPattern(QRegularExpression(R"(/\*)")),
17 m_commentEndPattern (QRegularExpression(R"(\*/)"))
18 {
19 Q_INIT_RESOURCE(qcodeeditor_resources);
20 QFile fl(":/languages/glsl.xml");
21
22 if (!fl.open(QIODevice::ReadOnly))
23 {
24 return;
25 }
26
27 QLanguage language(&fl);
28
29 if (!language.isLoaded())
30 {
31 return;
32 }
33
34 auto keys = language.keys();
35 for (auto&& key : keys)
36 {
37 auto names = language.names(key);
38 for (auto&& name : names)
39 {
40 m_highlightRules.append({
41 QRegularExpression(QString(R"(\b%1\b)").arg(name)),
42 key
43 });
44 }
45 }
46
47 // Following rules has higher priority to display
48 // than language specific keys
49 // So they must be applied at last.
50 // Numbers
51 m_highlightRules.append({
52 QRegularExpression(R"(\b(0b|0x){0,1}[\d.']+\b)"),
53 "Number"
54 });
55
56 // Define
57 m_highlightRules.append({
58 QRegularExpression(R"(#[a-zA-Z_]+)"),
59 "Preprocessor"
60 });
61
62 // Single line
63 m_highlightRules.append({
64 QRegularExpression("//[^\n]*"),
65 "Comment"
66 });
67 }
68
69 void QGLSLHighlighter::highlightBlock(const QString& text)
70 {
71
72 {
73 auto matchIterator = m_includePattern.globalMatch(text);
74
75 while (matchIterator.hasNext())
76 {
77 auto match = matchIterator.next();
78
79 setFormat(
80 match.capturedStart(),
81 match.capturedLength(),
82 syntaxStyle()->getFormat("Preprocessor")
83 );
84
85 setFormat(
86 match.capturedStart(1),
87 match.capturedLength(1),
88 syntaxStyle()->getFormat("String")
89 );
90 }
91 }
92 // Checking for function
93 {
94 auto matchIterator = m_functionPattern.globalMatch(text);
95
96 while (matchIterator.hasNext())
97 {
98 auto match = matchIterator.next();
99
100 setFormat(
101 match.capturedStart(),
102 match.capturedLength(),
103 syntaxStyle()->getFormat("Type")
104 );
105
106 setFormat(
107 match.capturedStart(2),
108 match.capturedLength(2),
109 syntaxStyle()->getFormat("Function")
110 );
111 }
112 }
113
114 for (auto& rule : m_highlightRules)
115 {
116 auto matchIterator = rule.pattern.globalMatch(text);
117
118 while (matchIterator.hasNext())
119 {
120 auto match = matchIterator.next();
121
122 setFormat(
123 match.capturedStart(),
124 match.capturedLength(),
125 syntaxStyle()->getFormat(rule.formatName)
126 );
127 }
128 }
129
130 setCurrentBlockState(0);
131
132 int startIndex = 0;
133 if (previousBlockState() != 1)
134 {
135 startIndex = text.indexOf(m_commentStartPattern);
136 }
137
138 while (startIndex >= 0)
139 {
140 auto match = m_commentEndPattern.match(text, startIndex);
141
142 int endIndex = match.capturedStart();
143 int commentLength = 0;
144
145 if (endIndex == -1)
146 {
147 setCurrentBlockState(1);
148 commentLength = text.length() - startIndex;
149 }
150 else
151 {
152 commentLength = endIndex - startIndex + match.capturedLength();
153 }
154
155 setFormat(
156 startIndex,
157 commentLength,
158 syntaxStyle()->getFormat("Comment")
159 );
160 startIndex = text.indexOf(m_commentStartPattern, startIndex + commentLength);
161 }
162 }