2017-03-29 10:18:37 +00:00
|
|
|
#include <QtGui>
|
|
|
|
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/HexAsciiHighlighter.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
AsciiHighlighter::AsciiHighlighter(QTextDocument *parent)
|
|
|
|
: QSyntaxHighlighter(parent)
|
|
|
|
{
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
|
|
|
asciiFormat.setForeground(QColor(65, 131, 215));
|
|
|
|
rule.pattern = QRegExp("\\b[A-Za-z0-9]+\\b");
|
|
|
|
rule.format = asciiFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
commentStartExpression = QRegExp("/\\*");
|
|
|
|
commentEndExpression = QRegExp("\\*/");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsciiHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
2018-05-04 07:58:32 +00:00
|
|
|
for (const HighlightingRule &rule : highlightingRules) {
|
2017-03-29 10:18:37 +00:00
|
|
|
QRegExp expression(rule.pattern);
|
|
|
|
int index = expression.indexIn(text);
|
2018-03-21 20:32:32 +00:00
|
|
|
while (index >= 0) {
|
2017-03-29 10:18:37 +00:00
|
|
|
int length = expression.matchedLength();
|
|
|
|
setFormat(index, length, rule.format);
|
|
|
|
index = expression.indexIn(text, index + length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
if (previousBlockState() != 1)
|
|
|
|
startIndex = commentStartExpression.indexIn(text);
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
while (startIndex >= 0) {
|
2017-03-29 10:18:37 +00:00
|
|
|
int endIndex = commentEndExpression.indexIn(text, startIndex);
|
|
|
|
int commentLength;
|
2018-03-21 20:32:32 +00:00
|
|
|
if (endIndex == -1) {
|
2017-03-29 10:18:37 +00:00
|
|
|
setCurrentBlockState(1);
|
|
|
|
commentLength = text.length() - startIndex;
|
2018-03-21 20:32:32 +00:00
|
|
|
} else {
|
2017-03-29 10:18:37 +00:00
|
|
|
commentLength = endIndex - startIndex
|
|
|
|
+ commentEndExpression.matchedLength();
|
|
|
|
}
|
|
|
|
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
|
|
|
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
|
|
|
|
}
|
|
|
|
}
|