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"
83 << "\\bsetHideExport\\b"
84 << "\\bsetIncludeImagesHorizontally\\b"
85 << "\\bsetIncludeImagesVertically\\b"
89 << "\\bsortChildren\\b"
92 << "\\bunscrollChilds\\b"
95 foreach (QString pattern, keywordPatterns) {
96 rule.pattern = QRegExp(pattern);
97 rule.format = keywordFormat;
98 highlightingRules.append(rule);
103 classFormat.setFontWeight(QFont::Bold);
104 classFormat.setForeground(Qt::darkMagenta);
105 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
106 rule.format = classFormat;
107 highlightingRules.append(rule);
110 // Single line comments
111 singleLineCommentFormat.setForeground(Qt::red);
112 rule.pattern = QRegExp("#[^\n]*");
113 rule.format = singleLineCommentFormat;
114 highlightingRules.append(rule);
116 // multiline comments
117 multiLineCommentFormat.setForeground(Qt::red);
118 commentStartExpression = QRegExp("/\\*");
119 commentEndExpression = QRegExp("\\*/");
122 quotationFormat.setForeground(Qt::darkGreen);
123 rule.pattern = QRegExp("\".*\"");
124 rule.format = quotationFormat;
125 highlightingRules.append(rule);
127 QStringList valuePatterns;
128 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
129 foreach (QString pattern, valuePatterns) {
130 rule.pattern = QRegExp(pattern);
131 rule.format = quotationFormat;
132 highlightingRules.append(rule);
139 functionFormat.setFontItalic(true);
140 functionFormat.setForeground(Qt::blue);
141 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
142 rule.format = functionFormat;
143 highlightingRules.append(rule);
148 void Highlighter::highlightBlock(const QString &text)
150 foreach (HighlightingRule rule, highlightingRules) {
151 QRegExp expression(rule.pattern);
152 int index = text.indexOf(expression);
154 int length = expression.matchedLength();
155 setFormat(index, length, rule.format);
156 index = text.indexOf(expression, index + length);
159 setCurrentBlockState(0);
162 if (previousBlockState() != 1)
163 startIndex = text.indexOf(commentStartExpression);
165 while (startIndex >= 0) {
166 int endIndex = text.indexOf(commentEndExpression, startIndex);
168 if (endIndex == -1) {
169 setCurrentBlockState(1);
170 commentLength = text.length() - startIndex;
172 commentLength = endIndex - startIndex
173 + commentEndExpression.matchedLength();
175 setFormat(startIndex, commentLength, multiLineCommentFormat);
176 startIndex = text.indexOf(commentStartExpression,
177 startIndex + commentLength);