Files
qad/piqt_utils/piqt_highlighter.cpp

71 lines
2.1 KiB
C++

#include "piqt_highlighter.h"
ConfigHighlighter::ConfigHighlighter(QTextDocument * parent): QSyntaxHighlighter(parent) {
HighlightingRule rule;
valueNameFormat.setForeground(QColor(0, 64, 154));
rule.pattern = QRegExp("[^=]"); //"\\b[A-Za-z0-9_]+(?=\\()");
rule.format = valueNameFormat;
highlightingRules.append(rule);
valueFormat.setForeground(QColor(192, 0, 0));
rule.pattern = QRegExp("=[^\n]*");
rule.format = valueFormat;
highlightingRules.append(rule);
equalFormat.setFontWeight(QFont::Bold);
equalFormat.setForeground(QColor(96, 126, 0));
rule.pattern = QRegExp("=");
rule.format = equalFormat;
highlightingRules.append(rule);
sectionFormat.setFontWeight(QFont::Bold);
sectionFormat.setForeground(QColor(0, 32, 64));
rule.pattern = QRegExp("\\[.*\\]");
rule.format = sectionFormat;
highlightingRules.append(rule);
//substFormat.setFontWeight(QFont::Bold);
substFormat.setForeground(QColor(192, 0, 192));
rule.pattern = QRegExp("\\$\\{.*\\}+");
rule.pattern.setMinimal(true);
rule.format = substFormat;
highlightingRules.append(rule);
rule.pattern = QRegExp("\\$\\{[^\\{]*\\}+");
highlightingRules.append(rule);
singleLineCommentFormat.setFontItalic(true);
singleLineCommentFormat.setForeground(QColor(128, 128, 128));
rule.pattern = QRegExp("#[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
spaceFormat.setForeground(QColor(210, 210, 210));
//commentStartExpression = QRegExp("/\\*");
//commentEndExpression = QRegExp("\\*/");
}
void ConfigHighlighter::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);
QRegExp expression = QRegExp("[ |\t]");
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, spaceFormat);
index = expression.indexIn(text, index + length);
}
}