5#include <QtCore/qstring.h>
9using namespace Qt::StringLiterals;
13QQmlProgressSupport::QQmlProgressSupport(QmlLsp::QQmlCodeModelManager *manager)
14 : m_codeModelManager(manager)
19 QLanguageServerProtocol *protocol)
21 m_protocol = protocol;
22 QObject::connect(server, &QLanguageServer::clientInitialized,
this,
23 &QQmlProgressSupport::clientInitialized);
28 if (
auto window = server->clientInfo().capabilities.window;
29 !window || !window->value(
"workDoneProgress"_L1).toBool(
false)) {
33 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildStarted,
34 this, &QQmlProgressSupport::onBackgroundBuildStarted);
35 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildFinished,
36 this, &QQmlProgressSupport::onBackgroundBuildDone);
38 QObject::connect(server->notifySignals(),
39 &QLspNotifySignals::receivedWorkDoneProgressCancelNotification,
this,
40 &QQmlProgressSupport::onBackgroundBuildCancelRequested);
45 const int token = m_idForBackgroundBuilds++;
46 m_tokens.insert(uri, { uri, token, InCreation });
47 m_uriByToken.insert(token, uri);
53 const auto it = m_tokens.find(uri);
54 return it == m_tokens.end() ?
nullptr : &*it;
57std::optional<QQmlProgressSupport::UriWithToken>
QQmlProgressSupport::Tokens::takeToken(
int token)
59 const auto it = m_uriByToken.find(token);
60 if (it == m_uriByToken.end())
63 const auto it2 = m_tokens.find(*it);
64 m_uriByToken.erase(it);
65 if (it2 == m_tokens.end())
67 std::optional<QQmlProgressSupport::UriWithToken> result = std::move(*it2);
74 const auto it = m_tokens.find(uri);
75 if (it != m_tokens.end())
78 if (
const auto it2 = m_uriByToken.find(it->token); it2 != m_uriByToken.end())
79 m_uriByToken.erase(it2);
85 QLspSpecification::Requests::WorkDoneProgressCreateParamsType p;
86 const int token = m_tokens.createUniqueToken(uri);
88 m_protocol->requestWorkDoneProgressCreate(p, [
this, uri, token]() {
89 QLspSpecification::ProgressParams beginParams{ token };
90 QLspSpecification::WorkDoneProgressBegin workDoneProgressBegin{};
91 workDoneProgressBegin.title =
"Qmlls running background build";
92 workDoneProgressBegin.cancellable =
true;
93 workDoneProgressBegin.message =
94 "Building \"" + QUrl::fromEncoded(uri).toLocalFile().toUtf8() +
"\"";
95 workDoneProgressBegin.cancellable =
true;
96 beginParams.value = workDoneProgressBegin;
97 m_protocol->notifyProgress(beginParams);
99 const auto token = m_tokens.find(uri);
103 switch (token->status) {
105 token->status = Created;
110 onBackgroundBuildDone(token->uri);
118 const auto token = m_tokens.find(uri);
122 switch (token->status) {
126 token->status = Finished;
130 QLspSpecification::WorkDoneProgressEnd workDoneProgressEnd;
131 workDoneProgressEnd.message =
"Build terminated";
132 const QLspSpecification::ProgressParams endParams{ token->token, workDoneProgressEnd };
133 m_protocol->notifyProgress(endParams);
134 m_tokens.removeToken(token->uri);
139 const QLspSpecification::Notifications::WorkDoneProgressCancelParamsType &p)
141 const auto tokenNumber = std::get_if<
int>(&p.token);
143 qCWarning(lspServerLog) <<
"Ignoring unknown token" << std::get<QByteArray>(p.token)
144 <<
"in cancellation request.";
148 const auto token = m_tokens.takeToken(*tokenNumber);
150 qCWarning(lspServerLog) <<
"Ignoring unknown token" << *tokenNumber
151 <<
"in cancellation request.";
155 m_codeModelManager->cancelBackgroundBuild(token->uri);
Implements a server for the language server protocol.
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) final
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) final
QQmlProgressSupport(QmlLsp::QQmlCodeModelManager *manager)