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"
60 << "\\bmoveBranchUp\\b"
61 << "\\bmoveBranchDown\\b"
71 << "\\bselectLastBranch\\b"
72 << "\\bselectLastImage\\b"
73 << "\\bselectLatestAdded\\b"
74 << "\\bsetFrameType\\b"
75 << "\\bsetFramePenColor\\b"
76 << "\\bsetFrameBrushColor\\b"
77 << "\\bsetFramePadding\\b"
78 << "\\bsetFrameBorderWidth\\b"
79 << "\\bsetHideLinkUnselected\\b"
80 << "\\bsetMapAuthor\\b"
81 << "\\bsetMapComment\\b"
82 << "\\bsetMapBackgroundColor\\b"
83 << "\\bsetMapDefLinkColor\\b"
84 << "\\bsetMapDefLinkStyle\\b"
87 << "\\bsetHideExport\\b"
88 << "\\bsetIncludeImagesHorizontally\\b"
89 << "\\bsetIncludeImagesVertically\\b"
93 << "\\bsortChildren\\b"
96 << "\\bunscrollChilds\\b"
99 foreach (QString pattern, keywordPatterns) {
100 rule.pattern = QRegExp(pattern);
101 rule.format = keywordFormat;
102 highlightingRules.append(rule);
107 classFormat.setFontWeight(QFont::Bold);
108 classFormat.setForeground(Qt::darkMagenta);
109 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
110 rule.format = classFormat;
111 highlightingRules.append(rule);
114 // Single line comments
115 singleLineCommentFormat.setForeground(Qt::red);
116 rule.pattern = QRegExp("#[^\n]*");
117 rule.format = singleLineCommentFormat;
118 highlightingRules.append(rule);
120 // multiline comments
121 multiLineCommentFormat.setForeground(Qt::red);
122 commentStartExpression = QRegExp("/\\*");
123 commentEndExpression = QRegExp("\\*/");
126 quotationFormat.setForeground(Qt::darkGreen);
127 rule.pattern = QRegExp("\".*\"");
128 rule.format = quotationFormat;
129 highlightingRules.append(rule);
131 QStringList valuePatterns;
132 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
133 foreach (QString pattern, valuePatterns) {
134 rule.pattern = QRegExp(pattern);
135 rule.format = quotationFormat;
136 highlightingRules.append(rule);
143 functionFormat.setFontItalic(true);
144 functionFormat.setForeground(Qt::blue);
145 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
146 rule.format = functionFormat;
147 highlightingRules.append(rule);
152 void Highlighter::highlightBlock(const QString &text)
154 foreach (HighlightingRule rule, highlightingRules) {
155 QRegExp expression(rule.pattern);
156 int index = text.indexOf(expression);
158 int length = expression.matchedLength();
159 setFormat(index, length, rule.format);
160 index = text.indexOf(expression, index + length);
163 setCurrentBlockState(0);
166 if (previousBlockState() != 1)
167 startIndex = text.indexOf(commentStartExpression);
169 while (startIndex >= 0) {
170 int endIndex = text.indexOf(commentEndExpression, startIndex);
172 if (endIndex == -1) {
173 setCurrentBlockState(1);
174 commentLength = text.length() - startIndex;
176 commentLength = endIndex - startIndex
177 + commentEndExpression.matchedLength();
179 setFormat(startIndex, commentLength, multiLineCommentFormat);
180 startIndex = text.indexOf(commentStartExpression,
181 startIndex + commentLength);