summaryrefslogtreecommitdiff
path: root/src/plugins/python/pythonutils.cpp
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2020-02-17 13:06:08 +0100
committerDavid Schulz <david.schulz@qt.io>2020-02-20 05:42:17 +0000
commit5a2de2609e797143fe830f55a838ab73c8584cd8 (patch)
tree0099748a805494aa500d54dce74d2fc16ed12d87 /src/plugins/python/pythonutils.cpp
parentdec81133778ed11ae7b8d4672e7fe4dcff032334 (diff)
downloadqt-creator-5a2de2609e797143fe830f55a838ab73c8584cd8.tar.gz
Python: detect language server for python asynchron
Change-Id: I775e4cc21dc443b9ec6af81fabef9cf2bfd4e7d2 Fixes: QTCREATORBUG-23599 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/plugins/python/pythonutils.cpp')
-rw-r--r--src/plugins/python/pythonutils.cpp66
1 files changed, 46 insertions, 20 deletions
diff --git a/src/plugins/python/pythonutils.cpp b/src/plugins/python/pythonutils.cpp
index bc0309a302..e584a90e4d 100644
--- a/src/plugins/python/pythonutils.cpp
+++ b/src/plugins/python/pythonutils.cpp
@@ -44,6 +44,7 @@
#include <texteditor/textdocument.h>
#include <utils/qtcassert.h>
+#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <QDir>
@@ -144,19 +145,18 @@ static PythonLanguageServerState checkPythonLanguageServer(const FilePath &pytho
using namespace LanguageClient;
SynchronousProcess pythonProcess;
const CommandLine pythonLShelpCommand(python, {"-m", "pyls", "-h"});
- SynchronousProcessResponse response = pythonProcess.runBlocking(pythonLShelpCommand);
- if (response.allOutput().contains("Python Language Server")) {
- const FilePath &modulePath = getPylsModulePath(pythonLShelpCommand);
- for (const StdIOSettings *serverSetting : configuredPythonLanguageServer()) {
- if (modulePath == getPylsModulePath(serverSetting->command())) {
- return {serverSetting->m_enabled ? PythonLanguageServerState::AlreadyConfigured
- : PythonLanguageServerState::ConfiguredButDisabled,
- FilePath()};
- }
+ const FilePath &modulePath = getPylsModulePath(pythonLShelpCommand);
+ for (const StdIOSettings *serverSetting : configuredPythonLanguageServer()) {
+ if (modulePath == getPylsModulePath(serverSetting->command())) {
+ return {serverSetting->m_enabled ? PythonLanguageServerState::AlreadyConfigured
+ : PythonLanguageServerState::ConfiguredButDisabled,
+ FilePath()};
}
+ }
+ SynchronousProcessResponse response = pythonProcess.runBlocking(pythonLShelpCommand);
+ if (response.allOutput().contains("Python Language Server"))
return {PythonLanguageServerState::AlreadyInstalled, modulePath};
- }
const CommandLine pythonPipVersionCommand(python, {"-m", "pip", "-V"});
response = pythonProcess.runBlocking(pythonPipVersionCommand);
@@ -368,11 +368,38 @@ void PyLSConfigureAssistant::documentOpened(Core::IDocument *document)
void PyLSConfigureAssistant::openDocumentWithPython(const FilePath &python,
TextEditor::TextDocument *document)
{
- const PythonLanguageServerState &lsState = checkPythonLanguageServer(python);
+ using CheckPylsWatcher = QFutureWatcher<PythonLanguageServerState>;
+
+ QPointer<CheckPylsWatcher> watcher = new CheckPylsWatcher();
+ watcher->setFuture(Utils::runAsync(&checkPythonLanguageServer, python));
- if (lsState.state == PythonLanguageServerState::CanNotBeInstalled)
+ // cancel and delete watcher after a 10 second timeout
+ QTimer::singleShot(10000, this, [watcher]() {
+ if (watcher) {
+ watcher->cancel();
+ watcher->deleteLater();
+ }
+ });
+
+ connect(
+ watcher,
+ &CheckPylsWatcher::resultReadyAt,
+ this,
+ [=, document = QPointer<TextEditor::TextDocument>(document)]() {
+ if (!document || !watcher)
+ return;
+ handlePyLSState(python, watcher->result(), document);
+ watcher->deleteLater();
+ });
+}
+
+void PyLSConfigureAssistant::handlePyLSState(const FilePath &python,
+ const PythonLanguageServerState &state,
+ TextEditor::TextDocument *document)
+{
+ if (state.state == PythonLanguageServerState::CanNotBeInstalled)
return;
- if (lsState.state == PythonLanguageServerState::AlreadyConfigured) {
+ if (state.state == PythonLanguageServerState::AlreadyConfigured) {
if (const StdIOSettings *setting = languageServerForPython(python)) {
if (Client *client = LanguageClientManager::clientForSetting(setting).value(0))
LanguageClientManager::reOpenDocumentWithClient(document, client);
@@ -382,12 +409,11 @@ void PyLSConfigureAssistant::openDocumentWithPython(const FilePath &python,
resetEditorInfoBar(document);
Core::InfoBar *infoBar = document->infoBar();
- if (lsState.state == PythonLanguageServerState::CanBeInstalled
+ if (state.state == PythonLanguageServerState::CanBeInstalled
&& infoBar->canInfoBeAdded(installPylsInfoBarId)) {
- auto message
- = tr("Install and set up Python language server (PyLS) for %1 (%2). "
- "The language server provides Python specific completion and annotation.")
- .arg(pythonName(python), python.toUserOutput());
+ auto message = tr("Install and set up Python language server (PyLS) for %1 (%2). "
+ "The language server provides Python specific completion and annotation.")
+ .arg(pythonName(python), python.toUserOutput());
Core::InfoBarEntry info(installPylsInfoBarId,
message,
Core::InfoBarEntry::GlobalSuppression::Enabled);
@@ -395,7 +421,7 @@ void PyLSConfigureAssistant::openDocumentWithPython(const FilePath &python,
[=]() { installPythonLanguageServer(python, document); });
infoBar->addInfo(info);
m_infoBarEntries[python] << document;
- } else if (lsState.state == PythonLanguageServerState::AlreadyInstalled
+ } else if (state.state == PythonLanguageServerState::AlreadyInstalled
&& infoBar->canInfoBeAdded(startPylsInfoBarId)) {
auto message = tr("Found a Python language server for %1 (%2). "
"Set it up for this document?")
@@ -407,7 +433,7 @@ void PyLSConfigureAssistant::openDocumentWithPython(const FilePath &python,
[=]() { setupPythonLanguageServer(python, document); });
infoBar->addInfo(info);
m_infoBarEntries[python] << document;
- } else if (lsState.state == PythonLanguageServerState::ConfiguredButDisabled
+ } else if (state.state == PythonLanguageServerState::ConfiguredButDisabled
&& infoBar->canInfoBeAdded(enablePylsInfoBarId)) {
auto message = tr("Enable Python language server for %1 (%2)?")
.arg(pythonName(python), python.toUserOutput());