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"
47 << "\\bdeletepKeepChilds\\b"
48 << "\\bdeletepChilds\\b"
51 << "\\bmoveBranchUp\\b"
52 << "\\bmoveBranchDown\\b"
59 << "\\bselectLastBranch\\b"
60 << "\\bselectLastImage\\b"
61 << "\\bsetFrameType\\b"
62 << "\\bsetFramePenColor\\b"
63 << "\\bsetFrameBrushColor\\b"
64 << "\\bsetFramePadding\\b"
65 << "\\bsetFrameBorderWidth\\b"
66 << "\\bsetMapAuthor\\b"
67 << "\\bsetMapComment\\b"
68 << "\\bsetMapBackgroundColor\\b"
69 << "\\bsetMapDefLinkColor\\b"
70 << "\\bsetMapDefLinkStyle\\b"
72 << "\\bsetHideExport\\b"
73 << "\\bsetIncludeImagesHorizontally\\b"
74 << "\\bsetIncludeImagesVertically\\b"
80 << "\\bunscrollChilds\\b"
83 foreach (QString pattern, keywordPatterns) {
84 rule.pattern = QRegExp(pattern);
85 rule.format = keywordFormat;
86 highlightingRules.append(rule);
91 classFormat.setFontWeight(QFont::Bold);
92 classFormat.setForeground(Qt::darkMagenta);
93 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
94 rule.format = classFormat;
95 highlightingRules.append(rule);
98 // Single line comments
99 singleLineCommentFormat.setForeground(Qt::red);
100 rule.pattern = QRegExp("#[^\n]*");
101 rule.format = singleLineCommentFormat;
102 highlightingRules.append(rule);
104 // multiline comments
105 multiLineCommentFormat.setForeground(Qt::red);
106 commentStartExpression = QRegExp("/\\*");
107 commentEndExpression = QRegExp("\\*/");
110 quotationFormat.setForeground(Qt::darkGreen);
111 rule.pattern = QRegExp("\".*\"");
112 rule.format = quotationFormat;
113 highlightingRules.append(rule);
115 QStringList valuePatterns;
116 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
117 foreach (QString pattern, valuePatterns) {
118 rule.pattern = QRegExp(pattern);
119 rule.format = quotationFormat;
120 highlightingRules.append(rule);
127 functionFormat.setFontItalic(true);
128 functionFormat.setForeground(Qt::blue);
129 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
130 rule.format = functionFormat;
131 highlightingRules.append(rule);
136 void Highlighter::highlightBlock(const QString &text)
138 foreach (HighlightingRule rule, highlightingRules) {
139 QRegExp expression(rule.pattern);
140 int index = text.indexOf(expression);
142 int length = expression.matchedLength();
143 setFormat(index, length, rule.format);
144 index = text.indexOf(expression, index + length);
147 setCurrentBlockState(0);
150 if (previousBlockState() != 1)
151 startIndex = text.indexOf(commentStartExpression);
153 while (startIndex >= 0) {
154 int endIndex = text.indexOf(commentEndExpression, startIndex);
156 if (endIndex == -1) {
157 setCurrentBlockState(1);
158 commentLength = text.length() - startIndex;
160 commentLength = endIndex - startIndex
161 + commentEndExpression.matchedLength();
163 setFormat(startIndex, commentLength, multiLineCommentFormat);
164 startIndex = text.indexOf(commentStartExpression,
165 startIndex + commentLength);