48 const QString start_pattern(
"<%1.*?/?>");
50 rule.start = QRegularExpression(start_pattern.arg(tag));
51 rule.end = QRegularExpression(QString(
"</%1>|<%1[^>]*?/>").arg(tag));
53 if (!parent.isEmpty())
55 QString parent_start = start_pattern.arg(parent);
56 rule.parent = std::find_if(rules_.begin(), rules_.end(), [&](
const std::pair<int, Rule>& rule) {
57 return rule.second.start.pattern() == parent_start;
61 rule.parent = rules_.end();
63 rules_.insert(std::make_pair(rules_.size(), rule));
66XmlSyntaxHighlighter::Rules::const_iterator
67XmlSyntaxHighlighter::highlight(Rules::const_iterator active, QStringRef text,
int start,
bool search_end,
int& end)
74 auto match = active->second.end.match(text);
76 end = match.hasMatch() ? match.capturedEnd() : text.size();
77 setFormat(start, end, active->second.format);
80 text = text.left(match.capturedStart());
81 next = active->second.parent;
90 for (
auto it = rules_.begin(); it != rules_.end(); ++it)
92 const auto& rule = it->second;
93 if (rule.parent != active)
99 auto match = rule.start.match(text, offset);
100 if (!match.hasMatch())
103 offset = match.capturedEnd() - match.capturedStart();
104 auto result = highlight(it,
text.mid(match.capturedStart()), start + match.capturedStart(),
true, offset);
106 offset += match.capturedStart();
109 assert(next == active || next == active->second.parent);
110 assert(offset ==
text.size());
122 Rules::const_iterator active = previousBlockState() < 0 ? rules_.end() : rules_.find(previousBlockState());
124 active = highlight(active, QStringRef(&text, 0, text.size()), 0, active != rules_.cend(), unused);
125 setCurrentBlockState(active != rules_.cend() ? active->first : -1);
void highlightBlock(const QString &text) override
void addTag(const QString &tag, const QTextCharFormat &format, const QString &parent=QString())
XmlSyntaxHighlighter(QTextDocument *parent=nullptr)