1
/* ****************************************************************************
2
This file is part of KAider
4
Copyright (C) 2007 by Nick Shaforostoff <shafff@ukr.net>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
In addition, as a special exception, the copyright holders give
21
permission to link the code of this program with any edition of
22
the Qt library by Trolltech AS, Norway (or with modified versions
23
of Qt that use the same license as Qt), and distribute linked
24
combinations including the two. You must obey the GNU General
25
Public License in all respects for all of the code used other than
26
Qt. If you modify this file, you may extend this exception to
27
your version of the file, but you are not obligated to do so. If
28
you do not wish to do so, delete this exception statement from
31
**************************************************************************** */
34
#include "syntaxhighlighter.h"
40
#include <QApplication>
42
#define STATE_NORMAL 0
46
#define NUM_OF_RULES 4
48
SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent/*, bool docbook*/)
49
: QSyntaxHighlighter(parent)
50
, tagBrush(KColorScheme::View,KColorScheme::VisitedText)
51
// , fuzzyState(false)
52
// , fromDocbook(docbook)
54
highlightingRules.reserve(NUM_OF_RULES);
55
HighlightingRule rule;
56
//rule.format.setFontItalic(true);
57
// tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
58
tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
59
//QTextCharFormat format;
60
//tagFormat.setForeground(Qt::darkBlue);
61
// if (!docbook) //support multiline tags
63
// rule.format = tagFormat;
64
// rule.pattern = QRegExp("<.+>");
65
// rule.pattern.setMinimal(true);
66
// highlightingRules.append(rule);
70
rule.format.setForeground(Qt::darkMagenta);
71
rule.pattern = QRegExp("(&[A-Za-z_:][A-Za-z0-9_\\.:-]*;)");
72
highlightingRules.append(rule);
74
rule.format.setForeground(Qt::darkMagenta);
75
rule.pattern = QRegExp(Project::instance()->accel());
76
//rule.pattern = QRegExp("&[^;]*;");
77
/* QString accelRx=Project::instance()->accel();
78
int pos=accelRx.indexOf('(')+1;
79
rule.pattern = QRegExp( accelRx.mid( pos,accelRx.indexOf(')',pos)-1 ) );*/
80
highlightingRules.append(rule);
83
rule.format.setForeground(Qt::darkGreen);
84
rule.pattern = QRegExp("(\\\\[abfnrtv'\"\?\\\\])|(\\\\\\d+)|(\\\\x[\\dabcdef]+)");
85
highlightingRules.append(rule);
89
rule.format.clearForeground();
90
rule.format.setBackground(Qt::Dense6Pattern);
91
rule.pattern = QRegExp(" ");
92
highlightingRules.append(rule);
94
// commentStartExpression = QRegExp("/\\*");
95
// commentEndExpression = QRegExp("\\*/");
97
// highlightingRulesFuzzy[i].format.setFontItalic(true);
100
void SyntaxHighlighter::setFuzzyState(bool fuzzy)
105
highlightingRules[i].format.setFontItalic(fuzzy);
107
tagFormat.setFontItalic(fuzzy);
110
void SyntaxHighlighter::highlightBlock(const QString &text)
114
setCurrentBlockState(STATE_NORMAL);
116
int startIndex = STATE_NORMAL;
117
if (previousBlockState() != STATE_TAG)
118
startIndex = text.indexOf('<');
120
while (startIndex >= 0)
122
int endIndex = text.indexOf('>', startIndex);
126
setCurrentBlockState(STATE_TAG);
127
commentLength = text.length() - startIndex;
131
commentLength = endIndex - startIndex
132
+1/*+ commentEndExpression.matchedLength()*/;
134
setFormat(startIndex, commentLength, tagFormat);
135
startIndex = text.indexOf('<', startIndex + commentLength);
139
foreach (HighlightingRule rule, highlightingRules)
141
QRegExp expression(rule.pattern);
142
int index = text.indexOf(expression);
145
int length = expression.matchedLength();
146
setFormat(index, length, rule.format);
147
index = text.indexOf(expression, index + length);
153
#include "syntaxhighlighter.moc"