itom
Loading...
Searching...
No Matches
pythonSyntaxHighlighter.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom.
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 itom is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17 General Public Licence for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with itom. If not, see <http://www.gnu.org/licenses/>.
21
22 Further hints:
23 ------------------------
24
25 This file belongs to the code editor of itom. The code editor is
26 in major parts a fork / rewritten version of the python-based source
27 code editor PyQode from Colin Duquesnoy and others
28 (see https://github.com/pyQode). PyQode itself is licensed under
29 the MIT License (MIT).
30
31 Some parts of the code editor of itom are also inspired by the
32 source code editor of the Spyder IDE (https://github.com/spyder-ide),
33 also licensed under the MIT License and developed by the Spyder Project
34 Contributors.
35
36*********************************************************************** */
37
38#ifndef PYSYNTAXHIGHLIGHER_H
39#define PYSYNTAXHIGHLIGHER_H
40
41#include "syntaxHighlighterBase.h"
42
43#include <qregularexpression.h>
44#include <qtextformat.h>
45
46namespace ito {
47
48/*
49This module contains a native python syntax highlighter, strongly inspired from
50spyderlib.widgets.source_code.syntax_higlighter.PythonSH but modified to
51highlight docstrings with a different color than the string color and to
52highlight decorators and self parameters.
53
54It is approximately 3 time faster then :class:`pyqode.core.modes.PygmentsSH`.
55*/
57{
58 Q_OBJECT
59public:
60 enum State
61 {
62 Normal = 0,
63 InsideSq3String = 1, // the line is within a '''....''' multiline comment
64 InsideDq3String = 2, // the line is within a """....""" multiline comment
65 InsideSqString = 3, // the line is within a '...' string
66 InsideDqString = 4 // the line is within a "..." string
67 };
68
69 PythonSyntaxHighlighter(QTextDocument *parent, const QString &description = "", QSharedPointer<CodeEditorStyle> editorStyle = QSharedPointer<CodeEditorStyle>());
70
72
73 /*
74 Abstract method. Override this to apply syntax highlighting.
75
76 :param text: Line of text to highlight.
77 :param block: current block
78 */
79 void highlight_block(const QString &text, QTextBlock &block);
80
81 void default_highlight_block(const QString &text, const TextBlockUserData *textBlockUserData);
82
83 virtual void rehighlight();
84
85private:
86
88 {
89 NamedRegExp(const QString &groupName_, const QRegularExpression &regExp_) : regExp(regExp_), groupNames(groupName_) {}
90 NamedRegExp(const QStringList &groupNames_, const QRegularExpression &regExp_) : regExp(regExp_), groupNames(groupNames_) {}
91 QRegularExpression regExp;
92 QStringList groupNames;
93 };
94
95 //syntax highlighting rules
96 static QList<NamedRegExp> regExpProg;
97 static QRegularExpression regExpIdProg;
98 static QRegularExpression regExpAsProg;
99 static QRegularExpression regExpOeComment; //comments suitable for outline explorer
100
101 QTextCharFormat getFormatFromStyle(StyleItem::StyleType token) const;
102 const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style = QString());
103
104 static QList<NamedRegExp> makePythonPatterns(const QStringList &additionalKeywords = QStringList(), const QStringList &additionalBuiltins = QStringList());
105};
106
107} //end namespace ito
108
109#endif
Definition pythonSyntaxHighlighter.h:57
State
< Syntax highlighting states (from one text block to another):
Definition pythonSyntaxHighlighter.h:61
Definition syntaxHighlighterBase.h:61
Definition textBlockUserData.h:56
Definition apiFunctionsGraph.cpp:40
Definition pythonSyntaxHighlighter.h:88