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"
50 << "\\bexportASCII\\b"
51 << "\\bexportImage\\b"
55 << "\\bmoveBranchUp\\b"
56 << "\\bmoveBranchDown\\b"
65 << "\\bselectLastBranch\\b"
66 << "\\bselectLastImage\\b"
67 << "\\bsetFrameType\\b"
68 << "\\bsetFramePenColor\\b"
69 << "\\bsetFrameBrushColor\\b"
70 << "\\bsetFramePadding\\b"
71 << "\\bsetFrameBorderWidth\\b"
72 << "\\bsetHideLinkUnselected\\b"
73 << "\\bsetMapAuthor\\b"
74 << "\\bsetMapComment\\b"
75 << "\\bsetMapBackgroundColor\\b"
76 << "\\bsetMapDefLinkColor\\b"
77 << "\\bsetMapDefLinkStyle\\b"
79 << "\\bsetHideExport\\b"
80 << "\\bsetIncludeImagesHorizontally\\b"
81 << "\\bsetIncludeImagesVertically\\b"
87 << "\\bunscrollChilds\\b"
90 foreach (QString pattern, keywordPatterns) {
91 rule.pattern = QRegExp(pattern);
92 rule.format = keywordFormat;
93 highlightingRules.append(rule);
98 classFormat.setFontWeight(QFont::Bold);
99 classFormat.setForeground(Qt::darkMagenta);
100 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
101 rule.format = classFormat;
102 highlightingRules.append(rule);
105 // Single line comments
106 singleLineCommentFormat.setForeground(Qt::red);
107 rule.pattern = QRegExp("#[^\n]*");
108 rule.format = singleLineCommentFormat;
109 highlightingRules.append(rule);
111 // multiline comments
112 multiLineCommentFormat.setForeground(Qt::red);
113 commentStartExpression = QRegExp("/\\*");
114 commentEndExpression = QRegExp("\\*/");
117 quotationFormat.setForeground(Qt::darkGreen);
118 rule.pattern = QRegExp("\".*\"");
119 rule.format = quotationFormat;
120 highlightingRules.append(rule);
122 QStringList valuePatterns;
123 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
124 foreach (QString pattern, valuePatterns) {
125 rule.pattern = QRegExp(pattern);
126 rule.format = quotationFormat;
127 highlightingRules.append(rule);
134 functionFormat.setFontItalic(true);
135 functionFormat.setForeground(Qt::blue);
136 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
137 rule.format = functionFormat;
138 highlightingRules.append(rule);
143 void Highlighter::highlightBlock(const QString &text)
145 foreach (HighlightingRule rule, highlightingRules) {
146 QRegExp expression(rule.pattern);
147 int index = text.indexOf(expression);
149 int length = expression.matchedLength();
150 setFormat(index, length, rule.format);
151 index = text.indexOf(expression, index + length);
154 setCurrentBlockState(0);
157 if (previousBlockState() != 1)
158 startIndex = text.indexOf(commentStartExpression);
160 while (startIndex >= 0) {
161 int endIndex = text.indexOf(commentEndExpression, startIndex);
163 if (endIndex == -1) {
164 setCurrentBlockState(1);
165 commentLength = text.length() - startIndex;
167 commentLength = endIndex - startIndex
168 + commentEndExpression.matchedLength();
170 setFormat(startIndex, commentLength, multiLineCommentFormat);
171 startIndex = text.indexOf(commentStartExpression,
172 startIndex + commentLength);