Developer Documentation
Loading...
Searching...
No Matches
PythonSyntaxHighlighter.cc
1/*
2This is a C++ port of the following PyQt example
3http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
4C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
5
6The following free software license applies for this file ("X11 license"):
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9and associated documentation files (the "Software"), to deal in the Software without restriction,
10including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
11and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all copies or substantial
15portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24#include "PythonSyntaxHighlighter.hh"
25
26PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument *parent)
27 : QSyntaxHighlighter(parent)
28{
29 keywords = QStringList() << "and" << "assert" << "break" << "class" << "continue" << "def" <<
30 "del" << "elif" << "else" << "except" << "exec" << "finally" <<
31 "for" << "from" << "global" << "if" << "import" << "in" <<
32 "is" << "lambda" << "not" << "or" << "pass" << "print" <<
33 "raise" << "return" << "try" << "while" << "yield" <<
34 "None" << "True" << "False";
35
36 operators = QStringList() << "=" <<
37 // Comparison
38 "==" << "!=" << "<" << "<=" << ">" << ">=" <<
39 // Arithmetic
40 "\\+" << "-" << "\\*" << "/" << "//" << "%" << "\\*\\*" <<
41 // In-place
42 "\\+=" << "-=" << "\\*=" << "/=" << "%=" <<
43 // Bitwise
44 "\\^" << "\\|" << "&" << "~" << ">>" << "<<";
45
46 braces = QStringList() << "{" << "}" << "\\(" << "\\)" << "\\[" << "]";
47
48 basicStyles.insert("keyword", getTextCharFormat("blue"));
49 basicStyles.insert("operator", getTextCharFormat("red"));
50 basicStyles.insert("brace", getTextCharFormat("darkGray"));
51 basicStyles.insert("defclass", getTextCharFormat("black", "bold"));
52 basicStyles.insert("brace", getTextCharFormat("darkGray"));
53 basicStyles.insert("string", getTextCharFormat("magenta"));
54 basicStyles.insert("string2", getTextCharFormat("darkMagenta"));
55 basicStyles.insert("comment", getTextCharFormat("darkGreen", "italic"));
56 basicStyles.insert("self", getTextCharFormat("black", "italic"));
57 basicStyles.insert("numbers", getTextCharFormat("brown"));
58
59 triSingleQuote.setPattern("'''");
60 triDoubleQuote.setPattern("\"\"\"");
61
62 initializeRules();
63}
64
65void PythonSyntaxHighlighter::initializeRules()
66{
67 foreach (QString currKeyword, keywords)
68 {
69 rules.append(HighlightingRule(QString("\\b%1\\b").arg(currKeyword), 0, basicStyles.value("keyword")));
70 }
71 foreach (QString currOperator, operators)
72 {
73 rules.append(HighlightingRule(QString("%1").arg(currOperator), 0, basicStyles.value("operator")));
74 }
75 foreach (QString currBrace, braces)
76 {
77 rules.append(HighlightingRule(QString("%1").arg(currBrace), 0, basicStyles.value("brace")));
78 }
79 // 'self'
80 rules.append(HighlightingRule("\\bself\\b", 0, basicStyles.value("self")));
81
82 // Double-quoted string, possibly containing escape sequences
83 // FF: originally in python : r'"[^"\\]*(\\.[^"\\]*)*"'
84 rules.append(HighlightingRule("\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value("string")));
85 // Single-quoted string, possibly containing escape sequences
86 // FF: originally in python : r"'[^'\\]*(\\.[^'\\]*)*'"
87 rules.append(HighlightingRule("'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value("string")));
88
89 // 'def' followed by an identifier
90 // FF: originally: r'\bdef\b\s*(\w+)'
91 rules.append(HighlightingRule("\\bdef\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));
92 // 'class' followed by an identifier
93 // FF: originally: r'\bclass\b\s*(\w+)'
94 rules.append(HighlightingRule("\\bclass\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));
95
96 // From '#' until a newline
97 // FF: originally: r'#[^\\n]*'
98 rules.append(HighlightingRule("#[^\\n]*", 0, basicStyles.value("comment")));
99
100 // Numeric literals
101 rules.append(HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?[0-9]+[lL]?\b'
102 rules.append(HighlightingRule("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b'
103 rules.append(HighlightingRule("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b'
104}
105
106void PythonSyntaxHighlighter::highlightBlock(const QString &text)
107{
108 foreach (HighlightingRule currRule, rules)
109 {
110 QRegularExpressionMatch match = currRule.pattern.match(text, 0);
111 int idx = match.capturedStart();
112 while (idx >= 0)
113 {
114 // Get index of Nth match
115 idx = match.capturedStart(currRule.nth);
116 int length = match.capturedLength(currRule.nth);
117 setFormat(idx, length, currRule.format);
118 match = currRule.pattern.match(text, idx + length);
119 idx = match.capturedStart();
120 }
121 }
122
123 setCurrentBlockState(0);
124
125 // Do multi-line strings
126 bool isInMultilne = matchMultiline(text, triSingleQuote, 1, basicStyles.value("string2"));
127 if (!isInMultilne)
128 isInMultilne = matchMultiline(text, triDoubleQuote, 2, basicStyles.value("string2"));
129}
130
131bool PythonSyntaxHighlighter::matchMultiline(const QString &text, const QRegularExpression &delimiter, const int inState, const QTextCharFormat &style)
132{
133 int start = -1;
134 int add = -1;
135 int length = 0;
136
137 // If inside triple-single quotes, start at 0
138 if (previousBlockState() == inState) {
139 start = 0;
140 add = 0;
141 }
142 // Otherwise, look for the delimiter on this line
143 else {
144 QRegularExpressionMatch match = delimiter.match(text);
145 start = match.capturedStart();
146 // Move past this match
147 add = match.capturedLength();
148 }
149
150 // As long as there's a delimiter match on this line...
151 while (start >= 0) {
152 // Look for the ending delimiter
153 QRegularExpressionMatch match = delimiter.match(text, start + add);
154 int end = match.capturedStart();
155 // Ending delimiter on this line?
156 if (end >= add) {
157 length = end - start + add + match.capturedLength();
158 setCurrentBlockState(0);
159 }
160 // No; multi-line string
161 else {
162 setCurrentBlockState(inState);
163 length = text.length() - start + add;
164 }
165 // Apply formatting and look for next
166 setFormat(start, length, style);
167 start = text.indexOf(delimiter, start + length);
168 }
169 // Return True if still inside a multi-line string, False otherwise
170 if (currentBlockState() == inState)
171 return true;
172 else
173 return false;
174}
175
176const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(const QString &colorName, const QString &style)
177{
178 QTextCharFormat charFormat;
179 QColor color(colorName);
180 charFormat.setForeground(color);
181 if (style.contains("bold", Qt::CaseInsensitive))
182 charFormat.setFontWeight(QFont::Bold);
183 if (style.contains("italic", Qt::CaseInsensitive))
184 charFormat.setFontItalic(true);
185 return charFormat;
186}
Container to describe a highlighting rule. Based on a regular expression, a relevant match # and the ...
bool matchMultiline(const QString &text, const QRegularExpression &delimiter, const int inState, const QTextCharFormat &style)
Highlighst multi-line strings, returns true if after processing we are still within the multi-line se...