QXMLHighlighter.cpp
Go to the documentation of this file.
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  {
44  syntaxStyle()->getFormat("Keyword"),
45  regex,
46  text
47  );
48  }
49 
51  syntaxStyle()->getFormat("Text"),
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 
91  syntaxStyle()->getFormat("String"),
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 }
QXMLHighlighter::m_xmlAttributeRegex
QRegularExpression m_xmlAttributeRegex
Definition: QXMLHighlighter.hpp:37
sol::load_mode::text
@ text
QStyleSyntaxHighlighter::syntaxStyle
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
Definition: QStyleSyntaxHighlighter.cpp:16
QXMLHighlighter::highlightBlock
void highlightBlock(const QString &text) override
QXMLHighlighter::highlightByRegex
void highlightByRegex(const QTextCharFormat &format, const QRegularExpression &regex, const QString &text)
format
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
Definition: color.h:543
QXMLHighlighter::m_xmlKeywordRegexes
QVector< QRegularExpression > m_xmlKeywordRegexes
Definition: QXMLHighlighter.hpp:35
QXMLHighlighter::QXMLHighlighter
QXMLHighlighter(QTextDocument *document=nullptr)
Constructor.
QStyleSyntaxHighlighter
Class, that descrubes highlighter with syntax style.
Definition: QStyleSyntaxHighlighter.hpp:12
QXMLHighlighter::m_xmlCommentBeginRegex
QRegularExpression m_xmlCommentBeginRegex
Definition: QXMLHighlighter.hpp:39
match
static const char * match(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.c:570
QXMLHighlighter::m_xmlCommentEndRegex
QRegularExpression m_xmlCommentEndRegex
Definition: QXMLHighlighter.hpp:40
QXMLHighlighter::m_xmlValueRegex
QRegularExpression m_xmlValueRegex
Definition: QXMLHighlighter.hpp:38
QXMLHighlighter::m_xmlElementRegex
QRegularExpression m_xmlElementRegex
Definition: QXMLHighlighter.hpp:36


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46