forked from qt-creator/qt-creator
LanguageClient: Introduce ClientRequestTask
This class is going to be used inside TaskTree. Change-Id: Ia227a8f41e4557b45053cb018497a7eca8f8ac6a Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -141,6 +141,7 @@ public:
|
|||||||
void setMethod(const QString &method)
|
void setMethod(const QString &method)
|
||||||
{ m_jsonObject.insert(methodKey, method); }
|
{ m_jsonObject.insert(methodKey, method); }
|
||||||
|
|
||||||
|
using Parameters = Params;
|
||||||
std::optional<Params> params() const
|
std::optional<Params> params() const
|
||||||
{
|
{
|
||||||
const QJsonValue ¶ms = m_jsonObject.value(paramsKey);
|
const QJsonValue ¶ms = m_jsonObject.value(paramsKey);
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ class LANGUAGESERVERPROTOCOL_EXPORT WorkspaceSymbolRequest : public Request<
|
|||||||
LanguageClientArray<SymbolInformation>, std::nullptr_t, WorkspaceSymbolParams>
|
LanguageClientArray<SymbolInformation>, std::nullptr_t, WorkspaceSymbolParams>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WorkspaceSymbolRequest(const WorkspaceSymbolParams ¶ms);
|
explicit WorkspaceSymbolRequest(const WorkspaceSymbolParams ¶ms);
|
||||||
using Request::Request;
|
using Request::Request;
|
||||||
constexpr static const char methodName[] = "workspace/symbol";
|
constexpr static const char methodName[] = "workspace/symbol";
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ add_qtc_plugin(LanguageClient
|
|||||||
SOURCES
|
SOURCES
|
||||||
callhierarchy.cpp callhierarchy.h
|
callhierarchy.cpp callhierarchy.h
|
||||||
client.cpp client.h
|
client.cpp client.h
|
||||||
|
clientrequesttask.cpp clientrequesttask.h
|
||||||
diagnosticmanager.cpp diagnosticmanager.h
|
diagnosticmanager.cpp diagnosticmanager.h
|
||||||
documentsymbolcache.cpp documentsymbolcache.h
|
documentsymbolcache.cpp documentsymbolcache.h
|
||||||
dynamiccapabilities.cpp dynamiccapabilities.h
|
dynamiccapabilities.cpp dynamiccapabilities.h
|
||||||
|
|||||||
39
src/plugins/languageclient/clientrequesttask.cpp
Normal file
39
src/plugins/languageclient/clientrequesttask.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
// Copyright (C) 2023 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
|
#include "clientrequesttask.h"
|
||||||
|
|
||||||
|
#include <QScopeGuard>
|
||||||
|
|
||||||
|
using namespace LanguageServerProtocol;
|
||||||
|
|
||||||
|
namespace LanguageClient {
|
||||||
|
|
||||||
|
ClientRequestTaskAdapter::ClientRequestTaskAdapter()
|
||||||
|
{
|
||||||
|
task()->setResponseCallback([this](const WorkspaceSymbolRequest::Response &response){
|
||||||
|
emit done(response.result().has_value());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientRequestTaskAdapter::start()
|
||||||
|
{
|
||||||
|
task()->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WorkspaceSymbolRequestTask::preStartCheck()
|
||||||
|
{
|
||||||
|
if (!ClientRequestTask::preStartCheck() || !client()->locatorsEnabled())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const std::optional<std::variant<bool, WorkDoneProgressOptions>> capability
|
||||||
|
= client()->capabilities().workspaceSymbolProvider();
|
||||||
|
if (!capability.has_value())
|
||||||
|
return false;
|
||||||
|
if (std::holds_alternative<bool>(*capability) && !std::get<bool>(*capability))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace LanguageClient
|
||||||
78
src/plugins/languageclient/clientrequesttask.h
Normal file
78
src/plugins/languageclient/clientrequesttask.h
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
// Copyright (C) 2023 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "languageclient_global.h"
|
||||||
|
|
||||||
|
#include "client.h"
|
||||||
|
|
||||||
|
#include <languageserverprotocol/lsptypes.h>
|
||||||
|
#include <languageserverprotocol/lsputils.h>
|
||||||
|
#include <languageserverprotocol/workspace.h>
|
||||||
|
#include <utils/tasktree.h>
|
||||||
|
|
||||||
|
namespace LanguageClient {
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Request>
|
||||||
|
class LANGUAGECLIENT_EXPORT ClientRequestTask
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~ClientRequestTask()
|
||||||
|
{
|
||||||
|
if (m_id)
|
||||||
|
m_client->cancelRequest(*m_id); // In order to not to invoke a response callback anymore
|
||||||
|
}
|
||||||
|
|
||||||
|
void setClient(Client *client) { m_client = client; }
|
||||||
|
Client *client() const { return m_client; }
|
||||||
|
void setParams(const typename Request::Parameters ¶ms) { m_params = params; }
|
||||||
|
|
||||||
|
void start()
|
||||||
|
{
|
||||||
|
QTC_ASSERT(isRunning(), return);
|
||||||
|
QTC_ASSERT(preStartCheck(), m_callback({}); return);
|
||||||
|
|
||||||
|
Request request(m_params);
|
||||||
|
request.setResponseCallback([this](const typename Request::Response &response) {
|
||||||
|
m_response = response;
|
||||||
|
m_id = {};
|
||||||
|
m_callback(response);
|
||||||
|
});
|
||||||
|
m_id = request.id();
|
||||||
|
m_client->sendMessage(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isRunning() const { return m_id.has_value(); }
|
||||||
|
virtual bool preStartCheck() { return m_client && m_client->reachable() && m_params.isValid(); }
|
||||||
|
|
||||||
|
typename Request::Response response() const { return m_response; }
|
||||||
|
void setResponseCallback(typename Request::ResponseCallback callback) { m_callback = callback; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client *m_client = nullptr;
|
||||||
|
typename Request::Parameters m_params;
|
||||||
|
typename Request::ResponseCallback m_callback;
|
||||||
|
std::optional<LanguageServerProtocol::MessageId> m_id;
|
||||||
|
typename Request::Response m_response;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LANGUAGECLIENT_EXPORT WorkspaceSymbolRequestTask
|
||||||
|
: public ClientRequestTask<LanguageServerProtocol::WorkspaceSymbolRequest>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool preStartCheck() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LANGUAGECLIENT_EXPORT ClientRequestTaskAdapter
|
||||||
|
: public Utils::Tasking::TaskAdapter<WorkspaceSymbolRequestTask>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ClientRequestTaskAdapter();
|
||||||
|
void start() final;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace LanguageClient
|
||||||
|
|
||||||
|
QTC_DECLARE_CUSTOM_TASK(WorkspaceSymbolRequest, LanguageClient::WorkspaceSymbolRequestTask);
|
||||||
@@ -24,6 +24,8 @@ QtcPlugin {
|
|||||||
"callhierarchy.h",
|
"callhierarchy.h",
|
||||||
"client.cpp",
|
"client.cpp",
|
||||||
"client.h",
|
"client.h",
|
||||||
|
"clientrequesttask.cpp",
|
||||||
|
"clientrequesttask.h",
|
||||||
"diagnosticmanager.cpp",
|
"diagnosticmanager.cpp",
|
||||||
"diagnosticmanager.h",
|
"diagnosticmanager.h",
|
||||||
"documentsymbolcache.cpp",
|
"documentsymbolcache.cpp",
|
||||||
|
|||||||
Reference in New Issue
Block a user