aboutsummaryrefslogtreecommitdiffstats
path: root/Plugins/PythonSyntaxHighlighter/pythonsyntaxhighlighter.cpp
blob: eb9012a3d376fb232acf660100fbe0aee20d3324 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "pythonsyntaxhighlighter.h"
#include "uiconfig.h"
#include "services/config.h"
#include <QtWidgets/QPlainTextEdit>
#include <QSyntaxHighlighter>
#include <QRegularExpression>

// Started from Qt Syntax Highlighter example and then ported https://wiki.python.org/moin/PyQt/Python%20syntax%20highlighting
// Ported code copied from https://forum.qt.io/topic/96285/c-highlighter-for-python
// and then adjusted for SQLiteStudio (i.e. migrated from QRegExp to QRegularExpression).
class PythonHighlighter : public QSyntaxHighlighter
{
    public:
        typedef QMap<QString, QTextCharFormat> FormatMap;

        PythonHighlighter(QTextDocument *parent, const QMap<PythonSyntaxHighlighterPlugin::State, QTextCharFormat>* styles);

    protected:
        void highlightBlock(const QString &text) override;

    private:
        struct HighlightingRule
        {
            QRegularExpression pattern;
            PythonSyntaxHighlighterPlugin::State state;
            int matchIndex = 0;

            HighlightingRule() { }
            HighlightingRule(const QRegularExpression &r, int i, PythonSyntaxHighlighterPlugin::State state) : pattern(r), state(state), matchIndex(i) { }
            HighlightingRule(const QString &p, int i, PythonSyntaxHighlighterPlugin::State state) : pattern(QRegularExpression(p)), state(state), matchIndex(i) { }
        };

        const QMap<PythonSyntaxHighlighterPlugin::State, QTextCharFormat>* styles;
        static const QStringList keywords;
        static const QStringList operators;
        static const QStringList braces;

        void initialize();
        void highlightPythonBlock(const QString &text);
        bool matchMultiLine(const QString &text, const HighlightingRule &rule);

        QVector<HighlightingRule> _pythonHighlightingRules;
        HighlightingRule _triSingle, _triDouble;
};

// Python keywords
const QStringList PythonHighlighter::keywords = {
    "and", "assert", "break", "class", "continue", "def",
    "del", "elif", "else", "except", "exec", "finally",
    "for", "from", "global", "if", "import", "in",
    "is", "lambda", "not", "or", "pass", "print",
    "raise", "return", "try", "while", "yield",
    "None", "True", "False"
};

// Python operators
const QStringList PythonHighlighter::operators = {
    "\\=",
    // Comparison
    "\\=\\=", "\\!\\=", "\\<", "\\<\\=", "\\>", "\\>\\=",
    // Arithmetic
    "\\+", "\\-", "\\*", "\\/", "\\/\\/", "\\%", "\\*\\*",
    // In-place
    "\\+\\=", "\\-\\=", "\\*\\=", "\\/\\=", "\\%\\=",
    // Bitwise
    "\\^", "\\|", "\\&", "\\~", "\\>\\>", "\\<\\<"
};

// Python braces
const QStringList PythonHighlighter::braces = {
    "\\{", "\\}", "\\(", "\\)", "\\[", "\\]"
};

PythonHighlighter::PythonHighlighter(QTextDocument *parent, const QMap<PythonSyntaxHighlighterPlugin::State, QTextCharFormat>* styles)
    : QSyntaxHighlighter(parent), styles(styles)
{
    initialize();
}

void PythonHighlighter::highlightBlock(const QString &text)
{
    highlightPythonBlock(text);
}

void PythonHighlighter::initialize()
{
    // Multi-line strings (expression, flag, style)
    // FIXME: The triple-quotes in these two lines will mess up the
    // syntax highlighting from this point onward
    _triSingle = HighlightingRule("'''", 1, PythonSyntaxHighlighterPlugin::STRING);
    _triDouble = HighlightingRule("\"\"\"", 2, PythonSyntaxHighlighterPlugin::STRING);

    // Keyword, operator, and brace rules
    for (const QString &keyword : keywords)
    {
        QString pattern = QString("\\b%1\\b").arg(keyword);
        _pythonHighlightingRules += HighlightingRule(pattern, 0, PythonSyntaxHighlighterPlugin::KEYWORD);
    }

    for (const QString &pattern: operators)
        _pythonHighlightingRules += HighlightingRule(pattern, 0, PythonSyntaxHighlighterPlugin::OPERATOR);

    for (const QString &pattern: braces)
        _pythonHighlightingRules += HighlightingRule(pattern, 0, PythonSyntaxHighlighterPlugin::BRACE);

    // All other rules

    // 'self'
    _pythonHighlightingRules += HighlightingRule("\\bself\\b", 0, PythonSyntaxHighlighterPlugin::SELF);

    // Double-quoted string, possibly containing escape sequences
    _pythonHighlightingRules += HighlightingRule("\"([^\"\\\\]|\\\\.)*\"", 0, PythonSyntaxHighlighterPlugin::STRING);
    // Single-quoted string, possibly containing escape sequences
    _pythonHighlightingRules += HighlightingRule("'([^'\\\\]|\\\\.)*'", 0, PythonSyntaxHighlighterPlugin::STRING);

    // 'def' followed by an identifier
    _pythonHighlightingRules += HighlightingRule("\\bdef\\b\\s*(\\w+)", 1, PythonSyntaxHighlighterPlugin::DEFCLASS);
    // 'class' followed by an identifier
    _pythonHighlightingRules += HighlightingRule("\\bclass\\b\\s*(\\w+)", 1, PythonSyntaxHighlighterPlugin::DEFCLASS);

    // From '#' until a newline
    _pythonHighlightingRules += HighlightingRule("#[^\\n]*", 0, PythonSyntaxHighlighterPlugin::COMMENT);

    // Numeric literals
    _pythonHighlightingRules += HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, PythonSyntaxHighlighterPlugin::NUMBER);
    _pythonHighlightingRules += HighlightingRule("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, PythonSyntaxHighlighterPlugin::NUMBER);
    _pythonHighlightingRules += HighlightingRule("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, PythonSyntaxHighlighterPlugin::NUMBER);
}

void PythonHighlighter::highlightPythonBlock(const QString &text)
{
    if (text.isEmpty())
        return;

    int index = -1;
    setFormat(0, text.length(), styles->value(PythonSyntaxHighlighterPlugin::STANDARD));

    // Do other syntax formatting
    for (HighlightingRule& rule : _pythonHighlightingRules)
    {
        QRegularExpressionMatchIterator iter = rule.pattern.globalMatch(text, 0);
        while (iter.hasNext())
        {
            QRegularExpressionMatch match = iter.next();
            index = match.capturedStart(rule.matchIndex);
            int length = match.capturedLength(rule.matchIndex);
            if (length > 0)
                setFormat(index, length, styles->value(rule.state));
        }
    }

    setCurrentBlockState(0);

    // Do multi-line strings
    bool in_multiline = matchMultiLine(text, _triSingle);
    if (!in_multiline)
        matchMultiLine(text, _triDouble);
}

/*Do highlighting of multi-line strings. ``delimiter`` should be a
``QRegExp`` for triple-single-quotes or triple-double-quotes, and
``in_state`` should be a unique integer to represent the corresponding
state changes when inside those strings. Returns True if we're still
inside a multi-line string when this function is finished.
*/
bool PythonHighlighter::matchMultiLine(const QString &text, const HighlightingRule &rule)
{
    int start, add, end, length;

    // If inside triple-single quotes, start at 0
    if (previousBlockState() == rule.matchIndex)
    {
        start = 0;
        add = 0;
    }
    // Otherwise, look for the delimiter on this line
    else
    {
        QRegularExpressionMatch match = rule.pattern.match(text);
        start = match.capturedStart();
        // Move past this match
        add = match.capturedLength();
    }

    // As long as there's a delimiter match on this line...
    while (start >= 0)
    {
        QRegularExpressionMatch match = rule.pattern.match(text, start + add);
        // Look for the ending delimiter
        end = match.capturedStart();
        // Ending delimiter on this line?
        if(end >= add)
        {
            length = end - start + add + match.capturedLength();
            setCurrentBlockState(0);
        }
        // No; multi-line string
        else
        {
            setCurrentBlockState(rule.matchIndex);
            length = text.length() - start + add;
        }

        // Apply formatting
        setFormat(start, length, styles->value(rule.state));

        // Look for the next match
        match = rule.pattern.match(text, start + length);
        start = match.capturedStart();
    }

    // Return True if still inside a multi-line string, False otherwise
    if (currentBlockState() == rule.matchIndex)
        return true;
    else
        return false;
}


PythonSyntaxHighlighterPlugin::PythonSyntaxHighlighterPlugin()
{
}

QString PythonSyntaxHighlighterPlugin::getLanguageName() const
{
    return QStringLiteral("Python");
}

QSyntaxHighlighter* PythonSyntaxHighlighterPlugin::createSyntaxHighlighter(QWidget* textEdit) const
{
    QPlainTextEdit* plainEdit = dynamic_cast<QPlainTextEdit*>(textEdit);
    if (plainEdit)
        return new PythonHighlighter(plainEdit->document(), &styles);

    QTextEdit* edit = dynamic_cast<QTextEdit*>(textEdit);
    if (edit)
        return new PythonHighlighter(edit->document(), &styles);

    return nullptr;
}

void PythonSyntaxHighlighterPlugin::refreshFormats()
{
    QTextCharFormat format;

    // Standard
    format.setForeground(Cfg::getSyntaxForeground());
    format.setFontWeight(QFont::Normal);
    format.setFontItalic(false);
    styles[PythonSyntaxHighlighterPlugin::STANDARD] = format;

    // Class
    format.setForeground(Cfg::getSyntaxKeywordFg());
    format.setFontWeight(QFont::Bold);
    styles[PythonSyntaxHighlighterPlugin::DEFCLASS] = format;
    styles[PythonSyntaxHighlighterPlugin::KEYWORD] = format;

    // Self
    format.setForeground(Cfg::getSyntaxKeywordFg());
    format.setFontWeight(QFont::Normal);
    format.setFontItalic(true);
    styles[PythonSyntaxHighlighterPlugin::SELF] = format;

    // Operator
    format.setForeground(Cfg::getSyntaxForeground());
    format.setFontItalic(false);
    styles[PythonSyntaxHighlighterPlugin::OPERATOR] = format;

    // Parenthesis
    format.setForeground(Cfg::getSyntaxForeground());
    styles[PythonSyntaxHighlighterPlugin::BRACE] = format;

    // String
    format.setForeground(Cfg::getSyntaxStringFg());
    styles[PythonSyntaxHighlighterPlugin::STRING] = format;

    // Numbers
    format.setForeground(Cfg::getSyntaxNumberFg());
    styles[PythonSyntaxHighlighterPlugin::NUMBER] = format;

    // Comment
    format.setForeground(Cfg::getSyntaxCommentFg());
    format.setFontItalic(true);
    styles[PythonSyntaxHighlighterPlugin::COMMENT] = format;
}

QString PythonSyntaxHighlighterPlugin::previewSampleCode() const
{
    static_qstring(code,
                   "class MyClass:\n"
                   "    \"\"\"A simple example class\"\"\"\n"
                   "    i = 12345\n"
                   "\n"
                   "    def f(self):\n"
                   "        return 'hello world'"""
                   );
    return code;
}