ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PythonHighlighter.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <QSyntaxHighlighter>
11 
12 // Qt5/Qt6 Compatibility
13 #include <QtCompat.h>
14 
15 class ColorScheme;
16 
17 // Started from Qt Syntax Highlighter example and then ported
18 // https://wiki.python.org/moin/PyQt/Python%20syntax%20highlighting
19 class PythonHighlighter final : public QSyntaxHighlighter
20 {
21  public:
22  // For lack of a better name
23  enum class CodeElement
24  {
25  Keyword = 0,
26  Operator,
27  Brace,
28  Definition,
29  String,
30  DocString,
31  Comment,
32  Self,
33  Numbers,
34  End
35  };
36 
38 
39  void useColorScheme(const ColorScheme &colorScheme);
40 
41  explicit PythonHighlighter(QTextDocument *parent = nullptr);
42 
43  protected:
44  void highlightBlock(const QString &text) override;
45 
46  private:
47  struct HighlightingRule
48  {
49  CodeElement element = CodeElement::End;
50  QtCompatRegExp pattern;
51  QTextCharFormat format;
52  int matchIndex = 0;
53 
54  HighlightingRule() = default;
55 
56  HighlightingRule(const CodeElement e, const QString &p, const int i)
57  : element(e), pattern(qtCompatRegExp(p)), matchIndex(i)
58  {
59  }
60  };
61 
62  void initialize();
63 
64  void highlightPythonBlock(const QString &text);
65 
66  bool matchMultiLine(const QString &text, const HighlightingRule &rule);
67 
68  QVector<HighlightingRule> m_highlightingRules;
69  HighlightingRule m_triSingle;
70  HighlightingRule m_triDouble;
71 };
filament::Texture::InternalFormat format
QRegularExpression qtCompatRegExp(const QString &pattern)
Definition: QtCompat.h:183
QRegularExpression QtCompatRegExp
Definition: QtCompat.h:170
ColorScheme to be used by the PythonHighlighter & Editor.
Definition: ColorScheme.h:14
static QString CodeElementName(PythonHighlighter::CodeElement e)
void highlightBlock(const QString &text) override
PythonHighlighter(QTextDocument *parent=nullptr)
void useColorScheme(const ColorScheme &colorScheme)