QXRD  0.11.16
qxrdhighlighter.cpp
Go to the documentation of this file.
1 #include <QtGui>
2 
3 #include "qxrdhighlighter.h"
4 
5 QxrdHighlighter::QxrdHighlighter(QTextDocument *parent)
6  : QSyntaxHighlighter(parent)
7 {
8  HighlightingRule rule;
9 
10  keywordFormat.setForeground(Qt::darkBlue);
11  keywordFormat.setFontWeight(QFont::Bold);
12  QStringList keywordPatterns;
13  keywordPatterns << "\\break\\b" << "\\bcase\\b" << "\\bcatch\\b"
14  << "\\bcontinue\\b" << "\\bdefault\\b" << "\\bdelete\\b"
15  << "\\bdo\\b" << "\\belse\\b" << "\\bfinally\\b"
16  << "\\bfor\\b" << "\\bfunction\\b" << "\\bif\\b"
17  << "\\bin\\b" << "\\binstanceof\\b" << "\\bnew\\b"
18  << "\\breturn\\b" << "\\bswitch\\b" << "\\bthis\\b"
19  << "\\bthrow\\b" << "\\btry\\b" << "\\btypeof\\b"
20  << "\\bvar\\b" << "\\btypedef\\b" << "\\btypename\\b"
21  << "\\bunion\\b" << "\\bvoid\\b" << "\\bwhile\\b"
22  << "\\bwith\\b"
23  << "\\babstract\\b" << "\\benum\\b" << "\\bint\\b" << "\\bshort\\b"
24  << "\\bboolean\\b" << "\\bexport\\b" << "\\binterface\\b" << "\\bstatic\\b"
25  << "\\bbyte\\b" << "\\bextends\\b" << "\\blong\\b" << "\\bsuper\\b"
26  << "\\bchar\\b" << "\\bfinal\\b" << "\\bnative\\b" << "\\bsynchronized\\b"
27  << "\\bclass\\b" << "\\bfloat\\b" << "\\bpackage\\b" << "\\bthrows\\b"
28  << "\\bconst\\b" << "\\bgoto\\b" << "\\bprivate\\b" << "\\btransient\\b"
29  << "\\bdebugger\\b" << "\\bimplements\\b" << "\\bprotected\\b" << "\\bvolatile\\b"
30  << "\\bdouble\\b" << "\\bimport\\b" << "\\bpublic\\b";
31 
32  foreach (QString pattern, keywordPatterns) {
33  rule.pattern = QRegExp(pattern);
34  rule.format = keywordFormat;
35  highlightingRules.append(rule);
36  }
37 
38  classFormat.setFontWeight(QFont::Bold);
39  classFormat.setForeground(Qt::darkMagenta);
40  rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
41  rule.format = classFormat;
42  highlightingRules.append(rule);
43 
44  singleLineCommentFormat.setForeground(Qt::red);
45  rule.pattern = QRegExp("//[^\n]*");
47  highlightingRules.append(rule);
48 
49  multiLineCommentFormat.setForeground(Qt::red);
50 
51  quotationFormat.setForeground(Qt::darkGreen);
52  rule.pattern = QRegExp("\".*\"");
53  rule.format = quotationFormat;
54  highlightingRules.append(rule);
55 
56  functionFormat.setFontItalic(true);
57  functionFormat.setForeground(Qt::blue);
58  rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
59  rule.format = functionFormat;
60  highlightingRules.append(rule);
61 
62  commentStartExpression = QRegExp("/\\*");
63  commentEndExpression = QRegExp("\\*/");
64 }
65 
66 void QxrdHighlighter::highlightBlock(const QString &text)
67 {
68  foreach (HighlightingRule rule, highlightingRules) {
69  QRegExp expression(rule.pattern);
70  int index = text.indexOf(expression);
71  while (index >= 0) {
72  int length = expression.matchedLength();
73  setFormat(index, length, rule.format);
74  index = text.indexOf(expression, index + length);
75  }
76  }
77  setCurrentBlockState(0);
78 
79  int startIndex = 0;
80  if (previousBlockState() != 1)
81  startIndex = text.indexOf(commentStartExpression);
82 
83  while (startIndex >= 0) {
84  int endIndex = text.indexOf(commentEndExpression, startIndex);
85  int commentLength;
86  if (endIndex == -1) {
87  setCurrentBlockState(1);
88  commentLength = text.length() - startIndex;
89  } else {
90  commentLength = endIndex - startIndex
91  + commentEndExpression.matchedLength();
92  }
93  setFormat(startIndex, commentLength, multiLineCommentFormat);
94  startIndex = text.indexOf(commentStartExpression,
95  startIndex + commentLength);
96  }
97 }
QRegExp commentStartExpression
QVector< HighlightingRule > highlightingRules
void highlightBlock(const QString &text)
QxrdHighlighter(QTextDocument *parent=0)
QTextCharFormat singleLineCommentFormat
QTextCharFormat multiLineCommentFormat
QRegExp commentEndExpression
QTextCharFormat keywordFormat
QTextCharFormat classFormat
QTextCharFormat quotationFormat
QTextCharFormat functionFormat