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"
52 << "\\bexportXHTML\\b"
57 << "\\bmoveBranchUp\\b"
58 << "\\bmoveBranchDown\\b"
67 << "\\bselectLastBranch\\b"
68 << "\\bselectLastImage\\b"
69 << "\\bsetFrameType\\b"
70 << "\\bsetFramePenColor\\b"
71 << "\\bsetFrameBrushColor\\b"
72 << "\\bsetFramePadding\\b"
73 << "\\bsetFrameBorderWidth\\b"
74 << "\\bsetHideLinkUnselected\\b"
75 << "\\bsetMapAuthor\\b"
76 << "\\bsetMapComment\\b"
77 << "\\bsetMapBackgroundColor\\b"
78 << "\\bsetMapDefLinkColor\\b"
79 << "\\bsetMapDefLinkStyle\\b"
81 << "\\bsetHideExport\\b"
82 << "\\bsetIncludeImagesHorizontally\\b"
83 << "\\bsetIncludeImagesVertically\\b"
89 << "\\bunscrollChilds\\b"
92 foreach (QString pattern, keywordPatterns) {
93 rule.pattern = QRegExp(pattern);
94 rule.format = keywordFormat;
95 highlightingRules.append(rule);
100 classFormat.setFontWeight(QFont::Bold);
101 classFormat.setForeground(Qt::darkMagenta);
102 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
103 rule.format = classFormat;
104 highlightingRules.append(rule);
107 // Single line comments
108 singleLineCommentFormat.setForeground(Qt::red);
109 rule.pattern = QRegExp("#[^\n]*");
110 rule.format = singleLineCommentFormat;
111 highlightingRules.append(rule);
113 // multiline comments
114 multiLineCommentFormat.setForeground(Qt::red);
115 commentStartExpression = QRegExp("/\\*");
116 commentEndExpression = QRegExp("\\*/");
119 quotationFormat.setForeground(Qt::darkGreen);
120 rule.pattern = QRegExp("\".*\"");
121 rule.format = quotationFormat;
122 highlightingRules.append(rule);
124 QStringList valuePatterns;
125 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
126 foreach (QString pattern, valuePatterns) {
127 rule.pattern = QRegExp(pattern);
128 rule.format = quotationFormat;
129 highlightingRules.append(rule);
136 functionFormat.setFontItalic(true);
137 functionFormat.setForeground(Qt::blue);
138 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
139 rule.format = functionFormat;
140 highlightingRules.append(rule);
145 void Highlighter::highlightBlock(const QString &text)
147 foreach (HighlightingRule rule, highlightingRules) {
148 QRegExp expression(rule.pattern);
149 int index = text.indexOf(expression);
151 int length = expression.matchedLength();
152 setFormat(index, length, rule.format);
153 index = text.indexOf(expression, index + length);
156 setCurrentBlockState(0);
159 if (previousBlockState() != 1)
160 startIndex = text.indexOf(commentStartExpression);
162 while (startIndex >= 0) {
163 int endIndex = text.indexOf(commentEndExpression, startIndex);
165 if (endIndex == -1) {
166 setCurrentBlockState(1);
167 commentLength = text.length() - startIndex;
169 commentLength = endIndex - startIndex
170 + commentEndExpression.matchedLength();
172 setFormat(startIndex, commentLength, multiLineCommentFormat);
173 startIndex = text.indexOf(commentStartExpression,
174 startIndex + commentLength);