Debian Codegraph @

avogadro/qtgui/generichighlighter.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "generichighlighter.h"
7
8 #include <cassert>
9
10 namespace Avogadro::QtGui {
11
12 GenericHighlighter::GenericHighlighter(QObject* parent_)
13 : QSyntaxHighlighter(parent_)
14 {
15 }
16
17 GenericHighlighter::~GenericHighlighter() {}
18
19 GenericHighlighter::GenericHighlighter(const GenericHighlighter& other)
20 : QSyntaxHighlighter(static_cast<QTextDocument*>(nullptr))
21 {
22 m_rules = other.m_rules;
23 }
24
25 GenericHighlighter& GenericHighlighter::operator=(GenericHighlighter other)
26 {
27 swap(*this, other);
28 return *this;
29 }
30
31 GenericHighlighter& GenericHighlighter::operator+=(
32 const GenericHighlighter& other)
33 {
34 m_rules.append(other.m_rules);
35 return *this;
36 }
37
38 GenericHighlighter::Rule& GenericHighlighter::addRule()
39 {
40 m_rules.push_back(Rule());
41 return m_rules.back();
42 }
43
44 int GenericHighlighter::ruleCount() const
45 {
46 return m_rules.size();
47 }
48
49 GenericHighlighter::Rule& GenericHighlighter::rule(int idx)
50 {
51 assert("idx in bounds" && idx < m_rules.size());
52 return m_rules[idx];
53 }
54
55 const GenericHighlighter::Rule& GenericHighlighter::rule(int idx) const
56 {
57 assert("idx in bounds" && idx < m_rules.size());
58 return m_rules[idx];
59 }
60
61 QList<GenericHighlighter::Rule> GenericHighlighter::rules() const
62 {
63 return m_rules;
64 }
65
66 void GenericHighlighter::highlightBlock(const QString& text)
67 {
68 for (auto& m_rule : m_rules)
69 m_rule.apply(text, *this);
70 }
71
72 void GenericHighlighter::Rule::apply(const QString& text,
73 GenericHighlighter& highlighter)
74 {
75 for (auto& m_pattern : m_patterns) {
76 // each m_pattern is a QRegularExpression
77 // We want to highlight every occurrence of m_pattern
78 QRegularExpressionMatchIterator iterator = m_pattern.globalMatch(text);
79 while (iterator.hasNext()) {
80 QRegularExpressionMatch match = iterator.next();
81 // If using a regex with capture groups defined, we explicitly don't want
82 // to highlight the whole expression, only the capture groups
83 if (m_pattern.captureCount() > 0) {
84 // Iterate over capture groups, skipping the implicit group 0
85 for (int i = 1; i <= match.lastCapturedIndex(); ++i) {
86 QString captured = match.captured(i);
87 if (!captured.isNull()) {
88 // According to StackOverflow user "peppe", who claims to have
89 // written the whole QRegularExpression class, the index returned is
90 // relative to the whole string, not to the current match
91 // https://stackoverflow.com/questions/28725588/qregularexpression-match-position-in-the-source-string
92 int index = match.capturedStart(i);
93 int length = match.capturedLength(i);
94 highlighter.setFormat(index, length, m_format);
95 }
96 }
97 } else {
98 // Straightforward regex with no capture groups, highlight whole match
99 int index = match.capturedStart(0);
100 int length = match.capturedLength(0);
101 highlighter.setFormat(index, length, m_format);
102 }
103 }
104 }
105 }
106
107 void GenericHighlighter::Rule::addPattern(const QRegularExpression& regexp)
108 {
109 m_patterns.append(regexp);
110 }
111
112 void GenericHighlighter::Rule::setFormat(const QTextCharFormat& format)
113 {
114 m_format = format;
115 }
116
117 } // namespace Avogadro::QtGui