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"
52 << "\\bmoveBranchUp\\b"
53 << "\\bmoveBranchDown\\b"
60 << "\\bselectLastBranch\\b"
61 << "\\bselectLastImage\\b"
62 << "\\bsetFrameType\\b"
63 << "\\bsetFramePenColor\\b"
64 << "\\bsetFrameBrushColor\\b"
65 << "\\bsetFramePadding\\b"
66 << "\\bsetFrameBorderWidth\\b"
67 << "\\bsetHideLinkUnselected\\b"
68 << "\\bsetMapAuthor\\b"
69 << "\\bsetMapComment\\b"
70 << "\\bsetMapBackgroundColor\\b"
71 << "\\bsetMapDefLinkColor\\b"
72 << "\\bsetMapDefLinkStyle\\b"
74 << "\\bsetHideExport\\b"
75 << "\\bsetIncludeImagesHorizontally\\b"
76 << "\\bsetIncludeImagesVertically\\b"
82 << "\\bunscrollChilds\\b"
85 foreach (QString pattern, keywordPatterns) {
86 rule.pattern = QRegExp(pattern);
87 rule.format = keywordFormat;
88 highlightingRules.append(rule);
93 classFormat.setFontWeight(QFont::Bold);
94 classFormat.setForeground(Qt::darkMagenta);
95 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
96 rule.format = classFormat;
97 highlightingRules.append(rule);
100 // Single line comments
101 singleLineCommentFormat.setForeground(Qt::red);
102 rule.pattern = QRegExp("#[^\n]*");
103 rule.format = singleLineCommentFormat;
104 highlightingRules.append(rule);
106 // multiline comments
107 multiLineCommentFormat.setForeground(Qt::red);
108 commentStartExpression = QRegExp("/\\*");
109 commentEndExpression = QRegExp("\\*/");
112 quotationFormat.setForeground(Qt::darkGreen);
113 rule.pattern = QRegExp("\".*\"");
114 rule.format = quotationFormat;
115 highlightingRules.append(rule);
117 QStringList valuePatterns;
118 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
119 foreach (QString pattern, valuePatterns) {
120 rule.pattern = QRegExp(pattern);
121 rule.format = quotationFormat;
122 highlightingRules.append(rule);
129 functionFormat.setFontItalic(true);
130 functionFormat.setForeground(Qt::blue);
131 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
132 rule.format = functionFormat;
133 highlightingRules.append(rule);
138 void Highlighter::highlightBlock(const QString &text)
140 foreach (HighlightingRule rule, highlightingRules) {
141 QRegExp expression(rule.pattern);
142 int index = text.indexOf(expression);
144 int length = expression.matchedLength();
145 setFormat(index, length, rule.format);
146 index = text.indexOf(expression, index + length);
149 setCurrentBlockState(0);
152 if (previousBlockState() != 1)
153 startIndex = text.indexOf(commentStartExpression);
155 while (startIndex >= 0) {
156 int endIndex = text.indexOf(commentEndExpression, startIndex);
158 if (endIndex == -1) {
159 setCurrentBlockState(1);
160 commentLength = text.length() - startIndex;
162 commentLength = endIndex - startIndex
163 + commentEndExpression.matchedLength();
165 setFormat(startIndex, commentLength, multiLineCommentFormat);
166 startIndex = text.indexOf(commentStartExpression,
167 startIndex + commentLength);