]> git.sven.stormbind.net Git - sven/vym.git/blob - highlighter.cpp
fdad6e4e3dcb17bac4848d043892302e92cb04b6
[sven/vym.git] / highlighter.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the example classes of the Qt Toolkit.
6 **
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
13 **
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.
18 **
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.
21 **
22 ****************************************************************************/
23
24 // highlighting rules have been adapted by Uwe Drechsel to match vym syntax
25
26
27 #include <QtGui>
28
29 #include "highlighter.h"
30
31 Highlighter::Highlighter(QTextDocument *parent) 
32     : QSyntaxHighlighter(parent)
33 {
34     HighlightingRule rule;
35
36     keywordFormat.setForeground(Qt::darkBlue);
37     keywordFormat.setFontWeight(QFont::Bold);
38
39     // QT keywords
40     /*
41     classFormat.setFontWeight(QFont::Bold);
42     classFormat.setForeground(Qt::darkMagenta);
43     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
44     rule.format = classFormat;
45     highlightingRules.append(rule);
46     */
47
48     // Single line comments
49     singleLineCommentFormat.setForeground(Qt::red);
50     rule.pattern = QRegExp("#[^\n]*");
51     rule.format = singleLineCommentFormat;
52     highlightingRules.append(rule);
53
54     // multiline comments
55     multiLineCommentFormat.setForeground(Qt::red);
56     commentStartExpression = QRegExp("/\\*");
57     commentEndExpression = QRegExp("\\*/");
58
59     // Quotations
60     quotationFormat.setForeground(Qt::darkGreen);
61     rule.pattern = QRegExp("\".*\"");
62     rule.format = quotationFormat;
63     highlightingRules.append(rule);
64
65     QStringList valuePatterns;
66     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
67     foreach (QString pattern, valuePatterns) {
68         rule.pattern = QRegExp(pattern);
69         rule.format = quotationFormat;
70         highlightingRules.append(rule);
71     }
72
73
74
75     // Funtions
76     /*
77     functionFormat.setFontItalic(true);
78     functionFormat.setForeground(Qt::blue);
79     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
80     rule.format = functionFormat;
81     highlightingRules.append(rule);
82     */
83
84 }
85
86 void Highlighter::addKeywords (const QStringList &list)
87 {
88     HighlightingRule rule;
89     foreach (QString pattern, list) {
90         rule.pattern = QRegExp(pattern);
91         rule.format = keywordFormat;
92         highlightingRules.append(rule);
93     }
94 }
95
96 void Highlighter::highlightBlock(const QString &text)
97 {
98     foreach (HighlightingRule rule, highlightingRules) {
99         QRegExp expression(rule.pattern);
100         int index = text.indexOf(expression);
101         while (index >= 0) {
102             int length = expression.matchedLength();
103             setFormat(index, length, rule.format);
104             index = text.indexOf(expression, index + length);
105         }
106     }
107     setCurrentBlockState(0);
108
109     int startIndex = 0;
110     if (previousBlockState() != 1)
111         startIndex = text.indexOf(commentStartExpression);
112
113     while (startIndex >= 0) {
114         int endIndex = text.indexOf(commentEndExpression, startIndex);
115         int commentLength;
116         if (endIndex == -1) {
117             setCurrentBlockState(1);
118             commentLength = text.length() - startIndex;
119         } else {
120             commentLength = endIndex - startIndex
121                             + commentEndExpression.matchedLength();
122         }
123         setFormat(startIndex, commentLength, multiLineCommentFormat);
124         startIndex = text.indexOf(commentStartExpression,
125                                                 startIndex + commentLength);
126     }
127 }