24#include "PythonSyntaxHighlighter.hh"
26PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument *parent)
27 : QSyntaxHighlighter(parent)
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";
36 operators = QStringList() <<
"=" <<
38 "==" <<
"!=" <<
"<" <<
"<=" <<
">" <<
">=" <<
40 "\\+" <<
"-" <<
"\\*" <<
"/" <<
"//" <<
"%" <<
"\\*\\*" <<
42 "\\+=" <<
"-=" <<
"\\*=" <<
"/=" <<
"%=" <<
44 "\\^" <<
"\\|" <<
"&" <<
"~" <<
">>" <<
"<<";
46 braces = QStringList() <<
"{" <<
"}" <<
"\\(" <<
"\\)" <<
"\\[" <<
"]";
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"));
59 triSingleQuote.setPattern(
"'''");
60 triDoubleQuote.setPattern(
"\"\"\"");
65void PythonSyntaxHighlighter::initializeRules()
67 foreach (QString currKeyword, keywords)
69 rules.append(
HighlightingRule(QString(
"\\b%1\\b").arg(currKeyword), 0, basicStyles.value(
"keyword")));
71 foreach (QString currOperator, operators)
73 rules.append(
HighlightingRule(QString(
"%1").arg(currOperator), 0, basicStyles.value(
"operator")));
75 foreach (QString currBrace, braces)
77 rules.append(
HighlightingRule(QString(
"%1").arg(currBrace), 0, basicStyles.value(
"brace")));
84 rules.append(
HighlightingRule(
"\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value(
"string")));
87 rules.append(
HighlightingRule(
"'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value(
"string")));
91 rules.append(
HighlightingRule(
"\\bdef\\b\\s*(\\w+)", 1, basicStyles.value(
"defclass")));
94 rules.append(
HighlightingRule(
"\\bclass\\b\\s*(\\w+)", 1, basicStyles.value(
"defclass")));
101 rules.append(
HighlightingRule(
"\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value(
"numbers")));
102 rules.append(
HighlightingRule(
"\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, basicStyles.value(
"numbers")));
103 rules.append(
HighlightingRule(
"\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, basicStyles.value(
"numbers")));
106void PythonSyntaxHighlighter::highlightBlock(
const QString &text)
110 QRegularExpressionMatch match = currRule.pattern.match(text, 0);
111 int idx = match.capturedStart();
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();
123 setCurrentBlockState(0);
126 bool isInMultilne =
matchMultiline(text, triSingleQuote, 1, basicStyles.value(
"string2"));
128 isInMultilne =
matchMultiline(text, triDoubleQuote, 2, basicStyles.value(
"string2"));
138 if (previousBlockState() == inState) {
144 QRegularExpressionMatch match = delimiter.match(text);
145 start = match.capturedStart();
147 add = match.capturedLength();
153 QRegularExpressionMatch match = delimiter.match(text, start + add);
154 int end = match.capturedStart();
157 length = end - start + add + match.capturedLength();
158 setCurrentBlockState(0);
162 setCurrentBlockState(inState);
163 length = text.length() - start + add;
166 setFormat(start, length, style);
167 start = text.indexOf(delimiter, start + length);
170 if (currentBlockState() == inState)
176const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(
const QString &colorName,
const QString &style)
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);
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...