KSeExpr 6.0.0.0
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
14 : QSyntaxHighlighter(parent)
15 , lightness(130)
16{
17 init();
18}
19
21 : QSyntaxHighlighter(edit)
22 , lightness(130)
23{
24 init();
25}
26
27void ExprHighlighter::fixStyle(const QPalette &palette)
28{
29 lightness = palette.color(QPalette::Base).value() < 127 ? 250 : 130;
30 init();
31}
32
34{
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 = QRegularExpression(QStringLiteral("\\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 = QRegularExpression(QStringLiteral("\\$[A-Za-z][A-Za-z0-9]*\\b"));
57 highlightingRules.append(rule);
58
59 singleLineCommentFormat.setForeground(QColor::fromHsv(54, 49, lightness));
60 rule.pattern = QRegularExpression(QStringLiteral("#[^\n]*"));
62 highlightingRules.append(rule);
63}
64
65void ExprHighlighter::highlightBlock(const QString &text)
66{
67 foreach (HighlightingRule rule, highlightingRules) {
68 QRegularExpression expression(rule.pattern);
69 QRegularExpressionMatch match;
70 int index = text.indexOf(expression, 0, &match);
71 while (index >= 0) {
72 int length = match.capturedLength();
73 setFormat(index, length, rule.format);
74 index = text.indexOf(expression, index + length);
75 }
76 }
77 setCurrentBlockState(0);
78}
void fixStyle(const QPalette &palette)
QTextCharFormat numberFormat
ExprHighlighter(QTextDocument *parent)
QTextCharFormat variableFormat
void highlightBlock(const QString &text) override
QVector< HighlightingRule > highlightingRules
QTextCharFormat singleLineCommentFormat