aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python/pythonplugin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/python/pythonplugin.cpp')
-rw-r--r--src/plugins/python/pythonplugin.cpp48
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