This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlighter.cpp
54 lines (46 loc) · 1.81 KB
/
highlighter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <qstring.h>
#include <QColor>
#include <QFont>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
#include <QTextCharFormat>
#include <QTextDocument>
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
MakeHighlightingRules(QString(""));
}
void Highlighter::MakeHighlightingRules(QString categoryregexp)
{
highlightingRules = QVector<HighlightingRule>();
blackformat = QTextCharFormat();
blackformat.setFontWeight(QFont::Bold);
blueformat = QTextCharFormat();
blueformat.setForeground(QColor(0, 0, 255));
greenformat = QTextCharFormat();
greenformat.setForeground(QColor(0, 128, 0));
greenformat.setFontItalic(true);
categoryformat = QTextCharFormat();
categoryformat.setBackground(QColor(245, 245, 220));
// make sure to append them in the right order
highlightingRules.append({ QRegularExpression(R"((@\d|_|>|#|~|`))"), blueformat });
highlightingRules.append({ QRegularExpression("(/)"), blackformat });
highlightingRules.append({ QRegularExpression("(" + categoryregexp + ")"), categoryformat });
highlightingRules.append({ QRegularExpression(R"((\[.*?\]))"), categoryformat });
highlightingRules.append({ QRegularExpression(R"(^(?:[^ ]+ (?:[^ ]+ )*)?(?:[^_][^ ]*)?(\*.*))"), greenformat });
}
void Highlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule rule : highlightingRules)
{
QRegularExpression expression(rule.pattern);
QRegularExpressionMatchIterator matches = expression.globalMatch(text);
while (matches.hasNext())
{
QRegularExpressionMatch match = matches.next();
setFormat(match.capturedStart(1), match.capturedLength(1), rule.format);
}
}
}