47 #include "highLighter.hh" 49 Highlighter::Highlighter(QTextDocument *parent)
50 : QSyntaxHighlighter(parent)
55 Highlighter::Highlighter(QTextEdit *parent)
56 : QSyntaxHighlighter(parent)
63 keywordFormat_.setForeground(Qt::darkGreen);
64 keywordFormat_.setFontWeight(QFont::Bold);
66 pluginFormat_.setForeground(Qt::darkBlue);
67 pluginFormat_.setFontWeight(QFont::Bold);
69 functionFormat_.setForeground(Qt::darkYellow);
70 functionFormat_.setFontWeight(QFont::Bold);
72 typeFormat_.setForeground(Qt::darkMagenta);
73 typeFormat_.setFontWeight(QFont::Bold);
75 quotationFormat_.setForeground(Qt::darkRed);
77 listFormat_.setForeground(Qt::darkRed);
79 singleLineCommentFormat_.setForeground(Qt::red);
80 multiLineCommentFormat_.setForeground(Qt::red);
82 commentStartExpression_ = QRegExp(
"/\\*");
83 commentEndExpression_ = QRegExp(
"\\*/");
86 keywordPatterns_ <<
"while" <<
"for" <<
"print" <<
"var" <<
"break" <<
"if";
89 typePatterns_ <<
"int" <<
"Matrix4x4" <<
"QString" <<
"idList" 90 <<
"bool" <<
"Vector" <<
"Vector4" <<
"double";
113 highlightingRules_.clear();
118 foreach (QString pattern, keywordPatterns_) {
119 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
120 rule.format = keywordFormat_;
121 highlightingRules_.append(rule);
125 foreach (QString pattern, pluginPatterns_ ) {
126 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
127 rule.format = pluginFormat_;
128 highlightingRules_.append(rule);
132 foreach (QString pattern, functionPatterns_ ) {
133 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
134 rule.format = functionFormat_;
135 highlightingRules_.append(rule);
139 foreach (QString pattern, typePatterns_ ) {
140 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
141 rule.format = typeFormat_;
142 highlightingRules_.append(rule);
146 rule.pattern = QRegExp(
"//[^\n]*");
147 rule.format = singleLineCommentFormat_;
148 highlightingRules_.append(rule);
151 rule.pattern = QRegExp(
"\".*\"");
152 rule.format = quotationFormat_;
153 highlightingRules_.append(rule);
156 rule.pattern = QRegExp(
"\\[.*\\]");
157 rule.format = listFormat_;
158 highlightingRules_.append(rule);
162 void Highlighter::highlightBlock(
const QString &text)
166 QRegExp expression(rule.pattern);
167 int index = text.indexOf(expression);
169 int length = expression.matchedLength();
170 setFormat(index, length, rule.format);
171 index = text.indexOf(expression, index + length);
174 setCurrentBlockState(0);
177 if (previousBlockState() != 1)
178 startIndex = text.indexOf(commentStartExpression_);
180 while (startIndex >= 0) {
181 int endIndex = text.indexOf(commentEndExpression_, startIndex);
183 if (endIndex == -1) {
184 setCurrentBlockState(1);
185 commentLength = text.length() - startIndex;
187 commentLength = endIndex - startIndex + commentEndExpression_.matchedLength();
189 setFormat(startIndex, commentLength, multiLineCommentFormat_);
190 startIndex = text.indexOf(commentStartExpression_, startIndex + commentLength);
void init()
common initializer function called by the constructors
void update()
Updates the highlighter with the current rule set defined in the patterns.