aboutsummaryrefslogtreecommitdiff
path: root/desktop-utilities/lumina-textedit/syntaxSupport.cpp
blob: a5b07f5dea782d4efbf703becdcb318f71387cc7 (plain)
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
//===========================================
//  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"

QStringList Custom_Syntax::availableRules(){
  QStringList avail;
    avail << "C++";
    avail << "Python";
    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")){ return; } //already has color info
  settings->setValue("colors/keyword", QColor(Qt::blue).name() );
  settings->setValue("colors/altkeyword", QColor(Qt::darkBlue).name() );
  settings->setValue("colors/class", QColor(Qt::darkRed).name() );
  settings->setValue("colors/text", QColor(Qt::darkMagenta).name() );
  settings->setValue("colors/function", QColor(Qt::darkCyan).name() );
  settings->setValue("colors/comment", QColor(Qt::darkGreen).name() );
  settings->setValue("colors/bracket-found", QColor(Qt::green).name() );
  settings->setValue("colors/bracket-missing", QColor(Qt::red).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=="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" << "friend" << "inline" \
			<< "int" << "long" << "namespace" << "operator" << "private" << "protected" << "public" \
			<< "short" << "signals" << "signed" << "slots" << "static" << "struct" << "template" \
			<< "typedef" << "typename" << "union" << "unsigned" << "virtual" << "void" << "volatile";
	  
    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( QColor(settings->value("colors/class").toString()) );
    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("\".*\"");
    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=="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("\".*\"");
    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"){
    //Keywords
    QStringList keywords;
	keywords << "emphasis" << "strong" << "literal" << "subscript" << "superscript" << "title-reference"; 
    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;
    }
    //Directives
    keywords.clear();
    keywords << "image"  << "figure" << "contents" << "container" << "rubric" << "topic" << "sidebar"  \
	  << "parsed-literal" << "epigraph" << "highlights" << "pull-quote" << "compound" << "table" << "csv-table" \
	  << "list-table" << "raw" << "include"<< "class" << "meta" << "title" << "default-role" << "role";
    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;
    }
    //Reset the font color
    rule.format = QTextCharFormat();
    // Strong Emphasis
    rule.format.setFontItalic(false);
    rule.format.setFontWeight(QFont::Bold);
    rule.pattern = QRegExp("\\b[*]{2}[^*\n]+[*]{2}\\b");
    rules << rule;
    // Emphasis
    rule.format.setFontItalic(true);
    rule.format.setFontWeight(QFont::Normal);
    rule.pattern = QRegExp("\\b[*][^*\n]+[*]\\b");
    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("\".*\"");
    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("\\b[.]{2}[^\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;
  }
}
bgstack15