1 #ifndef JSHIGHLIGHTER_H
2 #define JSHIGHLIGHTER_H
4 #include <QSyntaxHighlighter>
5 #include <QTextDocument>
10 #include <QTextCharFormat>
26 void SetRule(QString kind, QString pattern, QTextCharFormat format)
29 rule.
pattern = QRegExp(pattern);
36 QTextCharFormat keywordFormat;
37 keywordFormat.setForeground(Qt::black);
38 keywordFormat.setFontWeight(QFont::Bold);
39 QStringList keywordPatterns;
40 keywordPatterns <<
"\\bvar\\b" <<
"\\bArray\\b" <<
"\\bfunction\\b"
41 <<
"\\breturn\\b" <<
"\\barguments\\b" <<
"\\bif\\b"
42 <<
"\\belse\\b" <<
"\\bfor\\b" <<
"\\bswitch\\b"
43 <<
"\\bcase\\b" <<
"\\bbreak\\b" <<
"\\bwhile\\b";
45 foreach (
const QString &pattern, keywordPatterns) {
46 SetRule(QString(
"00_KeyWord_%1").arg(i),pattern,keywordFormat);
50 QTextCharFormat valueFormat, classFormat;
51 valueFormat.setForeground(Qt::blue);
52 SetRule(
"03_Values",
"\\btrue\\b|\\bfalse\\b|\\b[0-9]+\\b",valueFormat);
53 QTextCharFormat functionFormat;
55 functionFormat.setForeground(Qt::darkBlue);
56 SetRule(
"04_Functions",
"\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
58 classFormat.setFontWeight(QFont::Bold);
59 classFormat.setForeground(Qt::darkMagenta);
60 SetRule(
"06_QtClasses",
"\\bQ[A-Z]+[A-Za-z]+\\b",classFormat);
62 QTextCharFormat quotationFormat;
63 quotationFormat.setForeground(Qt::blue);
64 SetRule(
"z1_Quotations",
"\"[^\"]*\"",quotationFormat);
66 QTextCharFormat singleLineCommentFormat;
67 singleLineCommentFormat.setForeground(Qt::darkGreen);
68 SetRule(
"z2_SingleLineComments",
"//[^\n]*",singleLineCommentFormat);
72 #endif // JSHIGHLIGHTER_H