diff options
| author | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2024-10-14 14:27:12 +0200 |
|---|---|---|
| committer | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2024-10-15 10:38:37 +0000 |
| commit | 587742fbea82324a8e0950dcb971fc1f476bfd98 (patch) | |
| tree | 5b4b4adb3f92c711ffd7689417c23399b844a821 /src/plugins/python/pythonplugin.cpp | |
| parent | 6f258d25411299aba40319364a19cae7252ffd72 (diff) | |
Python: Register highlighter
Change-Id: Id3fcfbea25c1dcaafaf40b9852d921eb329c8bfa
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/python/pythonplugin.cpp')
| -rw-r--r-- | src/plugins/python/pythonplugin.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/plugins/python/pythonplugin.cpp b/src/plugins/python/pythonplugin.cpp index ad3a1da86d9..65c3ef7b887 100644 --- a/src/plugins/python/pythonplugin.cpp +++ b/src/plugins/python/pythonplugin.cpp @@ -5,6 +5,7 @@ #include "pythonbuildconfiguration.h" #include "pythonconstants.h" #include "pythoneditor.h" +#include "pythonhighlighter.h" #include "pythonkitaspect.h" #include "pythonproject.h" #include "pythonrunconfiguration.h" @@ -23,6 +24,8 @@ #include <projectexplorer/projectmanager.h> #include <projectexplorer/taskhub.h> +#include <texteditor/texteditorsettings.h> + #include <utils/fsengine/fileiconprovider.h> #include <utils/theme/theme.h> @@ -32,6 +35,39 @@ using namespace Utils; namespace Python::Internal { +static QFuture<QTextDocument *> highlightCode(const QString &code, const QString &mimeType) +{ + QTextDocument *document = new QTextDocument; + document->setPlainText(code); + + std::shared_ptr<QPromise<QTextDocument *>> promise + = std::make_shared<QPromise<QTextDocument *>>(); + + promise->start(); + + TextEditor::SyntaxHighlighter *highlighter = createPythonHighlighter(); + + QObject::connect( + highlighter, &TextEditor::SyntaxHighlighter::finished, document, [document, promise]() { + promise->addResult(document); + promise->finish(); + }); + + QFutureWatcher<QTextDocument *> *watcher = new QFutureWatcher<QTextDocument *>(document); + QObject::connect(watcher, &QFutureWatcher<QTextDocument *>::canceled, document, [document]() { + document->deleteLater(); + }); + watcher->setFuture(promise->future()); + + highlighter->setParent(document); + highlighter->setFontSettings(TextEditor::TextEditorSettings::fontSettings()); + highlighter->setMimeType(mimeType); + highlighter->setDocument(document); + highlighter->rehighlight(); + + return promise->future(); +} + class PythonPlugin final : public ExtensionSystem::IPlugin { Q_OBJECT @@ -59,6 +95,18 @@ class PythonPlugin final : public ExtensionSystem::IPlugin ProjectManager::registerProjectType<PythonProject>(Constants::C_PY_PROJECT_MIME_TYPE); ProjectManager::registerProjectType<PythonProject>(Constants::C_PY_PROJECT_MIME_TYPE_LEGACY); + + auto oldHighlighter = Utils::Text::codeHighlighter(); + Utils::Text::setCodeHighlighter( + [oldHighlighter](const QString &code, const QString &mimeType) + -> QFuture<QTextDocument *> { + if (mimeType == "text/python" || mimeType == "text/x-python" + || mimeType == "text/x-python3") { + return highlightCode(code, mimeType); + } + + return oldHighlighter(code, mimeType); + }); } void extensionsInitialized() final |
