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->workDoneProgress.value_or(
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(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::moduleExplorationStarted,
39 this, &QQmlProgressSupport::onModuleExplorationStarted);
40 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::moduleExplorationFinished,
41 this, &QQmlProgressSupport::onModuleExplorationFinished);
43 onModuleExplorationStarted();
45 QObject::connect(server->notifySignals(),
46 &QLspNotifySignals::receivedWorkDoneProgressCancelNotification,
this,
47 &QQmlProgressSupport::onBackgroundBuildCancelRequested);
52 const int token = m_idForBackgroundBuilds++;
53 m_tokens.insert(uri, { uri, token, InCreation });
54 m_uriByToken.insert(token, uri);
60 const auto it = m_tokens.find(uri);
61 return it == m_tokens.end() ?
nullptr : &*it;
64std::optional<QQmlProgressSupport::UriWithToken>
QQmlProgressSupport::Tokens::takeToken(
int token)
66 const auto it = m_uriByToken.find(token);
67 if (it == m_uriByToken.end())
70 const auto it2 = m_tokens.find(*it);
71 m_uriByToken.erase(it);
72 if (it2 == m_tokens.end())
74 std::optional<QQmlProgressSupport::UriWithToken> result = std::move(*it2);
81 const auto it = m_tokens.find(uri);
82 if (it == m_tokens.end())
85 if (
const auto it2 = m_uriByToken.find(it->token); it2 != m_uriByToken.end())
86 m_uriByToken.erase(it2);
90const QByteArray
QQmlProgressSupport::s_moduleExplorationKey =
"qmlls:module-exploration"_ba;
93 const ProgressDescription &description)
95 QLspSpecification::Requests::WorkDoneProgressCreateParamsType p;
96 const int token = m_tokens.createUniqueToken(uri);
98 m_protocol->requestWorkDoneProgressCreate(p, [
this, uri, token, description]() {
99 QLspSpecification::ProgressParams beginParams{ token };
100 QLspSpecification::WorkDoneProgressBegin workDoneProgressBegin{};
101 workDoneProgressBegin.title = description.title;
102 workDoneProgressBegin.cancellable = description.cancellable;
103 workDoneProgressBegin.message = description.message;
104 beginParams.value = workDoneProgressBegin;
105 m_protocol->notifyProgress(beginParams);
107 const auto token = m_tokens.find(uri);
111 switch (token->status) {
113 token->status = Created;
118 endProgress(token->uri, m_pendingEndMessages.take(token->uri));
126 const auto token = m_tokens.find(uri);
130 switch (token->status) {
134 token->status = Finished;
135 m_pendingEndMessages.insert(uri, message);
139 QLspSpecification::WorkDoneProgressEnd workDoneProgressEnd;
140 workDoneProgressEnd.message = message;
141 const QLspSpecification::ProgressParams endParams{ token->token, workDoneProgressEnd };
142 m_protocol->notifyProgress(endParams);
143 m_tokens.removeToken(token->uri);
150 {
"Qmlls running background build",
151 "Building \"" + QUrl::fromEncoded(uri).toLocalFile().toUtf8() +
"\"",
true });
156 endProgress(uri,
"Build terminated");
161 beginProgress(s_moduleExplorationKey,
162 {
"Qmlls collecting QML modules",
163 "Searching the import paths for importable QML modules",
true });
168 endProgress(s_moduleExplorationKey,
"Finished collecting QML modules");
172 const QLspSpecification::Notifications::WorkDoneProgressCancelParamsType &p)
174 const auto tokenNumber = std::get_if<
int>(&p.token);
176 qCWarning(lspServerLog) <<
"Ignoring unknown token" << std::get<QByteArray>(p.token)
177 <<
"in cancellation request.";
181 const auto token = m_tokens.takeToken(*tokenNumber);
183 qCWarning(lspServerLog) <<
"Ignoring unknown token" << *tokenNumber
184 <<
"in cancellation request.";
188 if (token->uri == s_moduleExplorationKey)
191 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)
void cancelModuleExploration()
bool isExploringModules() const