forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteSyntaxHighlighter.cpp
137 lines (122 loc) · 5.78 KB
/
SQLiteSyntaxHighlighter.cpp
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
#include "SQLiteSyntaxHighlighter.h"
#include "PreferencesDialog.h"
SQLiteSyntaxHighlighter::SQLiteSyntaxHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
HighlightingRule rule;
tableFormat = createFormat("table");
keywordFormat = createFormat("keyword");
singleLineCommentFormat = createFormat("comment");
identifierFormat = createFormat("identifier");
stringFormat = createFormat("string");
functionFormat = createFormat("function");
// Keywords
QStringList keywordPatterns;
keywordPatterns
<< "ABORT" << "ACTION" << "ADD" << "AFTER" << "ALL"
<< "ALTER" << "ANALYZE" << "AND" << "AS" << "ASC"
<< "ATTACH" << "AUTOINCREMENT" << "BEFORE" << "BEGIN" << "BETWEEN"
<< "BY" << "CASCADE" << "CASE" << "CAST" << "CHECK"
<< "COLLATE" << "COLUMN" << "COMMIT" << "CONFLICT" << "CONSTRAINT"
<< "CREATE" << "CROSS" << "CURRENT_DATE" << "CURRENT_TIME" << "CURRENT_TIMESTAMP"
<< "DATABASE" << "DEFAULT" << "DEFERRABLE" << "DEFERRED" << "DELETE"
<< "DESC" << "DETACH" << "DISTINCT" << "DROP" << "EACH"
<< "ELSE" << "END" << "ESCAPE" << "EXCEPT" << "EXCLUSIVE"
<< "EXISTS" << "EXPLAIN" << "FAIL" << "FOR" << "FOREIGN"
<< "FROM" << "FULL" << "GLOB" << "GROUP" << "HAVING"
<< "IF" << "IGNORE" << "IMMEDIATE" << "IN" << "INDEX"
<< "INDEXED" << "INITIALLY" << "INNER" << "INSERT" << "INSTEAD"
<< "INTERSECT" << "INTO" << "IS" << "ISNULL" << "JOIN"
<< "KEY" << "LEFT" << "LIKE" << "LIMIT" << "MATCH"
<< "NATURAL" << "NO" << "NOT" << "NOTNULL" << "NULL"
<< "OF" << "OFFSET" << "ON" << "OR" << "ORDER"
<< "OUTER" << "PLAN" << "PRAGMA" << "PRIMARY" << "QUERY"
<< "RAISE" << "REFERENCES" << "REGEXP" << "REINDEX" << "RELEASE"
<< "RENAME" << "REPLACE" << "RESTRICT" << "RIGHT" << "ROLLBACK"
<< "ROW" << "SAVEPOINT" << "SELECT" << "SET" << "TABLE"
<< "TEMP" << "TEMPORARY" << "THEN" << "TO" << "TRANSACTION"
<< "TRIGGER" << "UNION" << "UNIQUE" << "UPDATE" << "USING"
<< "VACUUM" << "VALUES" << "VIEW" << "VIRTUAL" << "WHEN"
<< "WHERE";
rule.format = keywordFormat;
foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegExp(QString("\\b%1\\b").arg(pattern));
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
highlightingRules.append(rule);
}
// Functions
QStringList functionPatterns;
functionPatterns
// Core functions
<< "ABS" << "CHANGES" << "CHAR" << "COALESCE" << "GLOB"
<< "IFNULL" << "INSTR" << "HEX" << "LAST_INSERT_ROW" << "LENGTH"
<< "LIKE" << "LOAD_EXTENSION" << "LOWER" << "LTRIM" << "MAX"
<< "MIN" << "NULLIF" << "QUOTE" << "RANDOM" << "RANDOMBLOB"
<< "REPLACE" << "ROUND" << "RTRIM" << "SOUNDEX" << "SQLITE_COMPILEOPTION_GET"
<< "SQLITE_COMPILEOPTION_USED" << "SQLITE_SOURCE_ID" << "SQLITE_VERSION" << "SUBSTR" << "TOTAL_CHANGES"
<< "TRIM" << "TYPEOF" << "UNICODE" << "UPPER" << "ZEROBLOB"
// Date and time functions
<< "DATE" << "TIME" << "DATETIME" << "JULIANDAY" << "STRFTIME"
// Aggregate functions
<< "AVG" << "COUNT" << "GROUP_CONCAT" << "MAX" << "MIN"
<< "SUM" << "TOTAL";
rule.format = functionFormat;
foreach(const QString& pattern, functionPatterns)
{
rule.pattern = QRegExp(QString("\\b%1\\b").arg(pattern));
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
highlightingRules.append(rule);
}
// single line comment
rule.pattern = QRegExp("--[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
// identifiers
rule.pattern = QRegExp("\"[^\"]*\""); // ""
rule.format = identifierFormat;
highlightingRules.append(rule);
rule.pattern = QRegExp("\\[[^\\]]*\\]"); // []
highlightingRules.append(rule);
rule.pattern = QRegExp("`[^`]*`"); // ``
highlightingRules.append(rule);
// string
rule.pattern = QRegExp("'[^']*'"); // ''
rule.format = stringFormat;
highlightingRules.append(rule);
}
void SQLiteSyntaxHighlighter::setTableNames(const QStringList &tablenames)
{
tableNameRules.clear();
foreach(const QString& tblname, tablenames) {
HighlightingRule rule;
rule.pattern = QRegExp(tblname);
rule.format = tableFormat;
tableNameRules.append(rule);
}
}
void SQLiteSyntaxHighlighter::highlightBlockVector(const QString& text, const QVector<HighlightingRule>& vec)
{
foreach (const HighlightingRule &rule, vec) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
}
void SQLiteSyntaxHighlighter::highlightBlock(const QString &text)
{
highlightBlockVector(text, tableNameRules);
highlightBlockVector(text, highlightingRules);
}
QTextCharFormat SQLiteSyntaxHighlighter::createFormat(const QString& settings_name)
{
QTextCharFormat format;
format.setForeground(QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", settings_name + "_colour").toString()));
format.setFontWeight(PreferencesDialog::getSettingsValue("syntaxhighlighter", settings_name + "_bold").toBool() ? QFont::Bold : QFont::Normal);
format.setFontItalic(PreferencesDialog::getSettingsValue("syntaxhighlighter", settings_name + "_italic").toBool());
format.setFontUnderline(PreferencesDialog::getSettingsValue("syntaxhighlighter", settings_name + "_underline").toBool());
return format;
}