Python: Move highlighter and indenter class definitions to .cpp

Change-Id: Ib71d520977034ca66bd84c9188ffed5fe74e1ba0
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2023-12-21 17:31:38 +01:00
parent 460b1641e0
commit 052ea6d231
5 changed files with 51 additions and 45 deletions

View File

@@ -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);

View File

@@ -17,10 +17,10 @@
#include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h>
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

View File

@@ -5,24 +5,8 @@
#include <texteditor/syntaxhighlighter.h>
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

View File

@@ -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

View File

@@ -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