SILICON
Toggle main menu visibility
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
27
#include <
ui/documents/code/codeSyntax.hpp
>
28
29
class
QPalette;
30
class
QTextDocument;
31
32
namespace
SILICON::ui
{
33
35
class
CodeSyntaxHighlighter
final :
public
QSyntaxHighlighter {
36
public
:
37
explicit
CodeSyntaxHighlighter
(QTextDocument* document);
38
39
void
setSyntax
(
const
CodeSyntax
*
syntax
);
40
void
setPalette
(
const
QPalette& palette);
41
42
protected
:
43
void
highlightBlock
(
const
QString& text)
override
;
44
45
private
:
46
struct
CompiledKeywordGroup
{
47
CodeSyntaxStyle
style
;
48
QRegularExpression
expression
;
49
};
50
51
struct
CompiledContainedRule
{
52
CodeSyntaxStyle
style
;
53
QRegularExpression
expression
;
54
};
55
56
struct
CompiledMatchRule
{
57
CodeSyntaxStyle
style
;
58
QRegularExpression
expression
;
59
std::vector<CompiledContainedRule>
containedRules
;
60
};
61
62
struct
CompiledRegionRule
{
63
CodeSyntaxStyle
style
;
64
QRegularExpression
startExpression
;
65
QRegularExpression
endExpression
;
66
QRegularExpression
skipExpression
;
67
std::vector<CompiledContainedRule>
containedRules
;
68
bool
multiline
;
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
SILICON::ui::CodeSyntaxHighlighter::format
const QTextCharFormat & format(CodeSyntaxStyle style) const
Definition
codeSyntaxHighlighter.cpp:153
SILICON::ui::CodeSyntaxHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition
codeSyntaxHighlighter.cpp:198
SILICON::ui::CodeSyntaxHighlighter::findRegionEnd
static RegionEnd findRegionEnd(const QString &text, qsizetype from, const CompiledRegionRule ®ion)
Definition
codeSyntaxHighlighter.cpp:159
SILICON::ui::CodeSyntaxHighlighter::formats
std::array< QTextCharFormat, STYLE_COUNT > formats
Definition
codeSyntaxHighlighter.hpp:88
SILICON::ui::CodeSyntaxHighlighter::applyContainedRules
void applyContainedRules(const QString &text, qsizetype start, qsizetype end, const std::vector< CompiledContainedRule > &rules)
Definition
codeSyntaxHighlighter.cpp:181
SILICON::ui::CodeSyntaxHighlighter::matchRules
std::vector< CompiledMatchRule > matchRules
Definition
codeSyntaxHighlighter.hpp:86
SILICON::ui::CodeSyntaxHighlighter::regionRules
std::vector< CompiledRegionRule > regionRules
Definition
codeSyntaxHighlighter.hpp:87
SILICON::ui::CodeSyntaxHighlighter::CodeSyntaxHighlighter
CodeSyntaxHighlighter(QTextDocument *document)
Definition
codeSyntaxHighlighter.cpp:53
SILICON::ui::CodeSyntaxHighlighter::STYLE_COUNT
static constexpr std::size_t STYLE_COUNT
Definition
codeSyntaxHighlighter.hpp:76
SILICON::ui::CodeSyntaxHighlighter::setSyntax
void setSyntax(const CodeSyntax *syntax)
Definition
codeSyntaxHighlighter.cpp:58
SILICON::ui::CodeSyntaxHighlighter::keywordGroups
std::vector< CompiledKeywordGroup > keywordGroups
Definition
codeSyntaxHighlighter.hpp:85
SILICON::ui::CodeSyntaxHighlighter::syntax
const CodeSyntax * syntax
Definition
codeSyntaxHighlighter.hpp:84
SILICON::ui::CodeSyntaxHighlighter::setPalette
void setPalette(const QPalette &palette)
Definition
codeSyntaxHighlighter.cpp:118
codeSyntax.hpp
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::CodeSyntaxStyle
CodeSyntaxStyle
Definition
codeSyntax.hpp:26
SILICON::ui::CodeSyntaxHighlighter::CompiledContainedRule
Definition
codeSyntaxHighlighter.hpp:51
SILICON::ui::CodeSyntaxHighlighter::CompiledContainedRule::style
CodeSyntaxStyle style
Definition
codeSyntaxHighlighter.hpp:52
SILICON::ui::CodeSyntaxHighlighter::CompiledContainedRule::expression
QRegularExpression expression
Definition
codeSyntaxHighlighter.hpp:53
SILICON::ui::CodeSyntaxHighlighter::CompiledKeywordGroup
Definition
codeSyntaxHighlighter.hpp:46
SILICON::ui::CodeSyntaxHighlighter::CompiledKeywordGroup::style
CodeSyntaxStyle style
Definition
codeSyntaxHighlighter.hpp:47
SILICON::ui::CodeSyntaxHighlighter::CompiledKeywordGroup::expression
QRegularExpression expression
Definition
codeSyntaxHighlighter.hpp:48
SILICON::ui::CodeSyntaxHighlighter::CompiledMatchRule
Definition
codeSyntaxHighlighter.hpp:56
SILICON::ui::CodeSyntaxHighlighter::CompiledMatchRule::style
CodeSyntaxStyle style
Definition
codeSyntaxHighlighter.hpp:57
SILICON::ui::CodeSyntaxHighlighter::CompiledMatchRule::containedRules
std::vector< CompiledContainedRule > containedRules
Definition
codeSyntaxHighlighter.hpp:59
SILICON::ui::CodeSyntaxHighlighter::CompiledMatchRule::expression
QRegularExpression expression
Definition
codeSyntaxHighlighter.hpp:58
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule
Definition
codeSyntaxHighlighter.hpp:62
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule::style
CodeSyntaxStyle style
Definition
codeSyntaxHighlighter.hpp:63
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule::endExpression
QRegularExpression endExpression
Definition
codeSyntaxHighlighter.hpp:65
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule::startExpression
QRegularExpression startExpression
Definition
codeSyntaxHighlighter.hpp:64
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule::multiline
bool multiline
Definition
codeSyntaxHighlighter.hpp:68
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule::skipExpression
QRegularExpression skipExpression
Definition
codeSyntaxHighlighter.hpp:66
SILICON::ui::CodeSyntaxHighlighter::CompiledRegionRule::containedRules
std::vector< CompiledContainedRule > containedRules
Definition
codeSyntaxHighlighter.hpp:67
SILICON::ui::CodeSyntaxHighlighter::RegionEnd
Definition
codeSyntaxHighlighter.hpp:71
SILICON::ui::CodeSyntaxHighlighter::RegionEnd::start
qsizetype start
Definition
codeSyntaxHighlighter.hpp:72
SILICON::ui::CodeSyntaxHighlighter::RegionEnd::end
qsizetype end
Definition
codeSyntaxHighlighter.hpp:73
SILICON::ui::CodeSyntax
Definition
codeSyntax.hpp:84
src
ui
documents
code
codeSyntaxHighlighter.hpp
Generated by
1.17.0