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" << "\\baddBranchBefore\\b"
40 << "\\baddMapInsert\\b" << "\\baddMapReplace\\b"
41 << "\\bcolorBranch\\b" << "\\bcolorSubtree\\b"
42 << "\\bcut\\b" << "\\bdelete\\b"
43 << "\\bdeletepKeepChilds\\b" << "\\bdeletepChilds\\b"
44 << "\\blinkTo\\b" << "\\bloadImage\\b"
45 << "\\bmoveBranchUp\\b" << "\\bmoveBranchDown\\b"
46 << "\\bmove\\b" << "\\bmoveRel\\b"
50 << "\\bselect\\b" << "\\bselectLastBranch\\b" << "\\bselectLastImage\\b"
51 << "\\bsetMapAuthor\\b"
52 << "\\bsetMapComment\\b"
53 << "\\bsetMapBackgroundColor\\b"
54 << "\\bsetMapDefLinkColor\\b"
55 << "\\bsetMapDefLinkStyle\\b"
57 << "\\bsetHideExport\\b"
58 << "\\bsetIncludeImagesHorizontally\\b"
59 << "\\bsetIncludeImagesVertically\\b"
66 foreach (QString pattern, keywordPatterns) {
67 rule.pattern = QRegExp(pattern);
68 rule.format = keywordFormat;
69 highlightingRules.append(rule);
74 classFormat.setFontWeight(QFont::Bold);
75 classFormat.setForeground(Qt::darkMagenta);
76 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
77 rule.format = classFormat;
78 highlightingRules.append(rule);
81 // Single line comments
82 singleLineCommentFormat.setForeground(Qt::red);
83 rule.pattern = QRegExp("#[^\n]*");
84 rule.format = singleLineCommentFormat;
85 highlightingRules.append(rule);
87 // Single line comments
88 multiLineCommentFormat.setForeground(Qt::red);
89 commentStartExpression = QRegExp("/\\*");
90 commentEndExpression = QRegExp("\\*/");
93 quotationFormat.setForeground(Qt::darkGreen);
94 rule.pattern = QRegExp("\".*\"");
95 rule.format = quotationFormat;
96 highlightingRules.append(rule);
98 QStringList valuePatterns;
99 valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
100 foreach (QString pattern, valuePatterns) {
101 rule.pattern = QRegExp(pattern);
102 rule.format = quotationFormat;
103 highlightingRules.append(rule);
110 functionFormat.setFontItalic(true);
111 functionFormat.setForeground(Qt::blue);
112 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
113 rule.format = functionFormat;
114 highlightingRules.append(rule);
119 void Highlighter::highlightBlock(const QString &text)
121 foreach (HighlightingRule rule, highlightingRules) {
122 QRegExp expression(rule.pattern);
123 int index = text.indexOf(expression);
125 int length = expression.matchedLength();
126 setFormat(index, length, rule.format);
127 index = text.indexOf(expression, index + length);
130 setCurrentBlockState(0);
133 if (previousBlockState() != 1)
134 startIndex = text.indexOf(commentStartExpression);
136 while (startIndex >= 0) {
137 int endIndex = text.indexOf(commentEndExpression, startIndex);
139 if (endIndex == -1) {
140 setCurrentBlockState(1);
141 commentLength = text.length() - startIndex;
143 commentLength = endIndex - startIndex
144 + commentEndExpression.matchedLength();
146 setFormat(startIndex, commentLength, multiLineCommentFormat);
147 startIndex = text.indexOf(commentStartExpression,
148 startIndex + commentLength);