-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntaxhighlighter.cpp
141 lines (119 loc) · 3.97 KB
/
syntaxhighlighter.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "syntaxhighlighter.h"
#include <QElapsedTimer>
SyntaxHighlighter::SyntaxHighlighter(QTextDocument* document): QSyntaxHighlighter(document)
{
languageSet = false;
searchExpression = QRegExp("");
}
void SyntaxHighlighter::load(AssetManager *am, QString lan)
{
assetManager = am;
ruleSet = am->getLanguageSupportRuleSet(lan);
if(ruleSet != NULL)
{
languageSet = true;
autoCompleteSuggestions += ruleSet->getConstantKeywords();
}
}
bool SyntaxHighlighter::isLanguageSet()
{
return languageSet;
}
void SyntaxHighlighter::setSyntaxHighlightingRules(SyntaxHighlightingRuleSet * in)
{
ruleSet = in;
if(in != NULL)
languageSet = true;
else
languageSet = false;
}
SyntaxHighlightingRuleSet * SyntaxHighlighter::getRuleSet() const
{
return ruleSet;
}
void SyntaxHighlighter::highlightMatch(const QString &text, QRegExp &exp)
{
searchExpression = exp;
this->rehighlight();
}
void SyntaxHighlighter::highlightBlock(const QString &text)
{
if(languageSet)
{
// stops syntax highlight code running when searching for reg exp query
// search line for any autocomplete suggestions
autoCompleteSuggestions += searchInputForAutocompleteRules(text);
foreach (const SyntaxHighlightingRuleSet::HighlightingRule rule, ruleSet->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 startIndex = 0;
if (previousBlockState() != 1)
startIndex = ruleSet->commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = ruleSet->commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ ruleSet->commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, ruleSet->multiLineCommentFormat);
startIndex = ruleSet->commentStartExpression.indexIn(text, startIndex + commentLength);
}
// if the search box is populated with an expression
if(searchExpression.pattern() != "")
{
highlightSearchExpression(searchExpression, text);
}
}
}
void SyntaxHighlighter::highlightSearchExpression(QRegExp expr, const QString &text)
{
QTextCharFormat fmt;
fmt.setBackground(QColor(ruleSet->searchHighlightColor));
fmt.setForeground(QColor(ruleSet->searchHighlightColorForeground));
if(expr.pattern() != "")
{
int index = expr.indexIn(text);
while(index >= 0)
{
int length = expr.matchedLength();
this->setFormat(index, length, fmt);
index = expr.indexIn(text, index+length);
}
}
}
QStringList SyntaxHighlighter::searchInputForAutocompleteRules(const QString &text)
{
QStringList suggestions;
QRegExp rx(ruleSet->autocorrectFormat);
QRegExp rxTrimmer(ruleSet->autocorrectTrimFormat);
int pos = rx.indexIn(text);
if(pos != -1)
{
QStringList suggestionsUnTrimmed = rx.capturedTexts();
for(int i = 0; i < suggestionsUnTrimmed.length(); i++)
{
int pos2 = rxTrimmer.indexIn(suggestionsUnTrimmed[i]);
if(pos2 != -1 && !autoCompleteSuggestions.contains(rxTrimmer.capturedTexts()[0]))
suggestions << rxTrimmer.capturedTexts()[0];
}
}
return suggestions;
}
QStringList & SyntaxHighlighter::getAutoCompleteRules()
{
return autoCompleteSuggestions;
}
SyntaxHighlighter::~SyntaxHighlighter()
{
}