50 #if QT_VERSION >= 0x050000 57 #include "highLighter.hh" 59 Highlighter::Highlighter(QTextDocument *parent)
60 : QSyntaxHighlighter(parent)
65 Highlighter::Highlighter(QTextEdit *parent)
66 : QSyntaxHighlighter(parent)
73 keywordFormat_.setForeground(Qt::darkGreen);
74 keywordFormat_.setFontWeight(QFont::Bold);
76 pluginFormat_.setForeground(Qt::darkBlue);
77 pluginFormat_.setFontWeight(QFont::Bold);
79 functionFormat_.setForeground(Qt::darkYellow);
80 functionFormat_.setFontWeight(QFont::Bold);
82 typeFormat_.setForeground(Qt::darkMagenta);
83 typeFormat_.setFontWeight(QFont::Bold);
85 quotationFormat_.setForeground(Qt::darkRed);
87 listFormat_.setForeground(Qt::darkRed);
89 singleLineCommentFormat_.setForeground(Qt::red);
90 multiLineCommentFormat_.setForeground(Qt::red);
92 commentStartExpression_ = QRegExp(
"/\\*");
93 commentEndExpression_ = QRegExp(
"\\*/");
96 keywordPatterns_ <<
"while" <<
"for" <<
"print" <<
"var" <<
"break" <<
"if";
99 typePatterns_ <<
"int" <<
"Matrix4x4" <<
"QString" <<
"idList" <<
"bool" <<
"Vector" <<
"double";
122 highlightingRules_.clear();
127 foreach (QString pattern, keywordPatterns_) {
128 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
129 rule.format = keywordFormat_;
130 highlightingRules_.append(rule);
134 foreach (QString pattern, pluginPatterns_ ) {
135 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
136 rule.format = pluginFormat_;
137 highlightingRules_.append(rule);
141 foreach (QString pattern, functionPatterns_ ) {
142 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
143 rule.format = functionFormat_;
144 highlightingRules_.append(rule);
148 foreach (QString pattern, typePatterns_ ) {
149 rule.pattern = QRegExp(
"\\b" + pattern +
"\\b" );
150 rule.format = typeFormat_;
151 highlightingRules_.append(rule);
155 rule.pattern = QRegExp(
"//[^\n]*");
156 rule.format = singleLineCommentFormat_;
157 highlightingRules_.append(rule);
160 rule.pattern = QRegExp(
"\".*\"");
161 rule.format = quotationFormat_;
162 highlightingRules_.append(rule);
165 rule.pattern = QRegExp(
"\\[.*\\]");
166 rule.format = listFormat_;
167 highlightingRules_.append(rule);
171 void Highlighter::highlightBlock(
const QString &text)
175 QRegExp expression(rule.pattern);
176 int index = text.indexOf(expression);
178 int length = expression.matchedLength();
179 setFormat(index, length, rule.format);
180 index = text.indexOf(expression, index + length);
183 setCurrentBlockState(0);
186 if (previousBlockState() != 1)
187 startIndex = text.indexOf(commentStartExpression_);
189 while (startIndex >= 0) {
190 int endIndex = text.indexOf(commentEndExpression_, startIndex);
192 if (endIndex == -1) {
193 setCurrentBlockState(1);
194 commentLength = text.length() - startIndex;
196 commentLength = endIndex - startIndex + commentEndExpression_.matchedLength();
198 setFormat(startIndex, commentLength, multiLineCommentFormat_);
199 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.