-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlighter.cpp
More file actions
120 lines (98 loc) · 3.32 KB
/
highlighter.cpp
File metadata and controls
120 lines (98 loc) · 3.32 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <QtGui>
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
QFile file(QString(":/keyword.txt"));
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qDebug()<<"Cannot read file keyword.txt.";
return ;
}
QTextStream in(&file);
QString keywords = in.readAll();
QStringList keylist=keywords.split(",");
foreach(QString key,keylist)
{
key = key.trimmed();
if(!key.isEmpty())
{
key=QString("\\b(%1)\\b").arg(key);
keywordPatterns.append(key);
}
}
foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
singleLineCommentFormat.setForeground(Qt::darkGreen);
//rule.pattern = QRegExp(QString("--[^\n]*"));
rule.pattern = QRegExp(QString("--.*"));
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::darkGreen);
quotationFormat.setForeground(Qt::red);
//rule.pattern = QRegExp(QString("\'.*\'"));
//rule.format = quotationFormat;
//highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
quotationExp = QRegExp("\'");
}
void Highlighter::highlightBlock(const QString &text)
{
//qDebug()<<text;
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
int qe = 0;
if(previousBlockState()!=1){
qe = quotationExp.indexIn(text);
}
QChar c;
int qs = 0;
bool begin = true;
int len = text.length();
while(qe<len){
c = text.at(qe);
if(c == QChar('\'')){
if(begin){
qs = qe;
begin = false;
}
else{
setFormat(qs,qe-qs+1,quotationFormat);
begin = true;
}
}
qe ++;
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}