QPythonHighlighter.cpp
Go to the documentation of this file.
1 // QCodeEditor
2 #include <QPythonHighlighter>
3 #include <QLanguage>
4 #include <QSyntaxStyle>
5 
6 // Qt
7 #include <QFile>
8 #include <QDebug>
9 
10 QPythonHighlighter::QPythonHighlighter(QTextDocument* document) :
11  QStyleSyntaxHighlighter(document),
12  m_highlightRules (),
13  m_highlightBlockRules(),
14  m_includePattern (QRegularExpression(R"(import \w+)")),
15  m_functionPattern (QRegularExpression(R"(\b([A-Za-z0-9_]+(?:\.))*([A-Za-z0-9_]+)(?=\())")),
16  m_defTypePattern (QRegularExpression(R"(\b([A-Za-z0-9_]+)\s+[A-Za-z]{1}[A-Za-z0-9_]+\s*[;=])"))
17 {
18  Q_INIT_RESOURCE(qcodeeditor_resources);
19  QFile fl(":/languages/python.xml");
20 
21  if (!fl.open(QIODevice::ReadOnly))
22  {
23  return;
24  }
25 
26  QLanguage language(&fl);
27 
28  if (!language.isLoaded())
29  {
30  return;
31  }
32 
33  auto keys = language.keys();
34  for (auto&& key : keys)
35  {
36  auto names = language.names(key);
37  for (auto&& name : names)
38  {
39  m_highlightRules.append({
40  QRegularExpression(QString(R"(\b%1\b)").arg(name)),
41  key
42  });
43  }
44  }
45 
46  // Following rules has higher priority to display
47  // than language specific keys
48  // So they must be applied at last.
49  // Numbers
50  m_highlightRules.append({
51  QRegularExpression(R"(\b(0b|0x){0,1}[\d.']+\b)"),
52  "Number"
53  });
54 
55  // Strings
56  m_highlightRules.append({
57  QRegularExpression(R"("[^\n"]*")"),
58  "String"
59  });
60  m_highlightRules.append({
61  QRegularExpression(R"('[^\n"]*')"),
62  "String"
63  });
64 
65  // Single line comment
66  m_highlightRules.append({
67  QRegularExpression("#[^\n]*"),
68  "Comment"
69  });
70 
71  // Multiline string
72  m_highlightBlockRules.append({
73  QRegularExpression("(''')"),
74  QRegularExpression("(''')"),
75  "String"
76  });
77  m_highlightBlockRules.append({
78  QRegularExpression("(\"\"\")"),
79  QRegularExpression("(\"\"\")"),
80  "String"
81  });
82 }
83 
84 void QPythonHighlighter::highlightBlock(const QString& text)
85 {
86  // Checking for function
87  {
88  auto matchIterator = m_functionPattern.globalMatch(text);
89 
90  while (matchIterator.hasNext())
91  {
92  auto match = matchIterator.next();
93 
94  setFormat(
95  match.capturedStart(),
96  match.capturedLength(),
97  syntaxStyle()->getFormat("Type")
98  );
99 
100  setFormat(
101  match.capturedStart(2),
102  match.capturedLength(2),
103  syntaxStyle()->getFormat("Function")
104  );
105  }
106  }
107 
108  for (auto& rule : m_highlightRules)
109  {
110  auto matchIterator = rule.pattern.globalMatch(text);
111 
112  while (matchIterator.hasNext())
113  {
114  auto match = matchIterator.next();
115 
116  setFormat(
117  match.capturedStart(),
118  match.capturedLength(),
119  syntaxStyle()->getFormat(rule.formatName)
120  );
121  }
122  }
123 
124  setCurrentBlockState(0);
125  int startIndex = 0;
126  int highlightRuleId = previousBlockState();
127  if (highlightRuleId < 1 || highlightRuleId > m_highlightBlockRules.size()) {
128  for(int i = 0; i < m_highlightBlockRules.size(); ++i) {
129  startIndex = text.indexOf(m_highlightBlockRules.at(i).startPattern);
130 
131  if (startIndex >= 0) {
132  highlightRuleId = i + 1;
133  break;
134  }
135  }
136  }
137 
138  while (startIndex >= 0)
139  {
140  const auto &blockRules = m_highlightBlockRules.at(highlightRuleId - 1);
141  auto match = blockRules.endPattern.match(text, startIndex+1); // Should be + length of start pattern
142 
143  int endIndex = match.capturedStart();
144  int matchLength = 0;
145 
146  if (endIndex == -1)
147  {
148  setCurrentBlockState(highlightRuleId);
149  matchLength = text.length() - startIndex;
150  }
151  else
152  {
153  matchLength = endIndex - startIndex + match.capturedLength();
154  }
155 
156  setFormat(
157  startIndex,
158  matchLength,
159  syntaxStyle()->getFormat(blockRules.formatName)
160  );
161  startIndex = text.indexOf(blockRules.startPattern, startIndex + matchLength);
162  }
163 }
QPythonHighlighter::QPythonHighlighter
QPythonHighlighter(QTextDocument *document=nullptr)
Constructor.
Definition: QPythonHighlighter.cpp:10
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
QPythonHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition: QPythonHighlighter.cpp:84
QLanguage::names
QStringList names(const QString &key)
Method for getting names from key.
Definition: QLanguage.cpp:74
QPythonHighlighter::m_highlightRules
QVector< QHighlightRule > m_highlightRules
Definition: QPythonHighlighter.hpp:34
QStyleSyntaxHighlighter::syntaxStyle
QSyntaxStyle * syntaxStyle() const
Method for getting syntax style.
Definition: QStyleSyntaxHighlighter.cpp:16
QLanguage::keys
QStringList keys()
Method for getting available keys.
Definition: QLanguage.cpp:69
QPythonHighlighter::m_functionPattern
QRegularExpression m_functionPattern
Definition: QPythonHighlighter.hpp:38
QPythonHighlighter::m_highlightBlockRules
QVector< QHighlightBlockRule > m_highlightBlockRules
Definition: QPythonHighlighter.hpp:35
QStyleSyntaxHighlighter
Class, that descrubes highlighter with syntax style.
Definition: QStyleSyntaxHighlighter.hpp:12
QLanguage::isLoaded
bool isLoaded() const
Method for getting is object loaded.
Definition: QLanguage.cpp:79
QLanguage
Definition: QLanguage.hpp:14
match
static const char * match(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.c:570


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