24 #include "PythonSyntaxHighlighter.hh" 26 PythonSyntaxHighlighter::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(
"\"\"\"");
65 void 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")));
106 void PythonSyntaxHighlighter::highlightBlock(
const QString &text)
110 int idx = currRule.pattern.indexIn(text, 0);
114 idx = currRule.pattern.pos(currRule.nth);
115 int length = currRule.pattern.cap(currRule.nth).length();
116 setFormat(idx, length, currRule.format);
117 idx = currRule.pattern.indexIn(text, idx + length);
121 setCurrentBlockState(0);
124 bool isInMultilne = matchMultiline(text, triSingleQuote, 1, basicStyles.value(
"string2"));
126 isInMultilne = matchMultiline(text, triDoubleQuote, 2, basicStyles.value(
"string2"));
136 if (previousBlockState() == inState) {
142 start = delimiter.indexIn(text);
144 add = delimiter.matchedLength();
150 int end = delimiter.indexIn(text, start + add);
153 length = end - start + add + delimiter.matchedLength();
154 setCurrentBlockState(0);
158 setCurrentBlockState(inState);
159 length = text.length() - start + add;
162 setFormat(start, length, style);
163 start = delimiter.indexIn(text, start + length);
166 if (currentBlockState() == inState)
172 const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(
const QString &colorName,
const QString &style)
174 QTextCharFormat charFormat;
175 QColor color(colorName);
176 charFormat.setForeground(color);
177 if (style.contains(
"bold", Qt::CaseInsensitive))
178 charFormat.setFontWeight(QFont::Bold);
179 if (style.contains(
"italic", Qt::CaseInsensitive))
180 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 QRegExp &delimiter, const int inState, const QTextCharFormat &style)
Highlighst multi-line strings, returns true if after processing we are still within the multi-line se...