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 << "\\bsetMapAuthor\\b"
62 << "\\bsetMapComment\\b"
63 << "\\bsetMapBackgroundColor\\b"
64 << "\\bsetMapDefLinkColor\\b"
65 << "\\bsetMapDefLinkStyle\\b"
67 << "\\bsetHideExport\\b"
68 << "\\bsetIncludeImagesHorizontally\\b"
69 << "\\bsetIncludeImagesVertically\\b"
77 foreach (QString pattern, keywordPatterns) {
78 rule.pattern = QRegExp(pattern);
79 rule.format = keywordFormat;
80 highlightingRules.append(rule);
85 classFormat.setFontWeight(QFont::Bold);
86 classFormat.setForeground(Qt::darkMagenta);
87 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
88 rule.format = classFormat;
89 highlightingRules.append(rule);
92 // Single line comments
93 singleLineCommentFormat.setForeground(Qt::red);
94 rule.pattern = QRegExp("#[^\n]*");
95 rule.format = singleLineCommentFormat;
96 highlightingRules.append(rule);
99 multiLineCommentFormat.setForeground(Qt::red);
100 commentStartExpression = QRegExp("/\\*");
101 commentEndExpression = QRegExp("\\*/");
104 quotationFormat.setForeground(Qt::darkGreen);
105 rule.pattern = QRegExp("\".*\"");
106 rule.format = quotationFormat;
107 highlightingRules.append(rule);
109 QStringList valuePatterns;
110 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
111 foreach (QString pattern, valuePatterns) {
112 rule.pattern = QRegExp(pattern);
113 rule.format = quotationFormat;
114 highlightingRules.append(rule);
121 functionFormat.setFontItalic(true);
122 functionFormat.setForeground(Qt::blue);
123 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
124 rule.format = functionFormat;
125 highlightingRules.append(rule);
130 void Highlighter::highlightBlock(const QString &text)
132 foreach (HighlightingRule rule, highlightingRules) {
133 QRegExp expression(rule.pattern);
134 int index = text.indexOf(expression);
136 int length = expression.matchedLength();
137 setFormat(index, length, rule.format);
138 index = text.indexOf(expression, index + length);
141 setCurrentBlockState(0);
144 if (previousBlockState() != 1)
145 startIndex = text.indexOf(commentStartExpression);
147 while (startIndex >= 0) {
148 int endIndex = text.indexOf(commentEndExpression, startIndex);
150 if (endIndex == -1) {
151 setCurrentBlockState(1);
152 commentLength = text.length() - startIndex;
154 commentLength = endIndex - startIndex
155 + commentEndExpression.matchedLength();
157 setFormat(startIndex, commentLength, multiLineCommentFormat);
158 startIndex = text.indexOf(commentStartExpression,
159 startIndex + commentLength);