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{
20 return "QQmlProgress"_L1;
21}
22
24 QLanguageServerProtocol *protocol)
25{
26 m_protocol = protocol;
27 QObject::connect(server, &QLanguageServer::clientInitialized, this,
28 &QQmlProgressSupport::clientInitialized);
29}
30
31void QQmlProgressSupport::clientInitialized(QLanguageServer *server)
32{
33 if (auto window = server->clientInfo().capabilities.window;
34 !window || !window->value("workDoneProgress"_L1).toBool(false)) {
35 return;
36 }
37
38 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildStarted,
39 this, &QQmlProgressSupport::onBackgroundBuildStarted);
40 QObject::connect(m_codeModelManager, &QmlLsp::QQmlCodeModelManager::backgroundBuildFinished,
41 this, &QQmlProgressSupport::onBackgroundBuildDone);
42
43 QObject::connect(server->notifySignals(),
44 &QLspNotifySignals::receivedWorkDoneProgressCancelNotification, this,
45 &QQmlProgressSupport::onBackgroundBuildCancelRequested);
46}
47
48int QQmlProgressSupport::createUniqueToken(const QByteArray &uri)
49{
50 const int token = m_idForBackgroundBuilds++;
51 m_tokens.append({ uri, token });
52 return token;
53}
54
55void QQmlProgressSupport::onBackgroundBuildStarted(const QByteArray &uri)
56{
57 QLspSpecification::Requests::WorkDoneProgressCreateParamsType p;
58 const int token = createUniqueToken(uri);
59 p.token = token;
60 m_protocol->requestWorkDoneProgressCreate(p, [this, uri, token]() {
61 QLspSpecification::ProgressParams beginParams{ token };
62 QLspSpecification::WorkDoneProgressBegin workDoneProgressBegin{};
63 workDoneProgressBegin.title = "Qmlls running background build";
64 workDoneProgressBegin.cancellable = true;
65 workDoneProgressBegin.message =
66 "Building \"" + QUrl::fromEncoded(uri).toLocalFile().toUtf8() + "\"";
67 workDoneProgressBegin.cancellable = true;
68 beginParams.value = workDoneProgressBegin;
69 m_protocol->notifyProgress(beginParams);
70 });
71}
72
73void QQmlProgressSupport::onBackgroundBuildDone(const QByteArray &uri)
74{
75 const auto it =
76 std::find_if(m_tokens.begin(), m_tokens.end(), [uri](const UriWithToken &uriWithToken) {
77 return uriWithToken.uri == uri;
78 });
79
80 if (it == m_tokens.end())
81 return;
82
83 QLspSpecification::WorkDoneProgressEnd workDoneProgressEnd;
84 workDoneProgressEnd.message = "Build terminated";
85 const QLspSpecification::ProgressParams endParams{ it->token, workDoneProgressEnd };
86 m_protocol->notifyProgress(endParams);
87 m_tokens.erase(it);
88}
89
90void QQmlProgressSupport::onBackgroundBuildCancelRequested(
91 const QLspSpecification::Notifications::WorkDoneProgressCancelParamsType &p)
92{
93 const auto token = std::get_if<int>(&p.token);
94 if (!token)
95 return;
96
97 const auto it = std::find_if(
98 m_tokens.begin(), m_tokens.end(),
99 [token](const UriWithToken &uriWithToken) { return uriWithToken.token == *token; });
100
101 if (it == m_tokens.end())
102 return;
103
104 m_codeModelManager->cancelBackgroundBuild(it->uri);
105 m_tokens.erase(it);
106}
107
108void QQmlProgressSupport::setupCapabilities(const QLspSpecification::InitializeParams &,
109 QLspSpecification::InitializeResult &)
110{
111}
112
113QT_END_NAMESPACE
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