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"
48 << "\\bdeleteKeepChilds\\b"
49 << "\\bdeleteChilds\\b"
53 << "\\bmoveBranchUp\\b"
54 << "\\bmoveBranchDown\\b"
63 << "\\bselectLastBranch\\b"
64 << "\\bselectLastImage\\b"
65 << "\\bsetFrameType\\b"
66 << "\\bsetFramePenColor\\b"
67 << "\\bsetFrameBrushColor\\b"
68 << "\\bsetFramePadding\\b"
69 << "\\bsetFrameBorderWidth\\b"
70 << "\\bsetHideLinkUnselected\\b"
71 << "\\bsetMapAuthor\\b"
72 << "\\bsetMapComment\\b"
73 << "\\bsetMapBackgroundColor\\b"
74 << "\\bsetMapDefLinkColor\\b"
75 << "\\bsetMapDefLinkStyle\\b"
77 << "\\bsetHideExport\\b"
78 << "\\bsetIncludeImagesHorizontally\\b"
79 << "\\bsetIncludeImagesVertically\\b"
85 << "\\bunscrollChilds\\b"
88 foreach (QString pattern, keywordPatterns) {
89 rule.pattern = QRegExp(pattern);
90 rule.format = keywordFormat;
91 highlightingRules.append(rule);
96 classFormat.setFontWeight(QFont::Bold);
97 classFormat.setForeground(Qt::darkMagenta);
98 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
99 rule.format = classFormat;
100 highlightingRules.append(rule);
103 // Single line comments
104 singleLineCommentFormat.setForeground(Qt::red);
105 rule.pattern = QRegExp("#[^\n]*");
106 rule.format = singleLineCommentFormat;
107 highlightingRules.append(rule);
109 // multiline comments
110 multiLineCommentFormat.setForeground(Qt::red);
111 commentStartExpression = QRegExp("/\\*");
112 commentEndExpression = QRegExp("\\*/");
115 quotationFormat.setForeground(Qt::darkGreen);
116 rule.pattern = QRegExp("\".*\"");
117 rule.format = quotationFormat;
118 highlightingRules.append(rule);
120 QStringList valuePatterns;
121 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
122 foreach (QString pattern, valuePatterns) {
123 rule.pattern = QRegExp(pattern);
124 rule.format = quotationFormat;
125 highlightingRules.append(rule);
132 functionFormat.setFontItalic(true);
133 functionFormat.setForeground(Qt::blue);
134 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
135 rule.format = functionFormat;
136 highlightingRules.append(rule);
141 void Highlighter::highlightBlock(const QString &text)
143 foreach (HighlightingRule rule, highlightingRules) {
144 QRegExp expression(rule.pattern);
145 int index = text.indexOf(expression);
147 int length = expression.matchedLength();
148 setFormat(index, length, rule.format);
149 index = text.indexOf(expression, index + length);
152 setCurrentBlockState(0);
155 if (previousBlockState() != 1)
156 startIndex = text.indexOf(commentStartExpression);
158 while (startIndex >= 0) {
159 int endIndex = text.indexOf(commentEndExpression, startIndex);
161 if (endIndex == -1) {
162 setCurrentBlockState(1);
163 commentLength = text.length() - startIndex;
165 commentLength = endIndex - startIndex
166 + commentEndExpression.matchedLength();
168 setFormat(startIndex, commentLength, multiLineCommentFormat);
169 startIndex = text.indexOf(commentStartExpression,
170 startIndex + commentLength);