Debian Codegraph @

src/internal/QPythonHighlighter.cpp

raw symbols SCIP
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 }