SILICON
Loading...
Searching...
No Matches
codeSyntaxHighlighter.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <array>
21#include <vector>
22
23#include <QRegularExpression>
24#include <QSyntaxHighlighter>
25#include <QTextCharFormat>
26
28
29class QPalette;
30class QTextDocument;
31
32namespace SILICON::ui {
33
35class CodeSyntaxHighlighter final : public QSyntaxHighlighter {
36public:
37 explicit CodeSyntaxHighlighter(QTextDocument* document);
38
39 void setSyntax(const CodeSyntax* syntax);
40 void setPalette(const QPalette& palette);
41
42protected:
43 void highlightBlock(const QString& text) override;
44
45private:
48 QRegularExpression expression;
49 };
50
53 QRegularExpression expression;
54 };
55
58 QRegularExpression expression;
59 std::vector<CompiledContainedRule> containedRules;
60 };
61
64 QRegularExpression startExpression;
65 QRegularExpression endExpression;
66 QRegularExpression skipExpression;
67 std::vector<CompiledContainedRule> containedRules;
69 };
70
71 struct RegionEnd {
72 qsizetype start;
73 qsizetype end;
74 };
75
76 static constexpr std::size_t STYLE_COUNT = 13;
77
78 [[nodiscard]] const QTextCharFormat& format(CodeSyntaxStyle style) const;
79 [[nodiscard]] static RegionEnd findRegionEnd(const QString& text, qsizetype from,
80 const CompiledRegionRule& region);
81 void applyContainedRules(const QString& text, qsizetype start, qsizetype end,
82 const std::vector<CompiledContainedRule>& rules);
83
84 const CodeSyntax* syntax = nullptr;
85 std::vector<CompiledKeywordGroup> keywordGroups;
86 std::vector<CompiledMatchRule> matchRules;
87 std::vector<CompiledRegionRule> regionRules;
88 std::array<QTextCharFormat, STYLE_COUNT> formats;
89};
90
91} // namespace SILICON::ui
const QTextCharFormat & format(CodeSyntaxStyle style) const
void highlightBlock(const QString &text) override
static RegionEnd findRegionEnd(const QString &text, qsizetype from, const CompiledRegionRule &region)
std::array< QTextCharFormat, STYLE_COUNT > formats
void applyContainedRules(const QString &text, qsizetype start, qsizetype end, const std::vector< CompiledContainedRule > &rules)
std::vector< CompiledMatchRule > matchRules
std::vector< CompiledRegionRule > regionRules
CodeSyntaxHighlighter(QTextDocument *document)
static constexpr std::size_t STYLE_COUNT
void setSyntax(const CodeSyntax *syntax)
std::vector< CompiledKeywordGroup > keywordGroups
void setPalette(const QPalette &palette)