5#include <QtCore/qstring.h>
9using namespace Qt::StringLiterals;
13QQmlProgressSupport::QQmlProgressSupport(QmlLsp::QQmlCodeModelManager *manager)
14 : m_codeModelManager(manager)
20 return "QQmlProgress"_L1;
24 QLanguageServerProtocol *protocol)
26 m_protocol = protocol;
27 QObject::connect(server, &QLanguageServer::clientInitialized,
this,
28 &QQmlProgressSupport::clientInitialized);
33 if (
auto window = server->clientInfo().capabilities.window;
34 !window || !window->value(
"workDoneProgress"_L1).toBool(
false)) {
38 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildStarted,
39 this, &QQmlProgressSupport::onBackgroundBuildStarted);
40 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildFinished,
41 this, &QQmlProgressSupport::onBackgroundBuildDone);
43 QObject::connect(server->notifySignals(),
44 &QLspNotifySignals::receivedWorkDoneProgressCancelNotification,
this,
45 &QQmlProgressSupport::onBackgroundBuildCancelRequested);
50 const int token = m_idForBackgroundBuilds++;
51 m_tokens.insert(uri, { uri, token, InCreation });
52 m_uriByToken.insert(token, uri);
58 const auto it = m_tokens.find(uri);
59 return it == m_tokens.end() ?
nullptr : &*it;
62std::optional<QQmlProgressSupport::UriWithToken>
QQmlProgressSupport::Tokens::takeToken(
int token)
64 const auto it = m_uriByToken.find(token);
65 if (it == m_uriByToken.end())
68 const auto it2 = m_tokens.find(*it);
69 m_uriByToken.erase(it);
70 if (it2 == m_tokens.end())
72 std::optional<QQmlProgressSupport::UriWithToken> result = std::move(*it2);
79 const auto it = m_tokens.find(uri);
80 if (it != m_tokens.end())
83 if (
const auto it2 = m_uriByToken.find(it->token); it2 != m_uriByToken.end())
84 m_uriByToken.erase(it2);
90 QLspSpecification::Requests::WorkDoneProgressCreateParamsType p;
91 const int token = m_tokens.createUniqueToken(uri);
93 m_protocol->requestWorkDoneProgressCreate(p, [
this, uri, token]() {
94 QLspSpecification::ProgressParams beginParams{ token };
95 QLspSpecification::WorkDoneProgressBegin workDoneProgressBegin{};
96 workDoneProgressBegin.title =
"Qmlls running background build";
97 workDoneProgressBegin.cancellable =
true;
98 workDoneProgressBegin.message =
99 "Building \"" + QUrl::fromEncoded(uri).toLocalFile().toUtf8() +
"\"";
100 workDoneProgressBegin.cancellable =
true;
101 beginParams.value = workDoneProgressBegin;
102 m_protocol->notifyProgress(beginParams);
104 const auto token = m_tokens.find(uri);
108 switch (token->status) {
110 token->status = Created;
115 onBackgroundBuildDone(token->uri);
123 const auto token = m_tokens.find(uri);
127 switch (token->status) {
131 token->status = Finished;
135 QLspSpecification::WorkDoneProgressEnd workDoneProgressEnd;
136 workDoneProgressEnd.message =
"Build terminated";
137 const QLspSpecification::ProgressParams endParams{ token->token, workDoneProgressEnd };
138 m_protocol->notifyProgress(endParams);
139 m_tokens.removeToken(token->uri);
144 const QLspSpecification::Notifications::WorkDoneProgressCancelParamsType &p)
146 const auto tokenNumber = std::get_if<
int>(&p.token);
148 qCWarning(lspServerLog) <<
"Ignoring unknown token" << std::get<QByteArray>(p.token)
149 <<
"in cancellation request.";
153 const auto token = m_tokens.takeToken(*tokenNumber);
155 qCWarning(lspServerLog) <<
"Ignoring unknown token" << *tokenNumber
156 <<
"in cancellation request.";
160 m_codeModelManager->cancelBackgroundBuild(token->uri);
164 QLspSpecification::InitializeResult &)
Implements a server for the language server protocol.
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) final
QQmlProgressSupport(QmlLsp::QQmlCodeModelManager *manager)
QString name() const final
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) final