Python: Remove pluginInstance() and convert users

Change-Id: I43179961cdee855b710fc082568a7d0d9a0f0719
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2024-02-01 16:37:40 +01:00
parent f589ed7f1e
commit ca2a4f771f
11 changed files with 33 additions and 56 deletions

View File

@@ -15,7 +15,7 @@ add_qtc_plugin(Python
pythonindenter.cpp pythonindenter.h pythonindenter.cpp pythonindenter.h
pythonkitaspect.h pythonkitaspect.cpp pythonkitaspect.h pythonkitaspect.cpp
pythonlanguageclient.cpp pythonlanguageclient.h pythonlanguageclient.cpp pythonlanguageclient.h
pythonplugin.cpp pythonplugin.h pythonplugin.cpp
pythonproject.cpp pythonproject.h pythonproject.cpp pythonproject.h
pythonrunconfiguration.cpp pythonrunconfiguration.h pythonrunconfiguration.cpp pythonrunconfiguration.h
pythonscanner.cpp pythonscanner.h pythonscanner.cpp pythonscanner.h

View File

@@ -3,7 +3,6 @@
#include "pipsupport.h" #include "pipsupport.h"
#include "pythonplugin.h"
#include "pythontr.h" #include "pythontr.h"
#include "pythonutils.h" #include "pythonutils.h"
@@ -220,9 +219,16 @@ QFuture<PipPackageInfo> Pip::info(const PipPackage &package)
return Utils::asyncRun(infoImpl, package, m_python); return Utils::asyncRun(infoImpl, package, m_python);
} }
Pip::Pip(const Utils::FilePath &python) static QObject *thePipGuard = nullptr;
: QObject(pluginInstance())
Pip::Pip(const FilePath &python)
: QObject(thePipGuard)
, m_python(python) , m_python(python)
{} {}
void setupPipSupport(QObject *guard)
{
thePipGuard = guard;
}
} // Python::Internal } // Python::Internal

View File

@@ -91,4 +91,6 @@ private:
QTimer m_killTimer; QTimer m_killTimer;
}; };
void setupPipSupport(QObject *guard);
} // Python::Internal } // Python::Internal

View File

@@ -4,7 +4,6 @@
#include "pyside.h" #include "pyside.h"
#include "pipsupport.h" #include "pipsupport.h"
#include "pythonplugin.h"
#include "pythontr.h" #include "pythontr.h"
#include "pythonutils.h" #include "pythonutils.h"
@@ -36,23 +35,17 @@ namespace Python::Internal {
const char installPySideInfoBarId[] = "Python::InstallPySide"; const char installPySideInfoBarId[] = "Python::InstallPySide";
PySideInstaller *PySideInstaller::instance()
{
static PySideInstaller *instance = new PySideInstaller; // FIXME: Leaks.
return instance;
}
void PySideInstaller::checkPySideInstallation(const FilePath &python, void PySideInstaller::checkPySideInstallation(const FilePath &python,
TextEditor::TextDocument *document) TextEditor::TextDocument *document)
{ {
document->infoBar()->removeInfo(installPySideInfoBarId); document->infoBar()->removeInfo(installPySideInfoBarId);
if (QPointer<QFutureWatcher<bool>> watcher = instance()->m_futureWatchers.value(document)) if (QPointer<QFutureWatcher<bool>> watcher = pySideInstaller().m_futureWatchers.value(document))
watcher->cancel(); watcher->cancel();
if (!python.exists()) if (!python.exists())
return; return;
const QString pySide = importedPySide(document->plainText()); const QString pySide = importedPySide(document->plainText());
if (pySide == "PySide2" || pySide == "PySide6") if (pySide == "PySide2" || pySide == "PySide6")
instance()->runPySideChecker(python, pySide, document); pySideInstaller().runPySideChecker(python, pySide, document);
} }
bool PySideInstaller::missingPySideInstallation(const FilePath &pythonPath, bool PySideInstaller::missingPySideInstallation(const FilePath &pythonPath,
@@ -80,9 +73,7 @@ QString PySideInstaller::importedPySide(const QString &text)
return match.captured(2); return match.captured(2);
} }
PySideInstaller::PySideInstaller() PySideInstaller::PySideInstaller() = default;
: QObject(pluginInstance())
{}
void PySideInstaller::installPyside(const FilePath &python, void PySideInstaller::installPyside(const FilePath &python,
const QString &pySide, const QString &pySide,
@@ -228,4 +219,10 @@ void PySideInstaller::runPySideChecker(const FilePath &python,
m_futureWatchers[document] = watcher; m_futureWatchers[document] = watcher;
} }
PySideInstaller &pySideInstaller()
{
static PySideInstaller thePySideInstaller;
return thePySideInstaller;
}
} // Python::Internal } // Python::Internal

View File

@@ -27,7 +27,6 @@ class PySideInstaller : public QObject
Q_OBJECT Q_OBJECT
public: public:
static PySideInstaller *instance();
static void checkPySideInstallation(const Utils::FilePath &python, static void checkPySideInstallation(const Utils::FilePath &python,
TextEditor::TextDocument *document); TextEditor::TextDocument *document);
@@ -36,6 +35,7 @@ signals:
private: private:
PySideInstaller(); PySideInstaller();
friend PySideInstaller &pySideInstaller();
void installPyside(const Utils::FilePath &python, void installPyside(const Utils::FilePath &python,
const QString &pySide, TextEditor::TextDocument *document); const QString &pySide, TextEditor::TextDocument *document);
@@ -53,4 +53,6 @@ private:
QHash<TextEditor::TextDocument *, QPointer<QFutureWatcher<bool>>> m_futureWatchers; QHash<TextEditor::TextDocument *, QPointer<QFutureWatcher<bool>>> m_futureWatchers;
}; };
PySideInstaller &pySideInstaller();
} // Python::Internal } // Python::Internal

View File

@@ -42,7 +42,6 @@ QtcPlugin {
"pythonlanguageclient.cpp", "pythonlanguageclient.cpp",
"pythonlanguageclient.h", "pythonlanguageclient.h",
"pythonplugin.cpp", "pythonplugin.cpp",
"pythonplugin.h",
"pythonproject.cpp", "pythonproject.cpp",
"pythonproject.h", "pythonproject.h",
"pythonrunconfiguration.cpp", "pythonrunconfiguration.cpp",

View File

@@ -264,7 +264,7 @@ PythonBuildConfiguration::PythonBuildConfiguration(Target *target, const Id &id)
updateCacheAndEmitEnvironmentChanged(); updateCacheAndEmitEnvironmentChanged();
connect(PySideInstaller::instance(), connect(&pySideInstaller(),
&PySideInstaller::pySideInstalled, &PySideInstaller::pySideInstalled,
this, this,
&PythonBuildConfiguration::handlePythonUpdated); &PythonBuildConfiguration::handlePythonUpdated);

View File

@@ -1,8 +1,7 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "pythonplugin.h" #include "pipsupport.h"
#include "pythonbuildconfiguration.h" #include "pythonbuildconfiguration.h"
#include "pythonconstants.h" #include "pythonconstants.h"
#include "pythoneditor.h" #include "pythoneditor.h"
@@ -33,30 +32,11 @@ using namespace Utils;
namespace Python::Internal { namespace Python::Internal {
static QObject *m_instance = nullptr;
QObject *pluginInstance()
{
return m_instance;
}
class PythonPlugin final : public ExtensionSystem::IPlugin class PythonPlugin final : public ExtensionSystem::IPlugin
{ {
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Python.json") Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Python.json")
public:
PythonPlugin()
{
m_instance = this;
}
~PythonPlugin() final
{
m_instance = nullptr;
}
private:
void initialize() final void initialize() final
{ {
setupPythonEditorFactory(this); setupPythonEditorFactory(this);
@@ -72,6 +52,8 @@ private:
setupPythonSettings(this); setupPythonSettings(this);
setupPythonWizard(); setupPythonWizard();
setupPipSupport(this);
KitManager::setIrrelevantAspects(KitManager::irrelevantAspects() KitManager::setIrrelevantAspects(KitManager::irrelevantAspects()
+ QSet<Id>{PythonKitAspect::id()}); + QSet<Id>{PythonKitAspect::id()});

View File

@@ -1,12 +0,0 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <QObject>
namespace Python::Internal {
QObject *pluginInstance();
} // Python::Internal

View File

@@ -5,7 +5,6 @@
#include "pythonconstants.h" #include "pythonconstants.h"
#include "pythonkitaspect.h" #include "pythonkitaspect.h"
#include "pythonplugin.h"
#include "pythontr.h" #include "pythontr.h"
#include "pythonutils.h" #include "pythonutils.h"
@@ -619,7 +618,7 @@ static QString defaultPylsConfiguration()
return QString::fromUtf8(QJsonDocument(configuration).toJson()); return QString::fromUtf8(QJsonDocument(configuration).toJson());
} }
static void disableOutdatedPylsNow() void PythonSettings::disableOutdatedPylsNow()
{ {
using namespace LanguageClient; using namespace LanguageClient;
const QList<BaseSettings *> const QList<BaseSettings *>
@@ -635,14 +634,14 @@ static void disableOutdatedPylsNow()
} }
} }
static void disableOutdatedPyls() void PythonSettings::disableOutdatedPyls()
{ {
using namespace ExtensionSystem; using namespace ExtensionSystem;
if (PluginManager::isInitializationDone()) { if (PluginManager::isInitializationDone()) {
disableOutdatedPylsNow(); disableOutdatedPylsNow();
} else { } else {
QObject::connect(PluginManager::instance(), &PluginManager::initializationDone, QObject::connect(PluginManager::instance(), &PluginManager::initializationDone,
pluginInstance(), &disableOutdatedPylsNow); this, &PythonSettings::disableOutdatedPylsNow);
} }
} }

View File

@@ -59,6 +59,8 @@ public slots:
void listDetectedPython(const QString &detectionSource, QString *logMessage); void listDetectedPython(const QString &detectionSource, QString *logMessage);
private: private:
void disableOutdatedPyls();
void disableOutdatedPylsNow();
void fixupPythonKits(); void fixupPythonKits();
void initFromSettings(Utils::QtcSettings *settings); void initFromSettings(Utils::QtcSettings *settings);
void writeToSettings(Utils::QtcSettings *settings); void writeToSettings(Utils::QtcSettings *settings);