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"
52 << "\\bexportASCII\\b"
53 << "\\bexportImage\\b"
59 << "\\bmoveBranchUp\\b"
60 << "\\bmoveBranchDown\\b"
70 << "\\bselectLastBranch\\b"
71 << "\\bselectLastImage\\b"
72 << "\\bselectLatestAdded\\b"
73 << "\\bsetFrameType\\b"
74 << "\\bsetFramePenColor\\b"
75 << "\\bsetFrameBrushColor\\b"
76 << "\\bsetFramePadding\\b"
77 << "\\bsetFrameBorderWidth\\b"
78 << "\\bsetHideLinkUnselected\\b"
79 << "\\bsetMapAuthor\\b"
80 << "\\bsetMapComment\\b"
81 << "\\bsetMapBackgroundColor\\b"
82 << "\\bsetMapDefLinkColor\\b"
83 << "\\bsetMapDefLinkStyle\\b"
86 << "\\bsetHideExport\\b"
87 << "\\bsetIncludeImagesHorizontally\\b"
88 << "\\bsetIncludeImagesVertically\\b"
92 << "\\bsortChildren\\b"
95 << "\\bunscrollChilds\\b"
98 foreach (QString pattern, keywordPatterns) {
99 rule.pattern = QRegExp(pattern);
100 rule.format = keywordFormat;
101 highlightingRules.append(rule);
106 classFormat.setFontWeight(QFont::Bold);
107 classFormat.setForeground(Qt::darkMagenta);
108 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
109 rule.format = classFormat;
110 highlightingRules.append(rule);
113 // Single line comments
114 singleLineCommentFormat.setForeground(Qt::red);
115 rule.pattern = QRegExp("#[^\n]*");
116 rule.format = singleLineCommentFormat;
117 highlightingRules.append(rule);
119 // multiline comments
120 multiLineCommentFormat.setForeground(Qt::red);
121 commentStartExpression = QRegExp("/\\*");
122 commentEndExpression = QRegExp("\\*/");
125 quotationFormat.setForeground(Qt::darkGreen);
126 rule.pattern = QRegExp("\".*\"");
127 rule.format = quotationFormat;
128 highlightingRules.append(rule);
130 QStringList valuePatterns;
131 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
132 foreach (QString pattern, valuePatterns) {
133 rule.pattern = QRegExp(pattern);
134 rule.format = quotationFormat;
135 highlightingRules.append(rule);
142 functionFormat.setFontItalic(true);
143 functionFormat.setForeground(Qt::blue);
144 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
145 rule.format = functionFormat;
146 highlightingRules.append(rule);
151 void Highlighter::highlightBlock(const QString &text)
153 foreach (HighlightingRule rule, highlightingRules) {
154 QRegExp expression(rule.pattern);
155 int index = text.indexOf(expression);
157 int length = expression.matchedLength();
158 setFormat(index, length, rule.format);
159 index = text.indexOf(expression, index + length);
162 setCurrentBlockState(0);
165 if (previousBlockState() != 1)
166 startIndex = text.indexOf(commentStartExpression);
168 while (startIndex >= 0) {
169 int endIndex = text.indexOf(commentEndExpression, startIndex);
171 if (endIndex == -1) {
172 setCurrentBlockState(1);
173 commentLength = text.length() - startIndex;
175 commentLength = endIndex - startIndex
176 + commentEndExpression.matchedLength();
178 setFormat(startIndex, commentLength, multiLineCommentFormat);
179 startIndex = text.indexOf(commentStartExpression,
180 startIndex + commentLength);