Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qworkspace.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
4#include "qworkspace_p.h"
6#include "qqmllsutils_p.h"
7
8#include <QtLanguageServer/private/qlanguageserverspectypes_p.h>
9#include <QtLanguageServer/private/qlspnotifysignals_p.h>
10
11#include <QtCore/qfile.h>
12#include <variant>
13
15using namespace Qt::StringLiterals;
16using namespace QLspSpecification;
17
19{
20 QObject::connect(server->notifySignals(),
21 &QLspNotifySignals::receivedDidChangeWorkspaceFoldersNotification, this,
22 [server, this](const DidChangeWorkspaceFoldersParams &params) {
23 const WorkspaceFoldersChangeEvent &event = params.event;
24
25 const QList<WorkspaceFolder> &removed = event.removed;
26 QList<QByteArray> toRemove;
27 for (const WorkspaceFolder &folder : removed) {
28 toRemove.append(QQmlLSUtils::lspUriToQmlUrl(folder.uri));
29 m_codeModel->removeDirectory(m_codeModel->url2Path(
30 QQmlLSUtils::lspUriToQmlUrl(folder.uri)));
31 }
32 m_codeModel->removeRootUrls(toRemove);
33 const QList<WorkspaceFolder> &added = event.added;
34 QList<QByteArray> toAdd;
35 QStringList pathsToAdd;
36 for (const WorkspaceFolder &folder : added) {
37 toAdd.append(QQmlLSUtils::lspUriToQmlUrl(folder.uri));
38 pathsToAdd.append(m_codeModel->url2Path(
39 QQmlLSUtils::lspUriToQmlUrl(folder.uri)));
40 }
41 m_codeModel->addRootUrls(toAdd);
42 m_codeModel->addDirectoriesToIndex(pathsToAdd, server);
43 });
44
45 QObject::connect(server->notifySignals(),
46 &QLspNotifySignals::receivedDidChangeWatchedFilesNotification, this,
47 [this](const DidChangeWatchedFilesParams &params) {
48 const QList<FileEvent> &changes = params.changes;
49 for (const FileEvent &change : changes) {
50 const QString filename =
51 m_codeModel->url2Path(QQmlLSUtils::lspUriToQmlUrl(change.uri));
52 switch (FileChangeType(change.type)) {
53 case FileChangeType::Created:
54 // m_codeModel->addFile(filename);
55 break;
56 case FileChangeType::Changed: {
57 QFile file(filename);
58 if (file.open(QIODevice::ReadOnly))
59 // m_modelManager->setFileContents(filename, file.readAll());
60 break;
61 break;
62 }
63 case FileChangeType::Deleted:
64 // m_modelManager->removeFile(filename);
65 break;
66 }
67 }
68 // update due to dep changes...
69 });
70
73}
74
76{
77 return u"Workspace"_s;
78}
79
80void WorkspaceHandlers::setupCapabilities(const QLspSpecification::InitializeParams &clientInfo,
81 QLspSpecification::InitializeResult &serverInfo)
82{
83 if (!clientInfo.capabilities.workspace
84 || !clientInfo.capabilities.workspace->value(u"workspaceFolders"_s).toBool(false))
85 return;
86 WorkspaceFoldersServerCapabilities folders;
87 folders.supported = true;
88 folders.changeNotifications = true;
89 if (!serverInfo.capabilities.workspace)
90 serverInfo.capabilities.workspace = QJsonObject();
91 serverInfo.capabilities.workspace->insert(u"workspaceFolders"_s,
92 QTypedJson::toJsonValue(folders));
93}
94
96{
97 QLanguageServerProtocol *protocol = server->protocol();
98 const auto clientInfo = server->clientInfo();
99 QList<Registration> registrations;
100 if (clientInfo.capabilities.workspace
101 && clientInfo.capabilities.workspace
102 ->value(u"didChangeWatchedFiles"_s)[u"dynamicRegistration"_s]
103 .toBool(false)) {
104 const int watchAll =
105 int(WatchKind::Create) | int(WatchKind::Change) | int(WatchKind::Delete);
106 DidChangeWatchedFilesRegistrationOptions watchedFilesParams;
107 FileSystemWatcher qmlWatcher;
108 qmlWatcher.globPattern = QByteArray("*.{qml,js,mjs}");
109 qmlWatcher.kind = watchAll;
110 FileSystemWatcher qmldirWatcher;
111 qmldirWatcher.globPattern = "qmldir";
112 qmldirWatcher.kind = watchAll;
113 FileSystemWatcher qmltypesWatcher;
114 qmltypesWatcher.globPattern = QByteArray("*.qmltypes");
115 qmltypesWatcher.kind = watchAll;
116 watchedFilesParams.watchers = QList<FileSystemWatcher>({
117 std::move(qmlWatcher),
118 std::move(qmldirWatcher),
119 std::move(qmltypesWatcher)
120 });
121 registrations.append(Registration {
122 // use ClientCapabilitiesInfo::WorkspaceDidChangeWatchedFiles as id too
123 ClientCapabilitiesInfo::WorkspaceDidChangeWatchedFiles,
124 ClientCapabilitiesInfo::WorkspaceDidChangeWatchedFiles,
125 QTypedJson::toJsonValue(watchedFilesParams) });
126 }
127
128 if (!registrations.isEmpty()) {
129 RegistrationParams params;
130 params.registrations = registrations;
131 protocol->requestRegistration(
132 params,
133 []() {
134 // successful registration
135 },
136 [protocol](const ResponseError &err) {
137 LogMessageParams msg;
138 msg.message = QByteArray("registration of file udates failed, will miss file "
139 "changes done outside the editor due to error ");
140 msg.message.append(QString::number(err.code).toUtf8());
141 if (!err.message.isEmpty())
142 msg.message.append(" ");
143 msg.message.append(err.message);
144 msg.type = MessageType::Warning;
145 qCWarning(lspServerLog) << QString::fromUtf8(msg.message);
146 protocol->notifyLogMessage(msg);
147 });
148 }
149
150 QSet<QString> rootPaths;
151 if (std::holds_alternative<QByteArray>(clientInfo.rootUri)) {
152 QString path = m_codeModel->url2Path(
153 QQmlLSUtils::lspUriToQmlUrl(std::get<QByteArray>(clientInfo.rootUri)));
154 rootPaths.insert(path);
155 } else if (clientInfo.rootPath && std::holds_alternative<QByteArray>(*clientInfo.rootPath)) {
156 QString path = QString::fromUtf8(std::get<QByteArray>(*clientInfo.rootPath));
157 rootPaths.insert(path);
158 }
159
160 if (clientInfo.workspaceFolders
161 && std::holds_alternative<QList<WorkspaceFolder>>(*clientInfo.workspaceFolders)) {
162 for (const WorkspaceFolder &workspace :
163 std::as_const(std::get<QList<WorkspaceFolder>>(*clientInfo.workspaceFolders))) {
164 const QUrl workspaceUrl(QString::fromUtf8(QQmlLSUtils::lspUriToQmlUrl(workspace.uri)));
165 rootPaths.insert(workspaceUrl.toLocalFile());
166 }
167 }
168 if (m_status == Status::Indexing)
169 m_codeModel->addDirectoriesToIndex(QStringList(rootPaths.begin(), rootPaths.end()), server);
170}
171
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
Implements a server for the language server protocol.
void clientInitialized(QLanguageServer *server)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static QByteArray lspUriToQmlUrl(const QByteArray &uri)
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Definition qurl.h:94
void addRootUrls(const QList< QByteArray > &urls)
QString url2Path(const QByteArray &url, UrlLookup options=UrlLookup::Caching)
void removeRootUrls(const QList< QByteArray > &urls)
void addDirectoriesToIndex(const QStringList &paths, QLanguageServer *server)
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
void clientInitialized(QLanguageServer *)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
QString name() const override
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qCWarning(category,...)
void ** params
GLsizei const GLchar *const * path