KSeExpr
4.0.4.0
KSeExprUI
ExprHighlighter.cpp
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2
// SPDX-License-Identifier: LicenseRef-Apache-2.0
3
// SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4
// SPDX-License-Identifier: GPL-3.0-or-later
5
/*
6
* @file ExprHighlighter.cpp
7
* @brief A Qt syntax highlighter for the SeExpr language
8
* @author aselle, amyspark
9
*/
10
11
#include "
ExprHighlighter.h
"
12
13
ExprHighlighter::ExprHighlighter
(QTextDocument *parent)
14
: QSyntaxHighlighter(parent)
15
,
lightness
(130)
16
{
17
init
();
18
}
19
20
ExprHighlighter::ExprHighlighter
(QTextEdit *edit)
21
: QSyntaxHighlighter(edit)
22
,
lightness
(130)
23
{
24
init
();
25
}
26
27
void
ExprHighlighter::fixStyle
(
const
QPalette &palette)
28
{
29
lightness
= palette.color(QPalette::Base).value() < 127 ? 250 : 130;
30
init
();
31
}
32
33
void
ExprHighlighter::init
()
34
{
35
HighlightingRule
rule;
36
highlightingRules
.clear();
37
38
// Operator highlighting, disabled for now
39
// operatorFormat.setForeground(QColor::fromHsv(50,128,lightness));
40
// QStringList operatorPatterns;
41
// operatorPatterns<<"(?:->)|(?:[()\\+-/\\*%\\^:\\?\\[\\]])";
42
// foreach (QString pattern,operatorPatterns){
43
// rule.pattern=QRegExp(pattern);
44
// rule.format=operatorFormat;
45
// highlightingRules.append(rule);
46
//}
47
48
numberFormat
.setForeground(QColor::fromHsv(37, 200,
lightness
));
49
rule.
pattern
= QRegExp(QString::fromLatin1(
"\\b[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)\\b"
));
// \\b?[^\\$][A-Za-z][A-Za-z0-9]*\\b");
50
rule.
format
=
numberFormat
;
51
highlightingRules
.append(rule);
52
53
variableFormat
.setForeground(QColor::fromHsv(200, 153,
lightness
));
54
// variableFormat.setFontWeight(QFont::Bold);
55
rule.
pattern
= QRegExp(QString::fromLatin1(
"\\$[A-Za-z][A-Za-z0-9]*\\b"
));
56
rule.
format
=
variableFormat
;
57
highlightingRules
.append(rule);
58
59
singleLineCommentFormat
.setForeground(QColor::fromHsv(54, 49,
lightness
));
60
rule.
pattern
= QRegExp(QString::fromLatin1(
"#[^\n]*"
));
61
rule.
format
=
singleLineCommentFormat
;
62
highlightingRules
.append(rule);
63
}
64
65
void
ExprHighlighter::highlightBlock
(
const
QString &text)
66
{
67
foreach
(
HighlightingRule
rule,
highlightingRules
) {
68
QRegExp expression(rule.
pattern
);
69
int
index = text.indexOf(expression);
70
while
(index >= 0) {
71
int
length = expression.matchedLength();
72
setFormat(index, length, rule.
format
);
73
index = text.indexOf(expression, index + length);
74
}
75
}
76
setCurrentBlockState(0);
77
}
ExprHighlighter.h
ExprHighlighter::fixStyle
void fixStyle(const QPalette &palette)
Definition
ExprHighlighter.cpp:27
ExprHighlighter::numberFormat
QTextCharFormat numberFormat
Definition
ExprHighlighter.h:28
ExprHighlighter::ExprHighlighter
ExprHighlighter(QTextDocument *parent)
Definition
ExprHighlighter.cpp:13
ExprHighlighter::variableFormat
QTextCharFormat variableFormat
Definition
ExprHighlighter.h:27
ExprHighlighter::lightness
int lightness
Definition
ExprHighlighter.h:31
ExprHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition
ExprHighlighter.cpp:65
ExprHighlighter::init
void init()
Definition
ExprHighlighter.cpp:33
ExprHighlighter::highlightingRules
QVector< HighlightingRule > highlightingRules
Definition
ExprHighlighter.h:25
ExprHighlighter::singleLineCommentFormat
QTextCharFormat singleLineCommentFormat
Definition
ExprHighlighter.h:26
ExprHighlighter::HighlightingRule
Definition
ExprHighlighter.h:21
ExprHighlighter::HighlightingRule::format
QTextCharFormat format
Definition
ExprHighlighter.h:23
ExprHighlighter::HighlightingRule::pattern
QRegExp pattern
Definition
ExprHighlighter.h:22
Generated by
1.14.0