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