Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlprogresssupport.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtCore/qstring.h>
6
8
9using namespace Qt::StringLiterals;
10
11QT_BEGIN_NAMESPACE
12
13QQmlProgressSupport::QQmlProgressSupport(QmlLsp::QQmlCodeModelManager *manager)
14 : m_codeModelManager(manager)
15{
16}
17
19 QLanguageServerProtocol *protocol)
20{
21 m_protocol = protocol;
22 QObject::connect(server, &QLanguageServer::clientInitialized, this,
23 &QQmlProgressSupport::clientInitialized);
24}
25
26void QQmlProgressSupport::clientInitialized(QLanguageServer *server)
27{
28 if (auto window = server->clientInfo().capabilities.window;
29 !window || !window->workDoneProgress.value_or(false)) {
30 return;
31 }
32
33 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildStarted,
34 this, &QQmlProgressSupport::onBackgroundBuildStarted);
35 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildFinished,
36 this, &QQmlProgressSupport::onBackgroundBuildDone);
37
38 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::moduleExplorationStarted,
39 this, &QQmlProgressSupport::onModuleExplorationStarted);
40 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::moduleExplorationFinished,
41 this, &QQmlProgressSupport::onModuleExplorationFinished);
42 if (m_codeModelManager->isExploringModules())
43 onModuleExplorationStarted();
44
45 QObject::connect(server->notifySignals(),
46 &QLspNotifySignals::receivedWorkDoneProgressCancelNotification, this,
47 &QQmlProgressSupport::onBackgroundBuildCancelRequested);
48}
49
50int QQmlProgressSupport::Tokens::createUniqueToken(const QByteArray &uri)
51{
52 const int token = m_idForBackgroundBuilds++;
53 m_tokens.insert(uri, { uri, token, InCreation });
54 m_uriByToken.insert(token, uri);
55 return token;
56}
57
58QQmlProgressSupport::UriWithToken *QQmlProgressSupport::Tokens::find(const QByteArray &uri)
59{
60 const auto it = m_tokens.find(uri);
61 return it == m_tokens.end() ? nullptr : &*it;
62}
63
64std::optional<QQmlProgressSupport::UriWithToken> QQmlProgressSupport::Tokens::takeToken(int token)
65{
66 const auto it = m_uriByToken.find(token);
67 if (it == m_uriByToken.end())
68 return {};
69
70 const auto it2 = m_tokens.find(*it);
71 m_uriByToken.erase(it);
72 if (it2 == m_tokens.end())
73 return {};
74 std::optional<QQmlProgressSupport::UriWithToken> result = std::move(*it2);
75 m_tokens.erase(it2);
76 return result;
77}
78
79void QQmlProgressSupport::Tokens::removeToken(const QByteArray &uri)
80{
81 const auto it = m_tokens.find(uri);
82 if (it == m_tokens.end())
83 return;
84
85 if (const auto it2 = m_uriByToken.find(it->token); it2 != m_uriByToken.end())
86 m_uriByToken.erase(it2);
87 m_tokens.erase(it);
88}
89
90const QByteArray QQmlProgressSupport::s_moduleExplorationKey = "qmlls:module-exploration"_ba;
91
92void QQmlProgressSupport::beginProgress(const QByteArray &uri,
93 const ProgressDescription &description)
94{
95 QLspSpecification::Requests::WorkDoneProgressCreateParamsType p;
96 const int token = m_tokens.createUniqueToken(uri);
97 p.token = token;
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);
106
107 const auto token = m_tokens.find(uri);
108 if (!token)
109 return;
110
111 switch (token->status) {
112 case InCreation:
113 token->status = Created;
114 break;
115 case Created:
116 Q_ASSERT(false);
117 case Finished:
118 endProgress(token->uri, m_pendingEndMessages.take(token->uri));
119 break;
120 }
121 });
122}
123
124void QQmlProgressSupport::endProgress(const QByteArray &uri, const QByteArray &message)
125{
126 const auto token = m_tokens.find(uri);
127 if (!token)
128 return;
129
130 switch (token->status) {
131 case InCreation:
132 // We can't report progress if the WorkDoneProgressCreate request didn't finish yet. Let the
133 // WorkDoneProgressCreate callback call this method again after the request was created.
134 token->status = Finished;
135 m_pendingEndMessages.insert(uri, message);
136 return;
137 case Finished:
138 case Created:
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);
144 }
145}
146
147void QQmlProgressSupport::onBackgroundBuildStarted(const QByteArray &uri)
148{
149 beginProgress(uri,
150 { "Qmlls running background build",
151 "Building \"" + QUrl::fromEncoded(uri).toLocalFile().toUtf8() + "\"", true });
152}
153
154void QQmlProgressSupport::onBackgroundBuildDone(const QByteArray &uri)
155{
156 endProgress(uri, "Build terminated");
157}
158
159void QQmlProgressSupport::onModuleExplorationStarted()
160{
161 beginProgress(s_moduleExplorationKey,
162 { "Qmlls collecting QML modules",
163 "Searching the import paths for importable QML modules", true });
164}
165
166void QQmlProgressSupport::onModuleExplorationFinished()
167{
168 endProgress(s_moduleExplorationKey, "Finished collecting QML modules");
169}
170
171void QQmlProgressSupport::onBackgroundBuildCancelRequested(
172 const QLspSpecification::Notifications::WorkDoneProgressCancelParamsType &p)
173{
174 const auto tokenNumber = std::get_if<int>(&p.token);
175 if (!tokenNumber) {
176 qCWarning(lspServerLog) << "Ignoring unknown token" << std::get<QByteArray>(p.token)
177 << "in cancellation request.";
178 return;
179 }
180
181 const auto token = m_tokens.takeToken(*tokenNumber);
182 if (!token) {
183 qCWarning(lspServerLog) << "Ignoring unknown token" << *tokenNumber
184 << "in cancellation request.";
185 return;
186 }
187
188 if (token->uri == s_moduleExplorationKey)
189 m_codeModelManager->cancelModuleExploration();
190 else
191 m_codeModelManager->cancelBackgroundBuild(token->uri);
192}
193
194void QQmlProgressSupport::setupCapabilities(QLspSpecification::ServerCapabilities &) { }
195
196QT_END_NAMESPACE
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)