-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinisyntaxhighlighter.cpp
More file actions
39 lines (34 loc) · 1.4 KB
/
inisyntaxhighlighter.cpp
File metadata and controls
39 lines (34 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "inisyntaxhighlighter.h"
INISyntaxHighlighter::INISyntaxHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent) {
}
void INISyntaxHighlighter::highlightBlock(const QString& text) {
QTextCharFormat commentFormat, sectionFormat, keyFormat, valueSpecialFormat;
commentFormat.setForeground(Qt::darkGreen);
sectionFormat.setForeground(Qt::red);
keyFormat.setForeground(Qt::darkCyan);
valueSpecialFormat.setForeground(Qt::blue);
QStringList lines = text.split("\n");
int startIndex = 0, endIndex = 0;
for (int i = 0; i < lines.size(); i++) {
QString line = lines[i];
endIndex = startIndex + line.size();
if (line.trimmed().startsWith("#"))
setFormat(startIndex, endIndex, commentFormat);
else if (line.trimmed().startsWith("["))
setFormat(startIndex, endIndex, sectionFormat);
else {
int mid = line.indexOf('=');
if (mid == -1)
mid = line.size();
setFormat(startIndex, startIndex + mid + 1, keyFormat);
QString key = line.mid(0, mid).trimmed();
QString value = line.mid(mid+1).trimmed();
bool isNum;
value.toDouble(&isNum);
if (isNum || value == "true" || value == "false")
setFormat(startIndex + mid + 1, endIndex, valueSpecialFormat);
}
startIndex = endIndex;
}
}