LanguageServerProtocol: add progress support

Change-Id: I8d3ccf0626ccde39516bbd024ed6e2da0380e4de
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-02-02 09:50:56 +01:00
parent 5fcd52fe83
commit a7fc1631a7
15 changed files with 512 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ add_qtc_library(LanguageServerProtocol
lsptypes.cpp lsptypes.h
lsputils.cpp lsputils.h
messages.cpp messages.h
progresssupport.cpp progresssupport.h
servercapabilities.cpp servercapabilities.h
shutdownmessages.cpp shutdownmessages.h
textsynchronization.cpp textsynchronization.h

View File

@@ -508,6 +508,27 @@ public:
void clearConfiguration() { remove(configurationKey); }
};
class WindowClientClientCapabilities : public JsonObject
{
public:
using JsonObject::JsonObject;
/**
* Whether client supports handling progress notifications.
* If set, servers are allowed to report in `workDoneProgress` property
* in the request specific server capabilities.
*
*/
Utils::optional<bool> workDoneProgress() const
{ return optionalValue<bool>(workDoneProgressKey); }
void setWorkDoneProgress(bool workDoneProgress)
{ insert(workDoneProgressKey, workDoneProgress); }
void clearWorkDoneProgress() { remove(workDoneProgressKey); }
private:
constexpr static const char workDoneProgressKey[] = "workDoneProgress";
};
class LANGUAGESERVERPROTOCOL_EXPORT ClientCapabilities : public JsonObject
{
public:
@@ -527,10 +548,17 @@ public:
{ insert(textDocumentKey, textDocument); }
void clearTextDocument() { remove(textDocumentKey); }
// Window specific client capabilities.
Utils::optional<WindowClientClientCapabilities> window() const
{ return optionalValue<WindowClientClientCapabilities>(windowKey); }
void setWindow(const WindowClientClientCapabilities &window)
{ insert(windowKey, window); }
void clearWindow() { remove(windowKey); }
// Experimental client capabilities.
QJsonValue experimental() const { return value(experimentalKey); }
void setExperimental(const QJsonValue &experimental) { insert(experimentalKey, experimental); }
void clearExperimental() { remove(experimentalKey); }
};
}
} // namespace LanguageServerProtocol

View File

@@ -215,6 +215,7 @@ constexpr char valueSetKey[] = "valueSet";
constexpr char versionKey[] = "version";
constexpr char willSaveKey[] = "willSave";
constexpr char willSaveWaitUntilKey[] = "willSaveWaitUntil";
constexpr char windowKey[] = "window";
constexpr char workDoneProgressKey[] = "workDoneProgress";
constexpr char workspaceEditKey[] = "workspaceEdit";
constexpr char workspaceFoldersKey[] = "workspaceFolders";

View File

@@ -17,15 +17,18 @@ HEADERS += \
lsptypes.h \
lsputils.h \
messages.h \
progresssupport.h \
servercapabilities.h \
shutdownmessages.h \
textsynchronization.h \
workspace.h
workspace.h \
SOURCES += \
basemessage.cpp \
client.cpp \
clientcapabilities.cpp \
completion.cpp \
diagnostics.cpp \
initializemessages.cpp \
jsonobject.cpp \
jsonrpcmessages.cpp \
@@ -33,9 +36,8 @@ SOURCES += \
lsptypes.cpp \
lsputils.cpp \
messages.cpp \
progresssupport.cpp \
servercapabilities.cpp \
shutdownmessages.cpp \
textsynchronization.cpp \
workspace.cpp \
client.cpp \
shutdownmessages.cpp \
diagnostics.cpp

View File

@@ -35,6 +35,8 @@ Project {
"lsputils.h",
"messages.cpp",
"messages.h",
"progresssupport.cpp",
"progresssupport.h",
"servercapabilities.cpp",
"servercapabilities.h",
"shutdownmessages.cpp",

View File

@@ -0,0 +1,71 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "progresssupport.h"
#include <utils/qtcassert.h>
#include <QUuid>
namespace LanguageServerProtocol {
ProgressToken::ProgressToken()
: Utils::variant<int, QString>(QUuid::createUuid().toString())
{}
ProgressToken::ProgressToken(const QJsonValue &value)
{
if (!QTC_GUARD(value.isDouble() || value.isString()))
emplace<QString>(QUuid::createUuid().toString());
else if (value.isDouble())
emplace<int>(value.toInt());
else
emplace<QString>(value.toString());
}
ProgressToken::operator QJsonValue() const
{
if (Utils::holds_alternative<QString>(*this))
return QJsonValue(Utils::get<QString>(*this));
return QJsonValue(Utils::get<int>(*this));
}
ProgressParams::ProgressType ProgressParams::value() const
{
QJsonObject paramsValue = JsonObject::value(valueKey).toObject();
if (paramsValue[kindKey] == "begin")
return ProgressParams::ProgressType(WorkDoneProgressBegin(paramsValue));
if (paramsValue[kindKey] == "report")
return ProgressParams::ProgressType(WorkDoneProgressReport(paramsValue));
return ProgressParams::ProgressType(WorkDoneProgressEnd(paramsValue));
}
void ProgressParams::setValue(const ProgressParams::ProgressType &value)
{
insertVariant<WorkDoneProgressBegin, WorkDoneProgressReport, WorkDoneProgressEnd>(valueKey, value);
}
} // namespace LanguageServerProtocol

View File

@@ -0,0 +1,189 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "jsonrpcmessages.h"
#include <utils/variant.h>
#include <QJsonValue>
namespace LanguageServerProtocol {
class LANGUAGESERVERPROTOCOL_EXPORT ProgressToken : public Utils::variant<int, QString>
{
public:
ProgressToken();
using variant::variant;
explicit ProgressToken(const QJsonValue &value);
bool isValid() { return true; }
operator QJsonValue() const;
};
class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressReport : public JsonObject
{
public:
using JsonObject::JsonObject;
/**
* Controls if a cancel button should be shown to allow the user to cancel the
* long running operation.
* Clients that don't support cancellation can ignore the setting.
*/
Utils::optional<bool> cancellable() const { return optionalValue<bool>(cancellableKey); }
void setCancellable(bool cancellable) { insert(cancellableKey, cancellable); }
void clearCancellable() { remove(cancellableKey); }
/**
* Optional, more detailed associated progress message. Contains
* complementary information to the `title`.
*
* Examples: "3/25 files", "project/src/module2", "node_modules/some_dep".
* If unset, the previous progress message (if any) is still valid.
*/
Utils::optional<QString> message() const { return optionalValue<QString>(messageKey); }
void setMessage(const QString &message) { insert(messageKey, message); }
void clearMessage() { remove(messageKey); }
/**
* Optional progress percentage to display (value 100 is considered 100%).
* If not provided infinite progress is assumed and clients are allowed
* to ignore the `percentage` value in subsequent in report notifications.
*
* The value should be steadily rising. Clients are free to ignore values
* that are not following this rule.
*/
Utils::optional<int> percentage() const { return optionalValue<int>(percentageKey); }
void setPercentage(int percentage) { insert(percentageKey, percentage); }
void clearPercentage() { remove(percentageKey); }
private:
constexpr static const char cancellableKey[] = "cancellable";
constexpr static const char messageKey[] = "message";
constexpr static const char percentageKey[] = "percentage";
};
class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressBegin : public WorkDoneProgressReport
{
public:
using WorkDoneProgressReport::WorkDoneProgressReport;
/**
* Mandatory title of the progress operation. Used to briefly inform about
* the kind of operation being performed.
*
* Examples: "Indexing" or "Linking dependencies".
*/
QString title() const { return typedValue<QString>(titleKey); }
void setTitle(const QString &title) { insert(titleKey, title); }
private:
constexpr static const char titleKey[] = "title";
};
class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressEnd : public JsonObject
{
public:
using JsonObject::JsonObject;
/**
* Optional, a final message indicating to for example indicate the outcome
* of the operation.
*/
Utils::optional<QString> message() const { return optionalValue<QString>(messageKey); }
void setMessage(const QString &message) { insert(messageKey, message); }
void clearMessage() { remove(messageKey); }
private:
constexpr static const char cancellableKey[] = "cancellable";
constexpr static const char messageKey[] = "message";
constexpr static const char percentageKey[] = "percentage";
};
class LANGUAGESERVERPROTOCOL_EXPORT ProgressParams : public JsonObject
{
public:
using JsonObject::JsonObject;
ProgressToken token() const { return ProgressToken(JsonObject::value(tokenKey)); }
void setToken(const ProgressToken &token) { insert(tokenKey, token); }
using ProgressType
= Utils::variant<WorkDoneProgressBegin, WorkDoneProgressReport, WorkDoneProgressEnd>;
ProgressType value() const;
void setValue(const ProgressType &value);
bool isValid() const override { return contains(tokenKey) && contains(valueKey); }
private:
static constexpr char tokenKey[] = "token";
static constexpr char valueKey[] = "value";
};
class LANGUAGESERVERPROTOCOL_EXPORT ProgressNotification : public Notification<ProgressParams>
{
public:
using Notification::Notification;
constexpr static const char methodName[] = "$/progress";
};
class LANGUAGESERVERPROTOCOL_EXPORT ProgressTokenParams : public JsonObject
{
public:
using JsonObject::JsonObject;
// The token to be used to report progress.
ProgressToken token() const { return typedValue<ProgressToken>(tokenKey); }
void setToken(const ProgressToken &token) { insert(tokenKey, token); }
private:
constexpr static const char tokenKey[] = "token";
};
using WorkDoneProgressCreateParams = ProgressTokenParams;
class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressCreateRequest
: public Request<std::nullptr_t, std::nullptr_t, WorkDoneProgressCreateParams>
{
public:
using Request::Request;
constexpr static const char methodName[] = "window/workDoneProgress/create";
};
using WorkDoneProgressCancelParams = ProgressTokenParams;
class LANGUAGESERVERPROTOCOL_EXPORT WorkDoneProgressCancelRequest
: public Request<std::nullptr_t, std::nullptr_t, WorkDoneProgressCancelParams>
{
public:
using Request::Request;
constexpr static const char methodName[] = "window/workDoneProgress/cancel";
};
} // namespace LanguageServerProtocol

View File

@@ -22,5 +22,6 @@ add_qtc_plugin(LanguageClient
languageclient_global.h
locatorfilter.cpp locatorfilter.h
lspinspector.cpp lspinspector.h
progressmanager.cpp progressmanager.h
semantichighlightsupport.cpp semantichighlightsupport.h
)

View File

@@ -40,6 +40,7 @@
#include <languageserverprotocol/messages.h>
#include <languageserverprotocol/servercapabilities.h>
#include <languageserverprotocol/workspace.h>
#include <languageserverprotocol/progresssupport.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <texteditor/codeassist/documentcontentcompletion.h>
@@ -54,6 +55,7 @@
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcprocess.h>
#include <utils/synchronousprocess.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <QDebug>
#include <QLoggingCategory>
@@ -246,6 +248,10 @@ static ClientCapabilities generateClientCapabilities()
documentCapabilities.setOnTypeFormatting(allowDynamicRegistration);
capabilities.setTextDocument(documentCapabilities);
WindowClientClientCapabilities window;
window.setWorkDoneProgress(true);
capabilities.setWindow(window);
return capabilities;
}
@@ -1110,6 +1116,16 @@ void Client::handleMethod(const QString &method, const MessageId &id, const ICon
}
response.setResult(result);
sendContent(response);
} else if (method == WorkDoneProgressCreateRequest::methodName) {
sendContent(WorkDoneProgressCreateRequest::Response(
dynamic_cast<const WorkDoneProgressCreateRequest *>(content)->id()));
} else if (method == ProgressNotification::methodName) {
if (Utils::optional<ProgressParams> params
= dynamic_cast<const ProgressNotification *>(content)->params()) {
if (!params->isValid())
logError(*params);
m_progressManager.handleProgress(*params);
}
} else if (id.isValid()) {
Response<JsonObject, JsonObject> response(id);
ResponseError<JsonObject> error;

View File

@@ -36,6 +36,7 @@
#include "languageclientquickfix.h"
#include "languageclientsettings.h"
#include "languageclientsymbolsupport.h"
#include "progressmanager.h"
#include <coreplugin/messagemanager.h>
@@ -47,6 +48,7 @@
#include <languageserverprotocol/initializemessages.h>
#include <languageserverprotocol/languagefeatures.h>
#include <languageserverprotocol/messages.h>
#include <languageserverprotocol/progresssupport.h>
#include <languageserverprotocol/shutdownmessages.h>
#include <languageserverprotocol/textsynchronization.h>
@@ -237,6 +239,7 @@ private:
const ProjectExplorer::Project *m_project = nullptr;
QSet<TextEditor::IAssistProcessor *> m_runningAssistProcessors;
SymbolSupport m_symbolSupport;
ProgressManager m_progressManager;
};
} // namespace LanguageClient

View File

@@ -22,7 +22,8 @@ HEADERS += \
languageclientutils.h \
locatorfilter.h \
lspinspector.h \
semantichighlightsupport.h
progressmanager.h \
semantichighlightsupport.h \
SOURCES += \
@@ -44,7 +45,8 @@ SOURCES += \
languageclientutils.cpp \
locatorfilter.cpp \
lspinspector.cpp \
semantichighlightsupport.cpp
progressmanager.cpp \
semantichighlightsupport.cpp \
RESOURCES += \
languageclient.qrc

View File

@@ -52,6 +52,8 @@ QtcPlugin {
"locatorfilter.h",
"lspinspector.cpp",
"lspinspector.h",
"progressmanager.cpp",
"progressmanager.h",
"semantichighlightsupport.cpp",
"semantichighlightsupport.h",
]

View File

@@ -33,6 +33,7 @@
#include <coreplugin/find/searchresultwindow.h>
#include <coreplugin/icore.h>
#include <languageserverprotocol/messages.h>
#include <languageserverprotocol/progresssupport.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
@@ -67,6 +68,8 @@ LanguageClientManager::LanguageClientManager(QObject *parent)
JsonRpcMessageHandler::registerMessageProvider<WorkSpaceFolderRequest>();
JsonRpcMessageHandler::registerMessageProvider<RegisterCapabilityRequest>();
JsonRpcMessageHandler::registerMessageProvider<UnregisterCapabilityRequest>();
JsonRpcMessageHandler::registerMessageProvider<WorkDoneProgressCreateRequest>();
JsonRpcMessageHandler::registerMessageProvider<ProgressNotification>();
connect(EditorManager::instance(), &EditorManager::editorOpened,
this, &LanguageClientManager::editorOpened);
connect(EditorManager::instance(), &EditorManager::documentOpened,

View File

@@ -0,0 +1,119 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "progressmanager.h"
#include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <languageserverprotocol/progresssupport.h>
using namespace LanguageServerProtocol;
namespace LanguageClient {
ProgressManager::ProgressManager()
{}
ProgressManager::~ProgressManager()
{
for (const ProgressToken &token : m_progress.keys())
endProgress(token);
}
void ProgressManager::handleProgress(const LanguageServerProtocol::ProgressParams &params)
{
const ProgressToken &token = params.token();
ProgressParams::ProgressType value = params.value();
if (auto begin = Utils::get_if<WorkDoneProgressBegin>(&value))
beginProgress(token, *begin);
else if (auto report = Utils::get_if<WorkDoneProgressReport>(&value))
reportProgress(token, *report);
else if (auto end = Utils::get_if<WorkDoneProgressEnd>(&value))
endProgress(token, *end);
}
Utils::Id languageClientProgressId(const ProgressToken &token)
{
constexpr char k_LanguageClientProgressId[] = "LanguageClient.ProgressId.";
auto toString = [](const ProgressToken &token){
if (Utils::holds_alternative<int>(token))
return QString::number(Utils::get<int>(token));
return Utils::get<QString>(token);
};
return Utils::Id(k_LanguageClientProgressId).withSuffix(toString(token));
}
void ProgressManager::beginProgress(const ProgressToken &token, const WorkDoneProgressBegin &begin)
{
auto interface = new QFutureInterface<void>();
interface->reportStarted();
interface->setProgressRange(0, 100); // LSP always reports percentage of the task
Core::FutureProgress *progress = Core::ProgressManager::addTask(
interface->future(), begin.title(), languageClientProgressId(token));
m_progress[token] = {progress, interface};
reportProgress(token, begin);
}
void ProgressManager::reportProgress(const ProgressToken &token,
const WorkDoneProgressReport &report)
{
const LanguageClientProgress &progress = m_progress.value(token);
if (progress.progressInterface) {
const Utils::optional<QString> &message = report.message();
if (message.has_value()) {
progress.progressInterface->setSubtitle(*message);
const bool showSubtitle = !message->isEmpty();
progress.progressInterface->setSubtitleVisibleInStatusBar(showSubtitle);
}
}
if (progress.futureInterface) {
const Utils::optional<int> &progressValue = report.percentage();
if (progressValue.has_value())
progress.futureInterface->setProgressValue(*progressValue);
}
}
void ProgressManager::endProgress(const ProgressToken &token, const WorkDoneProgressEnd &end)
{
const LanguageClientProgress &progress = m_progress.value(token);
const QString &message = end.message().value_or(QString());
if (!message.isEmpty() && progress.progressInterface) {
progress.progressInterface->setKeepOnFinish(
Core::FutureProgress::KeepOnFinishTillUserInteraction);
progress.progressInterface->setSubtitle(message);
progress.progressInterface->setSubtitleVisibleInStatusBar(true);
}
endProgress(token);
}
void ProgressManager::endProgress(const ProgressToken &token)
{
const LanguageClientProgress &progress = m_progress.take(token);
if (progress.futureInterface)
progress.futureInterface->reportFinished();
delete progress.futureInterface;
}
} // namespace LanguageClient

View File

@@ -0,0 +1,65 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <QFutureInterface>
namespace Core { class FutureProgress; }
namespace LanguageServerProtocol {
class ProgressParams;
class ProgressToken;
class WorkDoneProgressBegin;
class WorkDoneProgressReport;
class WorkDoneProgressEnd;
} // namespace LanguageServerProtocol
namespace LanguageClient {
class ProgressManager
{
public:
ProgressManager();
~ProgressManager();
void handleProgress(const LanguageServerProtocol::ProgressParams &params);
private:
void beginProgress(const LanguageServerProtocol::ProgressToken &token,
const LanguageServerProtocol::WorkDoneProgressBegin &begin);
void reportProgress(const LanguageServerProtocol::ProgressToken &token,
const LanguageServerProtocol::WorkDoneProgressReport &report);
void endProgress(const LanguageServerProtocol::ProgressToken &token,
const LanguageServerProtocol::WorkDoneProgressEnd &end);
void endProgress(const LanguageServerProtocol::ProgressToken &token);
struct LanguageClientProgress {
QPointer<Core::FutureProgress> progressInterface = nullptr;
QFutureInterface<void> *futureInterface = nullptr;
};
QMap<LanguageServerProtocol::ProgressToken, LanguageClientProgress> m_progress;
};
} // namespace LanguageClient