-
Notifications
You must be signed in to change notification settings - Fork 19
/
qsourcehighliter.h
169 lines (153 loc) · 5 KB
/
qsourcehighliter.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2019-2020 Waqar Ahmed -- <waqar.17a@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef QSOURCEHIGHLITER_H
#define QSOURCEHIGHLITER_H
#include <QSyntaxHighlighter>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QStringView>
#endif
namespace QSourceHighlite {
class QSourceHighliter : public QSyntaxHighlighter
{
public:
enum Themes {
Monokai = 1
};
explicit QSourceHighliter(QTextDocument *doc);
QSourceHighliter(QTextDocument *doc, Themes theme);
//languages
/*********
* When adding a language make sure that its value is a multiple of 2
* This is because we use the next number as comment for that language
* In case the language doesn't support multiline comments in the traditional C++
* sense, leave the next value empty. Otherwise mark the next value as comment for
* that language.
* e.g
* CodeCpp = 200
* CodeCppComment = 201
*/
enum Language {
//languages
CodeCpp = 200,
CodeCppComment = 201,
CodeJs = 202,
CodeJsComment = 203,
CodeC = 204,
CodeCComment = 205,
CodeBash = 206,
CodePHP = 208,
CodePHPComment = 209,
CodeQML = 210,
CodeQMLComment = 211,
CodePython = 212,
CodeRust = 214,
CodeRustComment = 215,
CodeJava = 216,
CodeJavaComment = 217,
CodeCSharp = 218,
CodeCSharpComment = 219,
CodeGo = 220,
CodeGoComment = 221,
CodeV = 222,
CodeVComment = 223,
CodeSQL = 224,
CodeJSON = 226,
CodeXML = 228,
CodeCSS = 230,
CodeCSSComment = 231,
CodeTypeScript = 232,
CodeTypeScriptComment = 233,
CodeYAML = 234,
CodeINI = 236,
CodeVex = 238,
CodeVexComment = 239,
CodeCMake = 240,
CodeMake = 242,
CodeAsm = 244,
CodeLua = 246,
CodeLuaComment = 247
};
Q_ENUM(Language)
enum Token {
CodeBlock,
CodeKeyWord,
CodeString,
CodeComment,
CodeType,
CodeOther,
CodeNumLiteral,
CodeBuiltIn,
};
Q_ENUM(Token)
void setCurrentLanguage(Language language);
Q_REQUIRED_RESULT Language currentLanguage();
void setTheme(Themes theme);
protected:
void highlightBlock(const QString &text) override;
private:
void highlightSyntax(const QString &text);
Q_REQUIRED_RESULT int highlightNumericLiterals(const QString &text, int i);
Q_REQUIRED_RESULT int highlightStringLiterals(const QChar strType, const QString &text, int i);
/**
* @brief returns true if c is octal
* @param c the char being checked
* @returns true if the number is octal, false otherwise
*/
Q_REQUIRED_RESULT static constexpr inline bool isOctal(const char c) {
return (c >= '0' && c <= '7');
}
/**
* @brief returns true if c is hex
* @param c the char being checked
* @returns true if the number is hex, false otherwise
*/
Q_REQUIRED_RESULT static constexpr inline bool isHex(const char c) {
return (
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F')
);
}
void cssHighlighter(const QString &text);
void ymlHighlighter(const QString &text);
void xmlHighlighter(const QString &text);
void makeHighlighter(const QString &text);
void highlightInlineAsmLabels(const QString& text);
void asmHighlighter(const QString& text);
void initFormats();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
static inline QStringView strMidRef(const QString& str, qsizetype position, qsizetype n = -1)
{
return QStringView(str).mid(position, n);
}
#else
static inline QStringRef strMidRef(const QString& str, int position, int n = -1)
{
return str.midRef(position, n);
}
#endif
QHash<Token, QTextCharFormat> _formats;
Language _language;
};
}
#endif // QSOURCEHIGHLITER_H