itom  4.1.0
pythonSyntaxHighlighter.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet 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 
46 namespace ito {
47 
48 /*
49 This module contains a native python syntax highlighter, strongly inspired from
50 spyderlib.widgets.source_code.syntax_higlighter.PythonSH but modified to
51 highlight docstrings with a different color than the string color and to
52 highlight decorators and self parameters.
53 
54 It is approximately 3 time faster then :class:`pyqode.core.modes.PygmentsSH`.
55 */
57 {
58  Q_OBJECT
59 public:
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  typedef QRegularExpression QQRegExp;
70 
71  PythonSyntaxHighlighter(QTextDocument *parent, const QString &description = "", QSharedPointer<CodeEditorStyle> editorStyle = QSharedPointer<CodeEditorStyle>());
72 
73  virtual ~PythonSyntaxHighlighter();
74 
75  /*
76  Abstract method. Override this to apply syntax highlighting.
77 
78  :param text: Line of text to highlight.
79  :param block: current block
80  */
81  void highlight_block(const QString &text, QTextBlock &block);
82 
83  void default_highlight_block(const QString &text, bool outputNotError);
84 
85  virtual void rehighlight();
86 
87 private:
88 
89  struct NamedRegExp
90  {
91  NamedRegExp(const QString &groupName_, const QQRegExp &regExp_) : regExp(regExp_), groupNames(groupName_) {}
92  NamedRegExp(const QStringList &groupNames_, const QQRegExp &regExp_) : regExp(regExp_), groupNames(groupNames_) {}
93  QQRegExp regExp;
94  QStringList groupNames;
95  };
96 
97  //syntax highlighting rules
98  static QList<NamedRegExp> regExpProg;
99  static QRegExp regExpIdProg;
100  static QRegExp regExpAsProg;
101  static QQRegExp regExpOeComment; //comments suitable for outline explorer
102 
103  QTextCharFormat getFormatFromStyle(StyleItem::StyleType token) const;
104  const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style = QString());
105 
106  static QList<NamedRegExp> makePythonPatterns(const QStringList &additionalKeywords = QStringList(), const QStringList &additionalBuiltins = QStringList());
107 };
108 
109 } //end namespace ito
110 
111 #endif
Definition: syntaxHighlighterBase.h:59
State
< Syntax highlighting states (from one text block to another):
Definition: pythonSyntaxHighlighter.h:60
Definition: apiFunctionsGraph.cpp:39
Definition: pythonSyntaxHighlighter.h:56
Definition: pythonSyntaxHighlighter.h:89