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));
|
2019-10-13 14:59:12 +00:00
|
|
|
rule.pattern.setPattern("\\b[A-Za-z0-9]+\\b");
|
2017-03-29 10:18:37 +00:00
|
|
|
rule.format = asciiFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2019-10-13 14:59:12 +00:00
|
|
|
commentStartRegularExpression.setPattern("/\\*");
|
|
|
|
commentEndRegularExpression.setPattern("\\*/");
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsciiHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
2018-05-04 07:58:32 +00:00
|
|
|
for (const HighlightingRule &rule : highlightingRules) {
|
2019-10-13 14:59:12 +00:00
|
|
|
QRegularExpression expression(rule.pattern);
|
|
|
|
int index = expression.match(text).capturedStart();
|
2018-03-21 20:32:32 +00:00
|
|
|
while (index >= 0) {
|
2019-10-13 14:59:12 +00:00
|
|
|
int length = expression.match(text).capturedLength();
|
2017-03-29 10:18:37 +00:00
|
|
|
setFormat(index, length, rule.format);
|
2019-10-13 14:59:12 +00:00
|
|
|
index = expression.match(text.mid(index + length)).capturedStart();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
if (previousBlockState() != 1)
|
2019-10-13 14:59:12 +00:00
|
|
|
startIndex = QRegularExpression(commentStartRegularExpression).match(text).capturedStart();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
while (startIndex >= 0) {
|
2019-10-13 14:59:12 +00:00
|
|
|
QRegularExpressionMatch commentEndMatch = QRegularExpression(commentEndRegularExpression).match(text.mid(startIndex));
|
|
|
|
int endIndex = commentEndMatch.capturedStart();
|
2017-03-29 10:18:37 +00:00
|
|
|
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
|
2019-10-13 14:59:12 +00:00
|
|
|
+ commentEndMatch.capturedLength();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
2019-10-13 14:59:12 +00:00
|
|
|
startIndex = QRegularExpression(commentStartRegularExpression).match(text.mid(startIndex + commentLength)).capturedStart();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|