Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 86 lines (71 sloc) 2.207 kb
3e49988 @bmcdorman New template system and more refactoring
bmcdorman authored
1 #include "syntax_highlighter.hpp"
2
3 #include <QSyntaxHighlighter>
4 #include <QRegExp>
5 #include <QStringList>
6 #include <QDebug>
7
8 using namespace Kiss;
9
10 SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
11 : QSyntaxHighlighter(parent)
12 {
13 }
14
15 void SyntaxHighlighter::highlightBlock(const QString& text)
16 {
17 qDebug() << "Highlighting" << text;
18 foreach(const Rule& rule, m_highlightingRules) {
19 QRegExp expression(rule.pattern);
20 int index = expression.indexIn(text);
21 while(index >= 0) {
22 int length = expression.matchedLength();
23 setFormat(index, length, rule.format);
24 index = expression.indexIn(text, index + length);
25 }
26 }
27
28 setCurrentBlockState(0);
29
30 int startIndex = 0;
31 if(previousBlockState() != 1)
32 startIndex = m_commentStartExpression.indexIn(text);
33
34 while(startIndex >= 0) {
35 int endIndex = m_commentEndExpression.indexIn(text, startIndex);
36 int commentLength;
37 if(endIndex == -1) {
38 setCurrentBlockState(1);
39 commentLength = text.length() - startIndex;
40 } else {
41 commentLength = endIndex - startIndex + m_commentEndExpression.matchedLength();
42 }
43 setFormat(startIndex, commentLength, m_multiLineCommentFormat);
44 startIndex = m_commentStartExpression.indexIn(text, startIndex + commentLength);
45 }
46 }
47
48 void SyntaxHighlighter::setCommentStartExpression(const QRegExp& commentStartExpression)
49 {
50 m_commentStartExpression = commentStartExpression;
51 }
52
53 void SyntaxHighlighter::setCommentEndExpression(const QRegExp& commentEndExpression)
54 {
55 m_commentEndExpression = commentEndExpression;
56 }
57
58 void SyntaxHighlighter::setMultiLineCommentFormat(const QTextCharFormat& multiLineCommentFormat)
59 {
60 m_multiLineCommentFormat = multiLineCommentFormat;
61 }
62
63 void SyntaxHighlighter::addRule(const SyntaxHighlighter::Rule& rule)
64 {
65 m_highlightingRules.append(rule);
66 }
67
68 const QRegExp& SyntaxHighlighter::commentStartExpression() const
69 {
70 return m_commentStartExpression;
71 }
72
73 const QRegExp& SyntaxHighlighter::commentEndExpression() const
74 {
75 return m_commentEndExpression;
76 }
77
78 const QTextCharFormat& SyntaxHighlighter::multiLineCommentFormat() const
79 {
80 return m_multiLineCommentFormat;
81 }
82
83 const QVector<SyntaxHighlighter::Rule>& SyntaxHighlighter::rules() const
84 {
85 return m_highlightingRules;
86 }
Something went wrong with that request. Please try again.