-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlSyntaxHighlighter.cpp
91 lines (74 loc) · 3.04 KB
/
XmlSyntaxHighlighter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "XmlSyntaxHighlighter.h"
XMLSyntaxHighlighter::XMLSyntaxHighlighter(QObject * parent) :
QSyntaxHighlighter(parent)
{
setRegexes();
setFormats();
}
XMLSyntaxHighlighter::XMLSyntaxHighlighter(QTextDocument * parent) :
QSyntaxHighlighter(parent)
{
setRegexes();
setFormats();
}
XMLSyntaxHighlighter::XMLSyntaxHighlighter(QTextEdit * parent) :
QSyntaxHighlighter(parent)
{
setRegexes();
setFormats();
}
void XMLSyntaxHighlighter::highlightBlock(const QString & text)
{
// Special treatment for xml element regex as we use captured text to emulate lookbehind
int xmlElementIndex = m_xmlElementRegex.indexIn(text);
while(xmlElementIndex >= 0)
{
int matchedPos = m_xmlElementRegex.pos(1);
int matchedLength = m_xmlElementRegex.cap(1).length();
setFormat(matchedPos, matchedLength, m_xmlElementFormat);
xmlElementIndex = m_xmlElementRegex.indexIn(text, matchedPos + matchedLength);
}
// Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element
typedef QList<QRegExp>::const_iterator Iter;
Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.end();
for(Iter it = m_xmlKeywordRegexes.begin(); it != xmlKeywordRegexesEnd; ++it) {
const QRegExp & regex = *it;
highlightByRegex(m_xmlKeywordFormat, regex, text);
}
highlightByRegex(m_xmlAttributeFormat, m_xmlAttributeRegex, text);
highlightByRegex(m_xmlCommentFormat, m_xmlCommentRegex, text);
highlightByRegex(m_xmlValueFormat, m_xmlValueRegex, text);
}
void XMLSyntaxHighlighter::highlightByRegex(const QTextCharFormat & format,
const QRegExp & regex, const QString & text)
{
int index = regex.indexIn(text);
while(index >= 0)
{
int matchedLength = regex.matchedLength();
setFormat(index, matchedLength, format);
index = regex.indexIn(text, index + matchedLength);
}
}
void XMLSyntaxHighlighter::setRegexes()
{
m_xmlElementRegex.setPattern("<[?\\s]*[/]?[\\s]*([^\\n][^>]*)(?=[\\s/>])");
m_xmlAttributeRegex.setPattern("\\w+(?=\\=)");
m_xmlValueRegex.setPattern("\"[^\\n\"]+\"(?=[?\\s/>])");
m_xmlCommentRegex.setPattern("<!--[^\\n]*-->");
m_xmlKeywordRegexes = QList<QRegExp>() << QRegExp("<\\?") << QRegExp("/>")
<< QRegExp(">") << QRegExp("<") << QRegExp("</")
<< QRegExp("\\?>");
}
void XMLSyntaxHighlighter::setFormats()
{
m_xmlKeywordFormat.setForeground(Qt::blue);
m_xmlKeywordFormat.setFontWeight(QFont::Bold);
m_xmlElementFormat.setForeground(Qt::darkMagenta);
m_xmlElementFormat.setFontWeight(QFont::Bold);
m_xmlAttributeFormat.setForeground(Qt::darkGreen);
m_xmlAttributeFormat.setFontWeight(QFont::Bold);
m_xmlAttributeFormat.setFontItalic(true);
m_xmlValueFormat.setForeground(Qt::darkRed);
m_xmlCommentFormat.setForeground(Qt::gray);
}