Copilot: improve client handling

- centralize starting client
 - allow to start copilot with alternative settings
 - do not reset the client on finished.

A finish might be followed by a restart attempt, so untracking the
client in the plugin can result in having multiple running clients. Use
a QPointer instead to safely reset once the client is deleted.

Change-Id: Ifab8b04287d84bbdd4a0e936f4684b8d4391da2b
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
David Schulz
2023-03-09 11:08:57 +01:00
parent 2fc801e153
commit 301a3cb34f
4 changed files with 23 additions and 27 deletions

View File

@@ -10,6 +10,8 @@
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <languageclient/languageclientmanager.h>
#include <texteditor/texteditor.h>
using namespace Utils;
@@ -18,28 +20,16 @@ using namespace Core;
namespace Copilot {
namespace Internal {
CopilotPlugin::~CopilotPlugin()
{
if (m_client)
m_client->shutdown();
m_client = nullptr;
}
void CopilotPlugin::initialize()
{
CopilotSettings::instance().readSettings(ICore::settings());
m_client = new CopilotClient();
restartClient();
connect(m_client, &CopilotClient::finished, this, [this]() { m_client = nullptr; });
connect(&CopilotSettings::instance(), &CopilotSettings::applied, this, [this]() {
if (m_client)
m_client->shutdown();
m_client = nullptr;
m_client = new CopilotClient();
});
connect(&CopilotSettings::instance(),
&CopilotSettings::applied,
this,
&CopilotPlugin::restartClient);
}
void CopilotPlugin::extensionsInitialized()
@@ -47,5 +37,12 @@ void CopilotPlugin::extensionsInitialized()
CopilotOptionsPage::instance().init();
}
void CopilotPlugin::restartClient()
{
LanguageClient::LanguageClientManager::shutdownClient(m_client);
m_client = new CopilotClient(CopilotSettings::instance().nodeJsPath.filePath(),
CopilotSettings::instance().distPath.filePath());
}
} // namespace Internal
} // namespace Copilot