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 << "\\baddMapCenter\\b"
42 << "\\baddMapInsert\\b"
43 << "\\baddMapReplace\\b"
44 << "\\bcolorBranch\\b"
45 << "\\bcolorSubtree\\b"
49 << "\\bdeleteKeepChilds\\b"
50 << "\\bdeleteChilds\\b"
51 << "\\bexportASCII\\b"
52 << "\\bexportImage\\b"
53 << "\\bexportXHTML\\b"
58 << "\\bmoveBranchUp\\b"
59 << "\\bmoveBranchDown\\b"
68 << "\\bselectLastBranch\\b"
69 << "\\bselectLastImage\\b"
70 << "\\bselectLatestAdded\\b"
71 << "\\bsetFrameType\\b"
72 << "\\bsetFramePenColor\\b"
73 << "\\bsetFrameBrushColor\\b"
74 << "\\bsetFramePadding\\b"
75 << "\\bsetFrameBorderWidth\\b"
76 << "\\bsetHideLinkUnselected\\b"
77 << "\\bsetMapAuthor\\b"
78 << "\\bsetMapComment\\b"
79 << "\\bsetMapBackgroundColor\\b"
80 << "\\bsetMapDefLinkColor\\b"
81 << "\\bsetMapDefLinkStyle\\b"
84 << "\\bsetHideExport\\b"
85 << "\\bsetIncludeImagesHorizontally\\b"
86 << "\\bsetIncludeImagesVertically\\b"
90 << "\\bsortChildren\\b"
93 << "\\bunscrollChilds\\b"
96 foreach (QString pattern, keywordPatterns) {
97 rule.pattern = QRegExp(pattern);
98 rule.format = keywordFormat;
99 highlightingRules.append(rule);
104 classFormat.setFontWeight(QFont::Bold);
105 classFormat.setForeground(Qt::darkMagenta);
106 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
107 rule.format = classFormat;
108 highlightingRules.append(rule);
111 // Single line comments
112 singleLineCommentFormat.setForeground(Qt::red);
113 rule.pattern = QRegExp("#[^\n]*");
114 rule.format = singleLineCommentFormat;
115 highlightingRules.append(rule);
117 // multiline comments
118 multiLineCommentFormat.setForeground(Qt::red);
119 commentStartExpression = QRegExp("/\\*");
120 commentEndExpression = QRegExp("\\*/");
123 quotationFormat.setForeground(Qt::darkGreen);
124 rule.pattern = QRegExp("\".*\"");
125 rule.format = quotationFormat;
126 highlightingRules.append(rule);
128 QStringList valuePatterns;
129 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
130 foreach (QString pattern, valuePatterns) {
131 rule.pattern = QRegExp(pattern);
132 rule.format = quotationFormat;
133 highlightingRules.append(rule);
140 functionFormat.setFontItalic(true);
141 functionFormat.setForeground(Qt::blue);
142 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
143 rule.format = functionFormat;
144 highlightingRules.append(rule);
149 void Highlighter::highlightBlock(const QString &text)
151 foreach (HighlightingRule rule, highlightingRules) {
152 QRegExp expression(rule.pattern);
153 int index = text.indexOf(expression);
155 int length = expression.matchedLength();
156 setFormat(index, length, rule.format);
157 index = text.indexOf(expression, index + length);
160 setCurrentBlockState(0);
163 if (previousBlockState() != 1)
164 startIndex = text.indexOf(commentStartExpression);
166 while (startIndex >= 0) {
167 int endIndex = text.indexOf(commentEndExpression, startIndex);
169 if (endIndex == -1) {
170 setCurrentBlockState(1);
171 commentLength = text.length() - startIndex;
173 commentLength = endIndex - startIndex
174 + commentEndExpression.matchedLength();
176 setFormat(startIndex, commentLength, multiLineCommentFormat);
177 startIndex = text.indexOf(commentStartExpression,
178 startIndex + commentLength);