QJSONHighlighter.cpp
Go to the documentation of this file.
1 // QCodeEditor
2 #include <QJSONHighlighter>
3 #include <QSyntaxStyle>
4 
5 
6 QJSONHighlighter::QJSONHighlighter(QTextDocument* document) :
7  QStyleSyntaxHighlighter(document),
8  m_highlightRules(),
9  m_keyRegex(R"(("[^\r\n:]+?")\s*:)")
10 {
11  auto keywords = QStringList()
12  << "null" << "true" << "false";
13 
14  for (auto&& keyword : keywords)
15  {
16  m_highlightRules.append({
17  QRegularExpression(QString(R"(\b%1\b)").arg(keyword)),
18  "Keyword"
19  });
20  }
21 
22  // Numbers
23  m_highlightRules.append({
24  QRegularExpression(R"(\b(0b|0x){0,1}[\d.']+\b)"),
25  "Number"
26  });
27 
28  // Strings
29  m_highlightRules.append({
30  QRegularExpression(R"("[^\n"]*")"),
31  "String"
32  });
33 }
34 
35 void QJSONHighlighter::highlightBlock(const QString& text)
36 {
37  for (auto&& rule : m_highlightRules)
38  {
39  auto matchIterator = rule.pattern.globalMatch(text);
40 
41  while (matchIterator.hasNext())
42  {
43  auto match = matchIterator.next();
44 
45  setFormat(
46  match.capturedStart(),
47  match.capturedLength(),
48  syntaxStyle()->getFormat(rule.formatName)
49  );
50  }
51  }
52 
53  // Special treatment for key regex
54  auto matchIterator = m_keyRegex.globalMatch(text);
55 
56  while (matchIterator.hasNext())
57  {
58  auto match = matchIterator.next();
59 
60  setFormat(
61  match.capturedStart(1),
62  match.capturedLength(1),
63  syntaxStyle()->getFormat("Keyword")
64  );
65  }
66 }
QJSONHighlighter::m_highlightRules
QVector< QHighlightRule > m_highlightRules
Definition: QJSONHighlighter.hpp:30
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
QJSONHighlighter::QJSONHighlighter
QJSONHighlighter(QTextDocument *document=nullptr)
Constructor.
Definition: QJSONHighlighter.cpp:6
QStyleSyntaxHighlighter::syntaxStyle
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
Definition: QStyleSyntaxHighlighter.cpp:16
QStyleSyntaxHighlighter
Class, that descrubes highlighter with syntax style.
Definition: QStyleSyntaxHighlighter.hpp:12
QJSONHighlighter::highlightBlock
void highlightBlock(const QString &text) override
match
static const char * match(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.c:570
QJSONHighlighter::m_keyRegex
QRegularExpression m_keyRegex
Definition: QJSONHighlighter.hpp:31


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