-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJSHighlighter.h
72 lines (66 loc) · 2.5 KB
/
JSHighlighter.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
#ifndef JSHIGHLIGHTER_H
#define JSHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextDocument>
#include <QTextFormat>
#include <QStringList>
#include <QRegExp>
#include <QVector>
#include <QTextCharFormat>
// incomplete example from
// http://www.qtcentre.org/threads/25501-QtScript-script-text-editor-for-Qt-4-4-3-with-Syntax-Highlighter
class JSHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
struct HighlightingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
void SetRule(QString kind, QString pattern, QTextCharFormat format)
{
HighlightingRule rule;
rule.pattern = QRegExp(pattern);
rule.format = format;
highlightingRules.append(rule);
}
JSHighlighter(QTextDocument *parent = 0): QSyntaxHighlighter(parent)
{
QTextCharFormat keywordFormat;
keywordFormat.setForeground(Qt::black);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bvar\\b" << "\\bArray\\b" << "\\bfunction\\b"
<< "\\breturn\\b" << "\\barguments\\b" << "\\bif\\b"
<< "\\belse\\b" << "\\bfor\\b" << "\\bswitch\\b"
<< "\\bcase\\b" << "\\bbreak\\b" << "\\bwhile\\b";
int i = 0;
foreach (const QString &pattern, keywordPatterns) {
SetRule(QString("00_KeyWord_%1").arg(i),pattern,keywordFormat);
++i;
}
// Values
QTextCharFormat valueFormat, classFormat;
valueFormat.setForeground(Qt::blue);
SetRule("03_Values","\\btrue\\b|\\bfalse\\b|\\b[0-9]+\\b",valueFormat);
QTextCharFormat functionFormat;
//functionFormat.setFontItalic(false);
functionFormat.setForeground(Qt::darkBlue);
SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
// Qt Classes
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
SetRule("06_QtClasses","\\bQ[A-Z]+[A-Za-z]+\\b",classFormat);
// Quotation
QTextCharFormat quotationFormat;
quotationFormat.setForeground(Qt::blue);
SetRule("z1_Quotations","\"[^\"]*\"",quotationFormat);
// Single Line Comments
QTextCharFormat singleLineCommentFormat;
singleLineCommentFormat.setForeground(Qt::darkGreen);
SetRule("z2_SingleLineComments","//[^\n]*",singleLineCommentFormat);
}
};
#endif // JSHIGHLIGHTER_H