2 #include <QXMLHighlighter> 3 #include <QSyntaxStyle> 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"(-->)") 16 << QRegularExpression("<\\?")
17 << QRegularExpression(
"/>")
18 << QRegularExpression(
">")
19 << QRegularExpression(
"<")
20 << QRegularExpression(
"</")
21 << QRegularExpression(
"\\?>");
27 auto matchIterator = m_xmlElementRegex.globalMatch(text);
28 while (matchIterator.hasNext())
30 auto match = matchIterator.next();
33 match.capturedStart(),
34 match.capturedLength(),
35 syntaxStyle()->getFormat(
"Keyword")
41 for (
auto&& regex : m_xmlKeywordRegexes)
44 syntaxStyle()->getFormat(
"Keyword"),
51 syntaxStyle()->getFormat(
"Text"),
56 setCurrentBlockState(0);
59 if (previousBlockState() != 1)
61 startIndex = text.indexOf(m_xmlCommentBeginRegex);
64 while (startIndex >= 0)
66 auto match = m_xmlCommentEndRegex.match(text, startIndex);
68 int endIndex =
match.capturedStart();
69 int commentLength = 0;
73 setCurrentBlockState(1);
74 commentLength = text.length() - startIndex;
78 commentLength = endIndex - startIndex +
match.capturedLength();
84 syntaxStyle()->getFormat(
"Comment")
87 startIndex = text.indexOf(m_xmlCommentBeginRegex, startIndex + commentLength);
91 syntaxStyle()->getFormat(
"String"),
99 auto matchIterator = regex.globalMatch(text);
101 while (matchIterator.hasNext())
103 auto match = matchIterator.next();
106 match.capturedStart(),
107 match.capturedLength(),
void highlightByRegex(const QTextCharFormat &format, const QRegularExpression ®ex, const QString &text)
QXMLHighlighter(QTextDocument *document=nullptr)
Constructor.
Class, that descrubes highlighter with syntax style.
static const char * match(MatchState *ms, const char *s, const char *p)
void highlightBlock(const QString &text) override
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)