Developer Documentation
Loading...
Searching...
No Matches
PythonSyntaxHighlighter.hh
1
2/*
3This is a C++ port of the following PyQt example
4http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
5C++ port by Frankie Simon (docklight.de, www.fuh-edv.de)
6
7The following free software license applies for this file ("X11 license"):
8
9Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10and associated documentation files (the "Software"), to deal in the Software without restriction,
11including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all copies or substantial
16portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
19LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#pragma once
26
27
28
29
30#include <QSyntaxHighlighter>
31#include <QRegularExpression>
32
35{
36public:
37 HighlightingRule(const QString &patternStr, int n, const QTextCharFormat &matchingFormat) :
38 originalRuleStr(patternStr),
39 pattern(QRegularExpression(patternStr)),
40 nth(n),
41 format(matchingFormat)
42 {
43 }
44
45 QString originalRuleStr;
46 QRegularExpression pattern;
47 int nth;
48 QTextCharFormat format;
49};
50
52class PythonSyntaxHighlighter : public QSyntaxHighlighter
53{
54 Q_OBJECT
55public:
56 explicit PythonSyntaxHighlighter(QTextDocument *parent = 0);
57protected:
58 void highlightBlock(const QString &text);
59private:
60 QStringList keywords;
61 QStringList operators;
62 QStringList braces;
63
64 QHash<QString, QTextCharFormat> basicStyles;
65
66 void initializeRules();
67
69 bool matchMultiline(const QString &text, const QRegularExpression &delimiter, const int inState, const QTextCharFormat &style);
70 const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style = QString());
71
72 QList<HighlightingRule> rules;
73 QRegularExpression triSingleQuote;
74 QRegularExpression triDoubleQuote;
75};
Container to describe a highlighting rule. Based on a regular expression, a relevant match # and the ...
Implementation of highlighting for Python code.
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...