]> git.sven.stormbind.net Git - sven/vym.git/blob - src/highlighter.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / 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 #include <QtGui>
27
28 #include "highlighter.h"
29
30 extern bool usingDarkTheme;
31
32 Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
33 {
34
35     HighlightingRule rule;
36
37     if (usingDarkTheme)
38         keywordFormat.setForeground(Qt::cyan);
39     else
40         keywordFormat.setForeground(Qt::darkBlue);
41
42     keywordFormat.setFontWeight(QFont::Bold);
43
44     // QT keywords
45     /*
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);
51     */
52
53     // Single line comments
54     if (usingDarkTheme)
55         singleLineCommentFormat.setForeground(Qt::magenta);
56     else
57         singleLineCommentFormat.setForeground(Qt::darkMagenta);
58     rule.pattern = QRegExp("//[^\n]*");
59     rule.format = singleLineCommentFormat;
60     highlightingRules.append(rule);
61
62     // multiline comments
63     if (usingDarkTheme)
64         multiLineCommentFormat.setForeground(Qt::magenta);
65     else
66         multiLineCommentFormat.setForeground(Qt::darkMagenta);
67     commentStartExpression = QRegExp("/\\*");
68     commentEndExpression = QRegExp("\\*/");
69
70     // Quotations
71     quotationFormat.setForeground(Qt::red);
72     rule.pattern = QRegExp("\".*\"");
73     rule.format = quotationFormat;
74     highlightingRules.append(rule);
75
76     QStringList valuePatterns;
77     valuePatterns << "\\btrue\\b"
78                   << "\\bfalse\\b";
79     foreach (QString pattern, valuePatterns) {
80         rule.pattern = QRegExp(pattern);
81         rule.format = quotationFormat;
82         highlightingRules.append(rule);
83     }
84
85     // Funtions
86     /*
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);
92     */
93 }
94
95 void Highlighter::addKeywords(const QStringList &list)
96 {
97     HighlightingRule rule;
98     foreach (QString pattern, list) {
99         rule.pattern = QRegExp(pattern);
100         rule.format = keywordFormat;
101         highlightingRules.append(rule);
102     }
103 }
104
105 void Highlighter::highlightBlock(const QString &text)
106 {
107     foreach (HighlightingRule rule, highlightingRules) {
108         QRegExp expression(rule.pattern);
109         int index = text.indexOf(expression);
110         while (index >= 0) {
111             int length = expression.matchedLength();
112             setFormat(index, length, rule.format);
113             index = text.indexOf(expression, index + length);
114         }
115     }
116     setCurrentBlockState(0);
117
118     int startIndex = 0;
119     if (previousBlockState() != 1)
120         startIndex = text.indexOf(commentStartExpression);
121
122     while (startIndex >= 0) {
123         int endIndex = text.indexOf(commentEndExpression, startIndex);
124         int commentLength;
125         if (endIndex == -1) {
126             setCurrentBlockState(1);
127             commentLength = text.length() - startIndex;
128         }
129         else {
130             commentLength =
131                 endIndex - startIndex + commentEndExpression.matchedLength();
132         }
133         setFormat(startIndex, commentLength, multiLineCommentFormat);
134         startIndex =
135             text.indexOf(commentStartExpression, startIndex + commentLength);
136     }
137 }