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
28 #include "highlighter.h"
30 extern bool usingDarkTheme;
32 Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
35 HighlightingRule rule;
38 keywordFormat.setForeground(Qt::cyan);
40 keywordFormat.setForeground(Qt::darkBlue);
42 keywordFormat.setFontWeight(QFont::Bold);
46 classFormat.setFontWeight(QFont::Bold);
47 classFormat.setForeground(Qt::darkMagenta);
48 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
49 rule.format = classFormat;
50 highlightingRules.append(rule);
53 // Single line comments
55 singleLineCommentFormat.setForeground(Qt::magenta);
57 singleLineCommentFormat.setForeground(Qt::darkMagenta);
58 rule.pattern = QRegExp("//[^\n]*");
59 rule.format = singleLineCommentFormat;
60 highlightingRules.append(rule);
64 multiLineCommentFormat.setForeground(Qt::magenta);
66 multiLineCommentFormat.setForeground(Qt::darkMagenta);
67 commentStartExpression = QRegExp("/\\*");
68 commentEndExpression = QRegExp("\\*/");
71 quotationFormat.setForeground(Qt::red);
72 rule.pattern = QRegExp("\".*\"");
73 rule.format = quotationFormat;
74 highlightingRules.append(rule);
76 QStringList valuePatterns;
77 valuePatterns << "\\btrue\\b"
79 foreach (QString pattern, valuePatterns) {
80 rule.pattern = QRegExp(pattern);
81 rule.format = quotationFormat;
82 highlightingRules.append(rule);
87 functionFormat.setFontItalic(true);
88 functionFormat.setForeground(Qt::blue);
89 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
90 rule.format = functionFormat;
91 highlightingRules.append(rule);
95 void Highlighter::addKeywords(const QStringList &list)
97 HighlightingRule rule;
98 foreach (QString pattern, list) {
99 rule.pattern = QRegExp(pattern);
100 rule.format = keywordFormat;
101 highlightingRules.append(rule);
105 void Highlighter::highlightBlock(const QString &text)
107 foreach (HighlightingRule rule, highlightingRules) {
108 QRegExp expression(rule.pattern);
109 int index = text.indexOf(expression);
111 int length = expression.matchedLength();
112 setFormat(index, length, rule.format);
113 index = text.indexOf(expression, index + length);
116 setCurrentBlockState(0);
119 if (previousBlockState() != 1)
120 startIndex = text.indexOf(commentStartExpression);
122 while (startIndex >= 0) {
123 int endIndex = text.indexOf(commentEndExpression, startIndex);
125 if (endIndex == -1) {
126 setCurrentBlockState(1);
127 commentLength = text.length() - startIndex;
131 endIndex - startIndex + commentEndExpression.matchedLength();
133 setFormat(startIndex, commentLength, multiLineCommentFormat);
135 text.indexOf(commentStartExpression, startIndex + commentLength);