30 HighlightingRule rule;
32 rule.mPattern =
QRegularExpression(
"\\b(and|as|assert|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|"
33 "nonlocal|not|or|pass|raise|return|try|while|with|yield|False|None|True)\\b");
34 rule.mPattern.optimize();
36 mHighlightingRules.append(rule);
38 rule.mPattern =
QRegularExpression(
"=|==|!=|<|<=|>|>=|\\+|-|\\*|/|//|%|\\*\\*|\\+=|-=|\\*=|/=|%=|\\^|\\||&|~|>>|<<");
39 rule.mPattern.optimize();
41 mHighlightingRules.append(rule);
44 rule.mPattern.optimize();
46 mHighlightingRules.append(rule);
49 rule.mPattern.optimize();
51 mHighlightingRules.append(rule);
54 rule.mPattern.optimize();
56 mHighlightingRules.append(rule);
59 rule.mPattern.optimize();
61 mHighlightingRules.append(rule);
63 rule.mPattern =
QRegularExpression(
"(\\b([1-9][0-9]*|0)[.]?[0-9]*[eE][+-]?[0-9]+[jJ]?\\b)|"
64 "(\\b([1-9][0-9]*|0)\\.[0-9]*[jJ]?)|"
65 "(\\b0[xX][0-9A-Fa-f]+[lL]?\\b)|"
66 "(\\b0[oO][0-7]+[lL]?\\b)|"
67 "(\\b([1-9][0-9]*|0)[lLjJ]?\\b)");
69 rule.mPattern.optimize();
71 mHighlightingRules.append(rule);
90 const QString skip_strings = R
"(\G(?:[^"'#\\]|\\.|"""(?:[^"\\]|\\.|"(?!""))*"""|'''(?:[^'\\]|\\.|'(?!''))*'''|"(?!"")(?:[^"\\]|\\.)*"|'(?!'')(?:[^'\\]|\\.)*')*\K)";
92 mSingleLineComment.optimize();
94 mMultiLineCommentDelimiter.optimize();
97 void PythonSyntaxHighlighter::highlightBlock(
const QString& text)
99 for (
const HighlightingRule& rule : mHighlightingRules)
107 setFormat(
index, length, rule.mFormat);
115 int current_index = 0;
117 if (previousBlockState() == 1)
119 current_index = closeMultilineComment(text, mTrippleSingleQuote);
121 if (current_index < 0)
123 setCurrentBlockState(1);
128 if (previousBlockState() == 2)
130 current_index = closeMultilineComment(text, mTrippleDoubleQuote);
132 if (current_index < 0)
134 setCurrentBlockState(2);
139 int comment_index = mSingleLineComment.match(text, current_index).capturedStart();
140 if (comment_index >= 0)
153 QChar comment_type = text.
at(comment_start_index);
155 if (comment_type ==
'\'')
160 if (single_match_index < 0)
162 setCurrentBlockState(1);
169 match = mMultiLineCommentDelimiter.match(text, single_match_index + 3);
177 if (double_match_index < 0)
179 setCurrentBlockState(2);
186 match = mMultiLineCommentDelimiter.match(text, double_match_index + 3);
190 setCurrentBlockState(0);
194 int PythonSyntaxHighlighter::closeMultilineComment(
const QString& text,
const QRegularExpression& delimiter,
const int offset)
PythonSyntaxHighlighter(QTextDocument *parent=nullptr)
std::unique_ptr< BasePluginInterface > instance
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
int capturedEnd(int nth) const const
int capturedLength(int nth) const const
int capturedStart(int nth) const const
bool hasMatch() const const
bool hasNext() const const
QRegularExpressionMatch next()
const QChar at(int position) const const