From 052ea6d23185943f12cccbf70df0ea52fc0171ba Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 21 Dec 2023 17:31:38 +0100 Subject: [PATCH] Python: Move highlighter and indenter class definitions to .cpp Change-Id: Ib71d520977034ca66bd84c9188ffed5fe74e1ba0 Reviewed-by: Reviewed-by: David Schulz --- src/plugins/python/pythoneditor.cpp | 4 ++-- src/plugins/python/pythonhighlighter.cpp | 30 ++++++++++++++++++------ src/plugins/python/pythonhighlighter.h | 22 +++-------------- src/plugins/python/pythonindenter.cpp | 25 +++++++++++++++++--- src/plugins/python/pythonindenter.h | 15 +----------- 5 files changed, 51 insertions(+), 45 deletions(-) diff --git a/src/plugins/python/pythoneditor.cpp b/src/plugins/python/pythoneditor.cpp index 58635d1a69b..1c74838ea4b 100644 --- a/src/plugins/python/pythoneditor.cpp +++ b/src/plugins/python/pythoneditor.cpp @@ -293,8 +293,8 @@ PythonEditorFactory::PythonEditorFactory() setDocumentCreator([]() { return new PythonDocument; }); setEditorWidgetCreator([]() { return new PythonEditorWidget; }); - setIndenterCreator([](QTextDocument *doc) { return new PythonIndenter(doc); }); - setSyntaxHighlighterCreator([] { return new PythonHighlighter; }); + setIndenterCreator(&createPythonIndenter); + setSyntaxHighlighterCreator(&createPythonHighlighter); setCommentDefinition(CommentDefinition::HashStyle); setParenthesesMatchingEnabled(true); setCodeFoldingSupported(true); diff --git a/src/plugins/python/pythonhighlighter.cpp b/src/plugins/python/pythonhighlighter.cpp index cc3b468f46f..14933eabaf7 100644 --- a/src/plugins/python/pythonhighlighter.cpp +++ b/src/plugins/python/pythonhighlighter.cpp @@ -17,10 +17,10 @@ #include #include #include + #include -namespace Python { -namespace Internal { +namespace Python::Internal { /** * @class PythonEditor::Internal::PythonHighlighter @@ -67,10 +67,22 @@ static TextEditor::TextStyle styleForFormat(int format) return C_TEXT; } -PythonHighlighter::PythonHighlighter() +class PythonHighlighter : public TextEditor::SyntaxHighlighter { - setTextFormatCategories(Format_FormatsAmount, styleForFormat); -} +public: + PythonHighlighter() + { + setTextFormatCategories(Format_FormatsAmount, styleForFormat); + } + +private: + void highlightBlock(const QString &text) override; + int highlightLine(const QString &text, int initialState); + void highlightImport(Internal::Scanner &scanner); + + int m_lastIndent = 0; + bool withinLicenseHeader = false; +}; /** * @brief PythonHighlighter::highlightBlock highlights single line of Python code @@ -187,5 +199,9 @@ void PythonHighlighter::highlightImport(Scanner &scanner) } } -} // namespace Internal -} // namespace Python +TextEditor::SyntaxHighlighter *createPythonHighlighter() +{ + return new PythonHighlighter; +} + +} // namespace Python::Internal diff --git a/src/plugins/python/pythonhighlighter.h b/src/plugins/python/pythonhighlighter.h index db29e53d89b..d88465ba746 100644 --- a/src/plugins/python/pythonhighlighter.h +++ b/src/plugins/python/pythonhighlighter.h @@ -5,24 +5,8 @@ #include -namespace Python { -namespace Internal { +namespace Python::Internal { -class Scanner; +TextEditor::SyntaxHighlighter *createPythonHighlighter(); -class PythonHighlighter : public TextEditor::SyntaxHighlighter -{ -public: - PythonHighlighter(); - -private: - void highlightBlock(const QString &text) override; - int highlightLine(const QString &text, int initialState); - void highlightImport(Internal::Scanner &scanner); - - int m_lastIndent = 0; - bool withinLicenseHeader = false; -}; - -} // namespace Internal -} // namespace Python +} // namespace Python::Internal diff --git a/src/plugins/python/pythonindenter.cpp b/src/plugins/python/pythonindenter.cpp index 1ffe095f3a1..b907d0f4531 100644 --- a/src/plugins/python/pythonindenter.cpp +++ b/src/plugins/python/pythonindenter.cpp @@ -28,9 +28,23 @@ static QTextBlock previousNonEmptyBlock(const QTextBlock &block) return result; } -PythonIndenter::PythonIndenter(QTextDocument *doc) - : TextEditor::TextIndenter(doc) -{} +class PythonIndenter : public TextEditor::TextIndenter +{ +public: + explicit PythonIndenter(QTextDocument *doc) + : TextEditor::TextIndenter(doc) + {} + +private: + bool isElectricCharacter(const QChar &ch) const override; + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; + + bool isElectricLine(const QString &line) const; + int getIndentDiff(const QString &previousLine, + const TextEditor::TabSettings &tabSettings) const; +}; /** * @brief Does given character change indentation level? @@ -102,4 +116,9 @@ int PythonIndenter::getIndentDiff(const QString &previousLine, return 0; } +TextEditor::TextIndenter *createPythonIndenter(QTextDocument *doc) +{ + return new PythonIndenter(doc); +} + } // namespace Python diff --git a/src/plugins/python/pythonindenter.h b/src/plugins/python/pythonindenter.h index 6c467371afb..6a3353cd005 100644 --- a/src/plugins/python/pythonindenter.h +++ b/src/plugins/python/pythonindenter.h @@ -7,19 +7,6 @@ namespace Python { -class PythonIndenter : public TextEditor::TextIndenter -{ -public: - explicit PythonIndenter(QTextDocument *doc); -private: - bool isElectricCharacter(const QChar &ch) const override; - int indentFor(const QTextBlock &block, - const TextEditor::TabSettings &tabSettings, - int cursorPositionInEditor = -1) override; - - bool isElectricLine(const QString &line) const; - int getIndentDiff(const QString &previousLine, - const TextEditor::TabSettings &tabSettings) const; -}; +TextEditor::TextIndenter *createPythonIndenter(QTextDocument *doc); } // namespace Python