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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
//===========================================
// Lumina-DE source code
// Copyright (c) 2015, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "syntaxSupport.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QFontDatabase>
#include <QFont>
#include <LUtils.h>
// ================
// SYNTAX FILE CLASS
// ================
QColor SyntaxFile::colorFromOption(QString opt, QSettings *settings){
opt = opt.simplified();
if(opt.startsWith("rgb(")){
QStringList opts = opt.section("(",1,-1).section(")",0,0).split(",");
if(opts.length()!=3){ return QColor(); }
return QColor( opts[0].simplified().toInt(), opts[1].simplified().toInt(), opts[2].simplified().toInt() );
}else if(opt.startsWith("#")){
return QColor(opt);
}else if(opt.startsWith("colors/")){
return QColor(settings->value(opt,"").toString());
}
return QColor();
}
QString SyntaxFile::name(){
if(!metaObj.contains("name")){ return ""; }
return metaObj.value("name").toString();
}
int SyntaxFile::char_limit(){
if(!formatObj.contains("columns_per_line")){ return -1; }
int num = formatObj.value("columns_per_line").toInt();
return num;
}
bool SyntaxFile::highlight_excess_whitespace(){
if(!formatObj.contains("highlight_whitespace_eol")){ return false; }
return formatObj.value("highlight_whitespace_eol").toBool();
}
void SyntaxFile::SetupDocument(QPlainTextEdit* editor){
if(formatObj.contains("line_wrap")){
editor->setLineWrapMode( formatObj.value("line_wrap").toBool() ? QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap);
}
if(formatObj.contains("font_type")){
QString type = formatObj.value("font_type").toString();
QFont font = editor->document()->defaultFont(); // current font
if(type=="monospace"){
font = QFontDatabase::systemFont(QFontDatabase::FixedFont); //get the default fixed-size font for the system
}
font.setStyle(QFont::StyleNormal);
font.setStyleStrategy(QFont::PreferAntialias);
editor->document()->setDefaultFont(font);
}
if(formatObj.contains("tab_width")){
int num = formatObj.value("tab_width").toInt();
if(num<=0){ num = 8; } //UNIX Standard of 8 characters per tab
editor->setTabStopWidth( num * QFontMetrics(editor->document()->defaultFont()).width(" ") );
}
}
bool SyntaxFile::supportsFile(QString file){
if(metaObj.contains("file_suffix")){
return metaObj.value("file_suffix").toArray().contains( file.section("/",-1).section(".",-1) );
}
return false;
}
bool SyntaxFile::LoadFile(QString file, QSettings *settings){
QStringList contents = LUtils::readFile(file);
//Now trim the extra non-JSON off the beginning of the file
while(!contents.isEmpty()){
if(contents[0].startsWith("{")){ break; } //stop here
else{ contents.removeAt(0); }
}
QJsonObject obj = QJsonDocument::fromJson(contents.join("\n").simplified().toLocal8Bit()).object();
if(!obj.contains("meta") || !obj.contains("format") || !obj.contains("rules")){ return false; } //could not get any info
//Save the raw meta/format objects for later
fileLoaded = file;
metaObj = obj.value("meta").toObject();
formatObj = obj.value("format").toObject();
//Now read/save the rules structure
QJsonArray rulesArray = obj.value("rules").toArray();
rules.clear();
//Create the blank/generic text format
for(int i=0; i<rulesArray.count(); i++){
QJsonObject rule = rulesArray[i].toObject();
SyntaxRule tmp;
//First load the rule
if(rule.contains("words")){} //valid option - handled at the end though
else if(rule.contains("regex")){
tmp.pattern = QRegExp(rule.value("regex").toString());
}else if(rule.contains("regex_start") && rule.contains("regex_stop")){
}else{ continue; } //bad rule - missing the actual detection logic
//Now load the appearance logic
if(rule.contains("foreground")){ tmp.format.setForeground( colorFromOption(rule.value("foreground").toString(), settings) ); }
if(rule.contains("background")){ tmp.format.setBackground( colorFromOption(rule.value("background").toString(), settings) ); }
if(rule.contains("font-weight")){
QString wgt = rule.value("font-weight").toString();
if(wgt =="bold"){ tmp.format.setFontWeight(QFont::Bold); }
if(wgt =="light"){ tmp.format.setFontWeight(QFont::Light); }
else{ tmp.format.setFontWeight(QFont::Normal); }
}
//Now save the rule(s) to the list
if(rule.contains("words")){
//special logic - this generates a bunch of rules all at once (one per word)
QJsonArray tmpArr = rule.value("words").toArray();
for(int a=0; a<tmpArr.count(); a++){
tmp.pattern = QRegExp("\\b"+tmpArr[a].toString()+"\\b"); //turn each keyword into a QRegExp and insert the rule
rules << tmp;
}
}else{ rules << tmp; } //just a single rule
}
return true;
}
QStringList Custom_Syntax::availableRules(){
QStringList avail;
avail << "C++";
//avail << "Python";
avail << "Shell";
avail << "reST";
return avail;
}
QStringList Custom_Syntax::knownColors(){
//Note: All these colors should be prefixed with "colors/" when accessing them from the settings file
QStringList avail;
//Standard colors
avail << "keyword" << "altkeyword" << "class" << "text" << "function" << "comment";
//Bracket/parenthesis/brace matching
avail << "bracket-found" << "bracket-missing";
return avail;
}
void Custom_Syntax::SetupDefaultColors(QSettings *settings){
if(!settings->contains("colors/keyword")){settings->setValue("colors/keyword", QColor(Qt::blue).name() ); }
if(!settings->contains("colors/altkeyword")){settings->setValue("colors/altkeyword", QColor(Qt::darkBlue).name() ); }
if(!settings->contains("colors/class")){settings->setValue("colors/class", QColor(Qt::darkRed).name() ); }
if(!settings->contains("colors/text")){settings->setValue("colors/text", QColor(Qt::darkMagenta).name() ); }
if(!settings->contains("colors/function")){settings->setValue("colors/function", QColor(Qt::darkCyan).name() ); }
if(!settings->contains("colors/comment")){settings->setValue("colors/comment", QColor(Qt::darkGreen).name() ); }
if(!settings->contains("colors/bracket-found")){settings->setValue("colors/bracket-found", QColor(Qt::green).name() ); }
if(!settings->contains("colors/bracket-missing")){settings->setValue("colors/bracket-missing", QColor(Qt::red).name() ); }
if(!settings->contains("colors/preprocessor")){settings->setValue("colors/preprocessor", QColor(Qt::darkYellow).name() ); }
}
QString Custom_Syntax::ruleForFile(QString filename){
QString suffix = filename.section(".",-1);
if(suffix=="cpp" || suffix=="hpp" || suffix=="c" || suffix=="h"){ return "C++"; }
//else if(suffix=="py" || suffix=="pyc"){ return "Python"; }
else if(suffix=="sh"){ return "Shell"; }
else if(suffix=="rst"){ return "reST"; }
return "";
}
void Custom_Syntax::loadRules(QString type){
//NOTE: the "multiLineComment
lasttype = type;
rules.clear();
splitrules.clear();
if(type=="C++"){
//Keywords (standard C/C++/Qt definitions)
QStringList keywords;
keywords << "char" << "class" << "const" << "double" << "enum" << "explicit" << "extern" << "float" << "friend" << "inline" \
<< "int" << "long" << "namespace" << "operator" << "private" << "protected" << "public" \
<< "short" << "signals" << "signed" << "slots" << "static" << "struct" << "template" \
<< "typedef" << "typename" << "union" << "unsigned" << "virtual" << "void" << "volatile" \
<< "true" << "false" << "bool";
SyntaxRule rule;
rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) );
rule.format.setFontWeight(QFont::Bold);
for(int i=0; i<keywords.length(); i++){
rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule
rules << rule;
}
//Alternate Keywords (built-in functions)
keywords.clear();
keywords << "for" << "while" << "switch" << "case" << "if" << "else" << "return" << "exit";
rule.format.setForeground( QColor(settings->value("colors/altkeyword").toString()) );
for(int i=0; i<keywords.length(); i++){
rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule
rules << rule;
}
//Class Names
rule.format.setForeground( QColor(settings->value("colors/class").toString()) );
rule.pattern = QRegExp("\\b[A-Za-z0-9_-\\.]+(?=::)\\b");
rules << rule;
//Quotes
rule.format.setForeground( QColor(settings->value("colors/text").toString()) );
rule.format.setFontWeight(QFont::Normal);
rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'");
rules << rule;
//Functions
rule.format.setForeground( QColor(settings->value("colors/function").toString()) );
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
rules << rule;
//Proprocessor commands
rule.format.setForeground( QColor(settings->value("colors/preprocessor").toString()) );
rule.pattern = QRegExp("^[\\s]*#[^\n]*");
rules << rule;
//Comment (single line)
rule.format.setForeground( QColor(settings->value("colors/comment").toString()) );
rule.pattern = QRegExp("//[^\n]*");
rules << rule;
//Comment (multi-line)
SyntaxRuleSplit srule;
srule.format = rule.format; //re-use the single-line comment format
srule.startPattern = QRegExp("/\\*");
srule.endPattern = QRegExp("\\*/");
splitrules << srule;
}else if(type=="Shell"){
//Keywords (standard Shell definitions)
QStringList keywords;
keywords << "alias" << "alloc" << "bg" << "bind" << " bindkey" << "break" \
<< "breaksw"<<"builtins"<<"case"<<"cd"<<"chdir"<<"command"<<"complete"<<"continue"<<"default" \
<<"dirs"<<"do"<<"done"<<"echo"<<"echotc"<<"elif"<<"else"<<"end"<<"endif"<<"endsw"<<"esac"<<"eval" \
<<"exec"<<"exit"<<"export"<<"false"<<"fc"<<"fg"<<"filetest"<<"fi"<<"for"<<"foreach"<<"getopts" \
<<"glob"<<"goto"<<"hash"<<"hashstat"<<"history"<<"hup"<<"if"<<"jobid"<<"jobs"<<"kill"<<"limit" \
<<"local"<<"log"<<"login"<<"logout"<<"ls-F"<<"nice"<<"nohup"<<"notify"<<"onintr"<<"popd" \
<<"printenv"<<"printf"<<"pushd"<<"pwd"<<"read"<<"readonly"<<"rehash"<<"repeat"<<"return" \
<<"sched"<<"set"<<"setenv"<<"settc"<<"setty"<<"setvar"<<"shift"<<"source"<<"stop"<<"suspend" \
<<"switch"<<"telltc"<<"test"<<"then"<<"time"<<"times"<<"trap"<<"true"<<"type"<<"ulimit"<<"umask" \
<<"unalias"<<"uncomplete"<<"unhash"<<"unlimit"<<"unset"<<"unsetenv"<<"until"<<"wait" \
<<"where"<<"which"<<"while";
SyntaxRule rule;
rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) );
rule.format.setFontWeight(QFont::Bold);
for(int i=0; i<keywords.length(); i++){
rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule
rules << rule;
}
//Alternate Keywords (built-in functions)
/*keywords.clear();
keywords << "for" << "while" << "switch" << "case" << "if" << "else" << "return" << "exit";
rule.format.setForeground( QColor(settings->value("colors/altkeyword").toString()) );
for(int i=0; i<keywords.length(); i++){
rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule
rules << rule;
}*/
//Variable Names
rule.format.setForeground( QColor(settings->value("colors/class").toString()) );
rule.pattern = QRegExp("\\$\\{[^\\n\\}]+\\}");
rules << rule;
rule.pattern = QRegExp("\\$[^\\s$]+(?=\\s|$)");
rules << rule;
//Quotes
rule.format.setForeground( QColor(settings->value("colors/text").toString()) );
rule.format.setFontWeight(QFont::Normal);
rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'");
rules << rule;
//Functions
rule.format.setForeground( QColor(settings->value("colors/function").toString()) );
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
rules << rule;
//Proprocessor commands
rule.format.setForeground( QColor(settings->value("colors/preprocessor").toString()) );
rule.pattern = QRegExp("^#![^\n]*");
rules << rule;
//Comment (single line)
rule.format.setForeground( QColor(settings->value("colors/comment").toString()) );
rule.pattern = QRegExp("#[^\n]*");
rules << rule;
//Comment (multi-line)
//SyntaxRuleSplit srule;
//srule.format = rule.format; //re-use the single-line comment format
//srule.startPattern = QRegExp("/\\*");
//srule.endPattern = QRegExp("\\*/");
//splitrules << srule;
}else if(type=="Python"){
//Keywords
QStringList keywords;
keywords << "and" << "as" << "assert" << "break" << "class" << "continue" << "def" << "del" \
<< "elif" << "else" << "except" << "exec" << "finally" << "for" << "from" \
<< "global" << "if" << "import" << "in" << "is" << "lambda" << "not" \
<< "or" << "pass" << "print" << "raise" << "return" << "try" << "while" << "with" << "yield";
SyntaxRule rule;
rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) );
rule.format.setFontWeight(QFont::Bold);
for(int i=0; i<keywords.length(); i++){
rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule
rules << rule;
}
//Class Names
//rule.format.setForeground(Qt::darkMagenta);
//rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
//rules << rule;
//Quotes
rule.format.setForeground( QColor(settings->value("colors/text").toString()) );
rule.format.setFontWeight(QFont::Normal);
rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'");
rules << rule;
//Functions
rule.format.setForeground( QColor(settings->value("colors/function").toString()) );
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
rules << rule;
//Comment (single line)
rule.format.setForeground( QColor(settings->value("colors/comment").toString()) );
rule.pattern = QRegExp("#[^\n]*");
rules << rule;
//Comment (multi-line)
//SyntaxRuleSplit srule;
//srule.format = rule.format; //re-use the single-line comment format
//srule.startPattern = QRegExp("/\\*");
//srule.endPattern = QRegExp("\\*/");
//splitrules << srule;
}else if(type=="reST"){
SyntaxRule rule;
// directives
rule.format.setForeground( QColor(settings->value("colors/class").toString()) );
rule.format.setFontItalic(false);
rule.pattern = QRegExp("(\\s|^):[a-zA-Z0-9 ]*:`[^`]*`");
rules << rule;
// hyperlinks
rule.format.setFontItalic(true);
rule.format.setFontWeight(QFont::Normal);
rule.pattern = QRegExp("`[^\\<]*\\<[^\\>]*\\>`_");
rules << rule;
// Code Sample
rule.format.setFontItalic(false);
rule.format.setFontWeight(QFont::Light);
rule.format.setFontFixedPitch(true);
rule.pattern = QRegExp("\\b`{2}.*`{2}\\b");
rules << rule;
//Quotes
/*rule.format.setForeground( QColor(settings->value("colors/text").toString()) );
rule.format.setFontWeight(QFont::Normal);
rule.pattern = QRegExp( "\"[^\"\\\\]*(\\\\(.|\\n)[^\"\\\\]*)*\"|'[^'\\\\]*(\\\\(.|\\n)[^'\\\\]*)*'");
rules << rule;*/
//TODO
rule = SyntaxRule(); //reset rule
rule.format.setFontWeight( QFont::Bold );
rule.pattern = QRegExp("^\\.\\.\\sTODO\\b");
rules << rule;
rule = SyntaxRule(); //reset rule
rule.format.setFontWeight( QFont::Bold );
rule.pattern = QRegExp("^(\\s*)\\.\\.(\\s*)([a-zA-Z0-9]+)::");
rules << rule;
//Functions
rule = SyntaxRule(); //reset rule
rule.format.setForeground( QColor(settings->value("colors/preprocessor").toString()) );
rule.pattern = QRegExp("^(\\s*)\\.\\.(\\s*)\\b_[a-zA-Z0-9 ]*:(\\s|$)");
rules << rule;
//figures and other properties for them
rule = SyntaxRule(); //reset rule
rule.format.setForeground( QColor(settings->value("colors/keyword").toString()) );
rule.pattern = QRegExp("^(\\s*)\\.\\.\\sfigure::\\s");
rules << rule;
rule = SyntaxRule(); //reset rule
rule.format.setForeground( QColor(settings->value("colors/altkeyword").toString()) );
rule.pattern = QRegExp("^( ){3}:(.)*: ");
rules << rule;
//Code Blocks
SyntaxRuleSplit srule;
srule.format.setBackground( QColor("lightblue") );
srule.startPattern = QRegExp("\\:\\:$");
srule.endPattern = QRegExp("^(?=[^\\s])");
splitrules << srule;
srule.startPattern = QRegExp("^(\\s*)\\.\\.\\scode-block::\\s"); //alternate start string for the same rule
srule.endPattern = QRegExp("^(?=[^\\s])");
splitrules << srule;
//Comment (multi-line)
srule = SyntaxRuleSplit();
srule.format.setForeground( QColor(settings->value("colors/comment").toString()) );
srule.startPattern = QRegExp("^(\\s*)\\.\\.\\s[^_](?![\\w-_\\.]+::(\\s|$))");
srule.endPattern = QRegExp("^(?=([^\\s]|$))");
splitrules << srule;
}
}
|