-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpphighlighter.cpp
94 lines (82 loc) · 3.82 KB
/
cpphighlighter.cpp
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
/***************************************************************************
* Copyright (C) 2016 by Renaud Guezennec *
* http://www.rolisteam.org/contact *
* *
* rolisteam is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "cpphighlighter.h"
CppHighLighter::CppHighLighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkMagenta);
QStringList keywordPatterns;
keywordPatterns << "\\bif\\b" << "\\belse\\b" << "\\breturn\\b"<< "\\bwhile\\b" << "\\bforeach\\b" << "\\bfor\\b" << "\\bvoid\\b"<< "\\bint\\b"<< "\\bfloat\\b"<< "\\bdouble\\b"
<< "\\bqint8\\b"
<< "\\bquint8\\b"
<< "\\bqint16\\b"
<< "\\bquint16\\b"
<< "\\bqint32\\b"
<< "\\bquint32\\b"
<< "\\bqint64\\b"
<< "\\bquint64\\b"
<< "\\bqreal\\b"
<< "\\bbool\\b"
<< "\\bconst\\b";
foreach (const QString &pattern, keywordPatterns)
{
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
propertyFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("Q[A-Z][A-z]+");
rule.format = propertyFormat;
highlightingRules.append(rule);
lookupFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\\b[0-9]+\\b");
rule.format = lookupFormat;
highlightingRules.append(rule);
quotationFormat.setForeground(Qt::green);
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
rule.pattern = QRegExp("'.*'");
rule.format = quotationFormat;
highlightingRules.append(rule);
itemFormat.setForeground(QColor(Qt::darkBlue));
rule.pattern = QRegExp("^#[A-z]+\\b");
rule.format = itemFormat;
highlightingRules.append(rule);
cppObjectFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("\\b[A-z]+::");
rule.format = cppObjectFormat;
highlightingRules.append(rule);
}
void CppHighLighter::highlightBlock(const QString &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);
}