-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexersel.cpp
363 lines (325 loc) · 8.41 KB
/
lexersel.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
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
#include <iostream>
#include <QtGui>
#include <qsciscintilla.h>
#include <qscilexer.h>
#include <qscilexercpp.h>
#include <qscilexerjava.h>
#include <qscilexerjavascript.h>
#include <qscilexerbash.h>
#include <qscilexermakefile.h>
#include <qscilexercmake.h>
#include <qscilexerpython.h>
#include <qscilexerperl.h>
#include <qscilexercss.h>
#include <qscilexerhtml.h>
#include <qscilexerdiff.h>
#include <qscilexertcl.h>
#include <qscilexertex.h>
#include "asciidoclexer.h"
#include "miblexer.h"
#include "lexersel.h"
#include "globals.h"
QList<LexerData> initFileData()
{
QList<LexerData> list;
list
<< LexerData("*.c;*.h;*.cpp;*.cc", LexerCPP)
<< LexerData("*.java", LexerJava)
<< LexerData("*.js", LexerJavaScript)
<< LexerData("*.sh;*.bash", LexerBash)
<< LexerData("*.mk;Makefile*", LexerMakefile)
<< LexerData("*.pro", LexerCMake)
<< LexerData("*.py", LexerPython)
<< LexerData("*.patch;*.diff;patch.txt", LexerDiff)
<< LexerData("*.tcl", LexerTCL)
<< LexerData("*.pl;*.pm", LexerPerl)
<< LexerData("*.html;*.xhtml", LexerHTML)
<< LexerData("*.css", LexerCSS)
<< LexerData("*.txt", LexerAsciiDoc)
<< LexerData("*.tex", LexerTex)
<< LexerData("*.mib", LexerMib);
return list;
}
QList<LexerData> LexerSelector::lexerInfo(initFileData());
QList<LexerData> initTextData()
{
QList<LexerData> list;
list
<< LexerData("/bin/sh;/bin/bash", LexerBash)
<< LexerData("/bin/perl;/usr/bin/perl", LexerPerl)
<< LexerData("/usr/bin/python", LexerPerl)
<< LexerData("\\documentclass", LexerTex);
return list;
}
QList<LexerData> LexerSelector::lexerTextInfo(initTextData());
QMap<QString,LexerID> initStringToId()
{
QMap<QString,LexerID> map;
#define addMap(id) map[#id] = id
addMap(LexerNone);
addMap(LexerCPP);
addMap(LexerJava);
addMap(LexerJavaScript);
addMap(LexerBash);
addMap(LexerMakefile);
addMap(LexerCMake);
addMap(LexerPython);
addMap(LexerDiff);
addMap(LexerTCL);
addMap(LexerPerl);
addMap(LexerHTML);
addMap(LexerCSS);
addMap(LexerAsciiDoc);
addMap(LexerTex);
addMap(LexerMib);
#undef addMap
return map;
}
QMap<QString,LexerID> LexerSelector::stringToIdMap(initStringToId());
QMap<LexerID,QString> initIdToString()
{
QMap<LexerID,QString> map;
#define addMap(id) map[id] = #id
addMap(LexerNone);
addMap(LexerCPP);
addMap(LexerJava);
addMap(LexerJavaScript);
addMap(LexerBash);
addMap(LexerMakefile);
addMap(LexerCMake);
addMap(LexerPython);
addMap(LexerDiff);
addMap(LexerTCL);
addMap(LexerPerl);
addMap(LexerHTML);
addMap(LexerCSS);
addMap(LexerAsciiDoc);
addMap(LexerTex);
addMap(LexerMib);
#undef addMap
return map;
}
QMap<LexerID,QString> LexerSelector::idToStringMap(initIdToString());
QsciLexer* LexerSelector::getLexerForFile(const QString &fileName,
QString *lineCommentString, QString *blockCommentStartString,
QString *blockCommentMiddleString, QString *blockCommentEndString)
{
LexerID id;
QList<LexerData>::iterator l_iter;
for (l_iter = lexerInfo.begin(); l_iter != lexerInfo.end(); l_iter++) {
LexerData lexerData = *l_iter;
QStringList list = lexerData.pattern.split(";");
QStringList::iterator iter;
for( iter = list.begin(); iter != list.end(); iter++){
QRegExp rx(*iter);
rx.setPatternSyntax(QRegExp::Wildcard);
if (rx.exactMatch(fileName)) {
// qDebug() << __FUNCTION__ << fileName << " matches" << *iter
// << " using lexer" << lexerData.id;
id = lexerData.id;
return getLexerById(id,lineCommentString,
blockCommentStartString,
blockCommentMiddleString,
blockCommentEndString);
}
}
}
return NULL;
}
QsciLexer* LexerSelector::getLexerForText(const QString &text,
QString *lineCommentString, QString *blockCommentStartString,
QString *blockCommentMiddleString, QString *blockCommentEndString)
{
LexerID id;
QList<LexerData>::iterator l_iter;
for (l_iter = lexerTextInfo.begin(); l_iter != lexerTextInfo.end(); l_iter++) {
LexerData lexerData = *l_iter;
QStringList list = lexerData.pattern.split(";");
QStringList::iterator iter;
for( iter = list.begin(); iter != list.end(); iter++){
if (text.contains(*iter)) {
// qDebug() << __FUNCTION__ << text << " matches" << *iter
// << " using lexer" << lexerData.id;
id = lexerData.id;
return getLexerById(id,lineCommentString,
blockCommentStartString,
blockCommentMiddleString,
blockCommentEndString);
}
}
}
return NULL;
}
QsciLexer* LexerSelector::getLexerById(int id,
QString *lineCommentString, QString *blockCommentStartString,
QString *blockCommentMiddleString, QString *blockCommentEndString)
{
QsciLexer *lexer = NULL;
switch (id) {
case LexerCPP:
lexer = new QsciLexerCPP;
break;
case LexerJava:
lexer = new QsciLexerJava;
break;
case LexerJavaScript:
lexer = new QsciLexerJavaScript;
break;
case LexerBash:
lexer = new QsciLexerBash;
break;
case LexerMakefile:
lexer = new QsciLexerMakefile;
break;
case LexerCMake:
lexer = new QsciLexerCMake;
break;
case LexerPython:
lexer = new QsciLexerPython;
break;
case LexerDiff:
lexer = new QsciLexerDiff;
break;
case LexerTCL:
lexer = new QsciLexerTCL;
break;
case LexerPerl:
lexer = new QsciLexerPerl;
break;
case LexerHTML:
lexer = new QsciLexerHTML;
break;
case LexerCSS:
lexer = new QsciLexerCSS;
break;
case LexerAsciiDoc:
lexer = new AsciiDocLexer;
break;
case LexerTex:
lexer = new QsciLexerTeX;
break;
case LexerMib:
lexer = new MibLexer;
default :
break;
}
switch (id) {
case LexerCPP:
case LexerJava:
case LexerJavaScript:
case LexerCSS:
*lineCommentString = "// ";
*blockCommentStartString = "/* ";
*blockCommentMiddleString = "";
*blockCommentEndString = " */";
break;
case LexerBash:
case LexerMakefile:
case LexerPython:
case LexerPerl:
case LexerCMake:
case LexerTCL:
*lineCommentString = "# ";
*blockCommentStartString = "# ";
*blockCommentMiddleString = "# ";
*blockCommentEndString = "";
break;
break;
case LexerHTML:
*lineCommentString = "";
*blockCommentStartString = "<!-- ";
*blockCommentMiddleString = "";
*blockCommentEndString = " -->";
break;
case LexerAsciiDoc:
*lineCommentString = "// ";
*blockCommentStartString = "// ";
*blockCommentMiddleString = "// ";
*blockCommentEndString = "";
break;
case LexerTex:
*lineCommentString = "% ";
*blockCommentStartString = "% ";
*blockCommentMiddleString = "% ";
*blockCommentEndString = "";
break;
case LexerMib:
*lineCommentString = "-- ";
*blockCommentStartString = "-- ";
*blockCommentMiddleString = "-- ";
*blockCommentEndString = "";
break;
default :
break;
}
return lexer;
}
void LexerSelector::saveLexerSettings()
{
QSettings settings(COMPANY_NAME, APPLICATION_NAME+"-Lexers");
QList<LexerData>::iterator iter;
int i;
settings.beginWriteArray("FileLexers");
for (iter = lexerInfo.begin(), i=0;
iter != lexerInfo.end();
iter++, i++) {
LexerData data(*iter);
settings.setArrayIndex(i);
settings.setValue("Pattern", data.pattern);
settings.setValue("Id", lexerIdToString(data.id));
}
settings.endArray();
settings.beginWriteArray("TextLexers");
for (iter = lexerTextInfo.begin(), i=0;
iter != lexerTextInfo.end();
iter++, i++) {
LexerData data(*iter);
settings.setArrayIndex(i);
settings.setValue("Pattern", data.pattern);
settings.setValue("Id", lexerIdToString(data.id));
}
settings.endArray();
}
void LexerSelector::loadLexerSettings()
{
QSettings settings(COMPANY_NAME, APPLICATION_NAME+"-Lexers");
QList<LexerData> list;
int i, size;
size = settings.beginReadArray("FileLexers");
for (i = 0; i < size; i++) {
settings.setArrayIndex(i);
QString pattern = settings.value("Pattern").toString();
LexerID id = lexerStringToId(settings.value("Id").toString());
if (id > LexerNone) {
LexerData data(pattern, id);
list.append(data);
}
}
settings.endArray();
if (size && list.size()) {
lexerInfo = list;
}
list.clear();
size = settings.beginReadArray("TextLexers");
for (i = 0; i < size; i++) {
settings.setArrayIndex(i);
QString pattern = settings.value("Pattern").toString();
LexerID id = (LexerID)lexerStringToId(settings.value("Id").toString());
if (id > LexerNone) {
LexerData data(pattern, id);
list.append(data);
}
}
settings.endArray();
if (size && list.size()) {
lexerTextInfo = list;
}
}
LexerID LexerSelector::lexerStringToId(QString str)
{
return stringToIdMap[str];
}
QString LexerSelector::lexerIdToString(LexerID id)
{
return idToStringMap[id];
}