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"
54 << "\\bexportXHTML\\b"
59 << "\\bmoveBranchUp\\b"
60 << "\\bmoveBranchDown\\b"
69 << "\\bselectLastBranch\\b"
70 << "\\bselectLastImage\\b"
71 << "\\bselectLatestAdded\\b"
72 << "\\bsetFrameType\\b"
73 << "\\bsetFramePenColor\\b"
74 << "\\bsetFrameBrushColor\\b"
75 << "\\bsetFramePadding\\b"
76 << "\\bsetFrameBorderWidth\\b"
77 << "\\bsetHideLinkUnselected\\b"
78 << "\\bsetMapAuthor\\b"
79 << "\\bsetMapComment\\b"
80 << "\\bsetMapBackgroundColor\\b"
81 << "\\bsetMapDefLinkColor\\b"
82 << "\\bsetMapDefLinkStyle\\b"
85 << "\\bsetHideExport\\b"
86 << "\\bsetIncludeImagesHorizontally\\b"
87 << "\\bsetIncludeImagesVertically\\b"
91 << "\\bsortChildren\\b"
94 << "\\bunscrollChilds\\b"
97 foreach (QString pattern, keywordPatterns) {
98 rule.pattern = QRegExp(pattern);
99 rule.format = keywordFormat;
100 highlightingRules.append(rule);
105 classFormat.setFontWeight(QFont::Bold);
106 classFormat.setForeground(Qt::darkMagenta);
107 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
108 rule.format = classFormat;
109 highlightingRules.append(rule);
112 // Single line comments
113 singleLineCommentFormat.setForeground(Qt::red);
114 rule.pattern = QRegExp("#[^\n]*");
115 rule.format = singleLineCommentFormat;
116 highlightingRules.append(rule);
118 // multiline comments
119 multiLineCommentFormat.setForeground(Qt::red);
120 commentStartExpression = QRegExp("/\\*");
121 commentEndExpression = QRegExp("\\*/");
124 quotationFormat.setForeground(Qt::darkGreen);
125 rule.pattern = QRegExp("\".*\"");
126 rule.format = quotationFormat;
127 highlightingRules.append(rule);
129 QStringList valuePatterns;
130 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
131 foreach (QString pattern, valuePatterns) {
132 rule.pattern = QRegExp(pattern);
133 rule.format = quotationFormat;
134 highlightingRules.append(rule);
141 functionFormat.setFontItalic(true);
142 functionFormat.setForeground(Qt::blue);
143 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
144 rule.format = functionFormat;
145 highlightingRules.append(rule);
150 void Highlighter::highlightBlock(const QString &text)
152 foreach (HighlightingRule rule, highlightingRules) {
153 QRegExp expression(rule.pattern);
154 int index = text.indexOf(expression);
156 int length = expression.matchedLength();
157 setFormat(index, length, rule.format);
158 index = text.indexOf(expression, index + length);
161 setCurrentBlockState(0);
164 if (previousBlockState() != 1)
165 startIndex = text.indexOf(commentStartExpression);
167 while (startIndex >= 0) {
168 int endIndex = text.indexOf(commentEndExpression, startIndex);
170 if (endIndex == -1) {
171 setCurrentBlockState(1);
172 commentLength = text.length() - startIndex;
174 commentLength = endIndex - startIndex
175 + commentEndExpression.matchedLength();
177 setFormat(startIndex, commentLength, multiLineCommentFormat);
178 startIndex = text.indexOf(commentStartExpression,
179 startIndex + commentLength);