| 1 | // QCodeEditor |
| 2 | #include <QXMLHighlighter> |
| 3 | #include <QSyntaxStyle> |
| 4 | |
| 5 | |
| 6 | QXMLHighlighter::QXMLHighlighter(QTextDocument* document) : |
| 7 | QStyleSyntaxHighlighter(document), |
| 8 | m_xmlKeywordRegexes (), |
| 9 | m_xmlElementRegex (R"(<[\s]*[/]?[\s]*([^\n][a-zA-Z-_:]*)(?=[\s/>]))"), |
| 10 | m_xmlAttributeRegex (R"(\w+(?=\=))"), |
| 11 | m_xmlValueRegex (R"("[^\n"]+"(?=\??[\s/>]))"), |
| 12 | m_xmlCommentBeginRegex(R"(<!--)"), |
| 13 | m_xmlCommentEndRegex (R"(-->)") |
| 14 | { |
| 15 | m_xmlKeywordRegexes |
| 16 | << QRegularExpression("<\\?") |
| 17 | << QRegularExpression("/>") |
| 18 | << QRegularExpression(">") |
| 19 | << QRegularExpression("<") |
| 20 | << QRegularExpression("</") |
| 21 | << QRegularExpression("\\?>"); |
| 22 | } |
| 23 | |
| 24 | void QXMLHighlighter::highlightBlock(const QString& text) |
| 25 | { |
| 26 | // Special treatment for xml element regex as we use captured text to emulate lookbehind |
| 27 | auto matchIterator = m_xmlElementRegex.globalMatch(text); |
| 28 | while (matchIterator.hasNext()) |
| 29 | { |
| 30 | auto match = matchIterator.next(); |
| 31 | |
| 32 | setFormat( |
| 33 | match.capturedStart(), |
| 34 | match.capturedLength(), |
| 35 | syntaxStyle()->getFormat("Keyword") // XML ELEMENT FORMAT |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | // Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element |
| 40 | |
| 41 | for (auto&& regex : m_xmlKeywordRegexes) |
| 42 | { |
| 43 | highlightByRegex( |
| 44 | syntaxStyle()->getFormat("Keyword"), |
| 45 | regex, |
| 46 | text |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | highlightByRegex( |
| 51 | syntaxStyle()->getFormat("Text"), |
| 52 | m_xmlAttributeRegex, |
| 53 | text |
| 54 | ); |
| 55 | |
| 56 | setCurrentBlockState(0); |
| 57 | |
| 58 | int startIndex = 0; |
| 59 | if (previousBlockState() != 1) |
| 60 | { |
| 61 | startIndex = text.indexOf(m_xmlCommentBeginRegex); |
| 62 | } |
| 63 | |
| 64 | while (startIndex >= 0) |
| 65 | { |
| 66 | auto match = m_xmlCommentEndRegex.match(text, startIndex); |
| 67 | |
| 68 | int endIndex = match.capturedStart(); |
| 69 | int commentLength = 0; |
| 70 | |
| 71 | if (endIndex == -1) |
| 72 | { |
| 73 | setCurrentBlockState(1); |
| 74 | commentLength = text.length() - startIndex; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | commentLength = endIndex - startIndex + match.capturedLength(); |
| 79 | } |
| 80 | |
| 81 | setFormat( |
| 82 | startIndex, |
| 83 | commentLength, |
| 84 | syntaxStyle()->getFormat("Comment") |
| 85 | ); |
| 86 | |
| 87 | startIndex = text.indexOf(m_xmlCommentBeginRegex, startIndex + commentLength); |
| 88 | } |
| 89 | |
| 90 | highlightByRegex( |
| 91 | syntaxStyle()->getFormat("String"), |
| 92 | m_xmlValueRegex, |
| 93 | text |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | void QXMLHighlighter::highlightByRegex(const QTextCharFormat& format, const QRegularExpression& regex, const QString& text) |
| 98 | { |
| 99 | auto matchIterator = regex.globalMatch(text); |
| 100 | |
| 101 | while (matchIterator.hasNext()) |
| 102 | { |
| 103 | auto match = matchIterator.next(); |
| 104 | |
| 105 | setFormat( |
| 106 | match.capturedStart(), |
| 107 | match.capturedLength(), |
| 108 | format |
| 109 | ); |
| 110 | } |
| 111 | } |