|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | +
|
| 4 | + * Copyright (c) 2015 Dmitry Ivanov |
| 5 | +
|
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "BasicXMLSyntaxHighlighter.h" |
| 26 | + |
| 27 | +BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QObject * parent) : |
| 28 | + QSyntaxHighlighter(parent) |
| 29 | +{ |
| 30 | + setRegexes(); |
| 31 | + setFormats(); |
| 32 | +} |
| 33 | + |
| 34 | +BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QTextDocument * parent) : |
| 35 | + QSyntaxHighlighter(parent) |
| 36 | +{ |
| 37 | + setRegexes(); |
| 38 | + setFormats(); |
| 39 | +} |
| 40 | + |
| 41 | +BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QTextEdit * parent) : |
| 42 | + QSyntaxHighlighter(parent) |
| 43 | +{ |
| 44 | + setRegexes(); |
| 45 | + setFormats(); |
| 46 | +} |
| 47 | + |
| 48 | +void BasicXMLSyntaxHighlighter::highlightBlock(const QString & text) |
| 49 | +{ |
| 50 | + // Special treatment for xml element regex as we use captured text to emulate lookbehind |
| 51 | + int xmlElementIndex = m_xmlElementRegex.indexIn(text); |
| 52 | + while(xmlElementIndex >= 0) |
| 53 | + { |
| 54 | + int matchedPos = m_xmlElementRegex.pos(1); |
| 55 | + int matchedLength = m_xmlElementRegex.cap(1).length(); |
| 56 | + setFormat(matchedPos, matchedLength, m_xmlElementFormat); |
| 57 | + |
| 58 | + xmlElementIndex = m_xmlElementRegex.indexIn(text, matchedPos + matchedLength); |
| 59 | + } |
| 60 | + |
| 61 | + // Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element |
| 62 | + typedef QList<QRegExp>::const_iterator Iter; |
| 63 | + Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.end(); |
| 64 | + for(Iter it = m_xmlKeywordRegexes.begin(); it != xmlKeywordRegexesEnd; ++it) { |
| 65 | + const QRegExp & regex = *it; |
| 66 | + highlightByRegex(m_xmlKeywordFormat, regex, text); |
| 67 | + } |
| 68 | + |
| 69 | + highlightByRegex(m_xmlAttributeFormat, m_xmlAttributeRegex, text); |
| 70 | + highlightByRegex(m_xmlCommentFormat, m_xmlCommentRegex, text); |
| 71 | + highlightByRegex(m_xmlValueFormat, m_xmlValueRegex, text); |
| 72 | +} |
| 73 | + |
| 74 | +void BasicXMLSyntaxHighlighter::highlightByRegex(const QTextCharFormat & format, |
| 75 | + const QRegExp & regex, const QString & text) |
| 76 | +{ |
| 77 | + int index = regex.indexIn(text); |
| 78 | + |
| 79 | + while(index >= 0) |
| 80 | + { |
| 81 | + int matchedLength = regex.matchedLength(); |
| 82 | + setFormat(index, matchedLength, format); |
| 83 | + |
| 84 | + index = regex.indexIn(text, index + matchedLength); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void BasicXMLSyntaxHighlighter::setRegexes() |
| 89 | +{ |
| 90 | + m_xmlElementRegex.setPattern("<[?\\s]*[/]?[\\s]*([^\\n][^>]*)(?=[\\s/>])"); |
| 91 | + m_xmlAttributeRegex.setPattern("\\w+(?=\\=)"); |
| 92 | + m_xmlValueRegex.setPattern("\"[^\\n\"]+\"(?=[?\\s/>])"); |
| 93 | + m_xmlCommentRegex.setPattern("<!--[^\\n]*-->"); |
| 94 | + |
| 95 | + m_xmlKeywordRegexes = QList<QRegExp>() << QRegExp("<\\?") << QRegExp("/>") |
| 96 | + << QRegExp(">") << QRegExp("<") << QRegExp("</") |
| 97 | + << QRegExp("\\?>"); |
| 98 | +} |
| 99 | + |
| 100 | +void BasicXMLSyntaxHighlighter::setFormats() |
| 101 | +{ |
| 102 | + m_xmlKeywordFormat.setForeground(Qt::blue); |
| 103 | + m_xmlKeywordFormat.setFontWeight(QFont::Bold); |
| 104 | + |
| 105 | + m_xmlElementFormat.setForeground(Qt::darkMagenta); |
| 106 | + m_xmlElementFormat.setFontWeight(QFont::Bold); |
| 107 | + |
| 108 | + m_xmlAttributeFormat.setForeground(Qt::darkGreen); |
| 109 | + m_xmlAttributeFormat.setFontWeight(QFont::Bold); |
| 110 | + m_xmlAttributeFormat.setFontItalic(true); |
| 111 | + |
| 112 | + m_xmlValueFormat.setForeground(Qt::darkRed); |
| 113 | + |
| 114 | + m_xmlCommentFormat.setForeground(Qt::gray); |
| 115 | +} |
0 commit comments