1 /****************************************************************************
3 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
5 ** This file is part of the example classes of the Qt Toolkit.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 // highlighting rules have been adapted by Uwe Drechsel to match vym syntax
29 #include "highlighter.h"
31 Highlighter::Highlighter(QTextDocument *parent)
32 : QSyntaxHighlighter(parent)
34 HighlightingRule rule;
36 keywordFormat.setForeground(Qt::darkBlue);
37 keywordFormat.setFontWeight(QFont::Bold);
38 QStringList keywordPatterns;
39 keywordPatterns << "\\baddBranch\\b"
40 << "\\baddBranchBefore\\b"
41 << "\\baddMapInsert\\b"
42 << "\\baddMapReplace\\b"
43 << "\\bcolorBranch\\b"
44 << "\\bcolorSubtree\\b"
48 << "\\bdeleteKeepChilds\\b"
49 << "\\bdeleteChilds\\b"
54 << "\\bmoveBranchUp\\b"
55 << "\\bmoveBranchDown\\b"
64 << "\\bselectLastBranch\\b"
65 << "\\bselectLastImage\\b"
66 << "\\bsetFrameType\\b"
67 << "\\bsetFramePenColor\\b"
68 << "\\bsetFrameBrushColor\\b"
69 << "\\bsetFramePadding\\b"
70 << "\\bsetFrameBorderWidth\\b"
71 << "\\bsetHideLinkUnselected\\b"
72 << "\\bsetMapAuthor\\b"
73 << "\\bsetMapComment\\b"
74 << "\\bsetMapBackgroundColor\\b"
75 << "\\bsetMapDefLinkColor\\b"
76 << "\\bsetMapDefLinkStyle\\b"
78 << "\\bsetHideExport\\b"
79 << "\\bsetIncludeImagesHorizontally\\b"
80 << "\\bsetIncludeImagesVertically\\b"
86 << "\\bunscrollChilds\\b"
89 foreach (QString pattern, keywordPatterns) {
90 rule.pattern = QRegExp(pattern);
91 rule.format = keywordFormat;
92 highlightingRules.append(rule);
97 classFormat.setFontWeight(QFont::Bold);
98 classFormat.setForeground(Qt::darkMagenta);
99 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
100 rule.format = classFormat;
101 highlightingRules.append(rule);
104 // Single line comments
105 singleLineCommentFormat.setForeground(Qt::red);
106 rule.pattern = QRegExp("#[^\n]*");
107 rule.format = singleLineCommentFormat;
108 highlightingRules.append(rule);
110 // multiline comments
111 multiLineCommentFormat.setForeground(Qt::red);
112 commentStartExpression = QRegExp("/\\*");
113 commentEndExpression = QRegExp("\\*/");
116 quotationFormat.setForeground(Qt::darkGreen);
117 rule.pattern = QRegExp("\".*\"");
118 rule.format = quotationFormat;
119 highlightingRules.append(rule);
121 QStringList valuePatterns;
122 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
123 foreach (QString pattern, valuePatterns) {
124 rule.pattern = QRegExp(pattern);
125 rule.format = quotationFormat;
126 highlightingRules.append(rule);
133 functionFormat.setFontItalic(true);
134 functionFormat.setForeground(Qt::blue);
135 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
136 rule.format = functionFormat;
137 highlightingRules.append(rule);
142 void Highlighter::highlightBlock(const QString &text)
144 foreach (HighlightingRule rule, highlightingRules) {
145 QRegExp expression(rule.pattern);
146 int index = text.indexOf(expression);
148 int length = expression.matchedLength();
149 setFormat(index, length, rule.format);
150 index = text.indexOf(expression, index + length);
153 setCurrentBlockState(0);
156 if (previousBlockState() != 1)
157 startIndex = text.indexOf(commentStartExpression);
159 while (startIndex >= 0) {
160 int endIndex = text.indexOf(commentEndExpression, startIndex);
162 if (endIndex == -1) {
163 setCurrentBlockState(1);
164 commentLength = text.length() - startIndex;
166 commentLength = endIndex - startIndex
167 + commentEndExpression.matchedLength();
169 setFormat(startIndex, commentLength, multiLineCommentFormat);
170 startIndex = text.indexOf(commentStartExpression,
171 startIndex + commentLength);