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
qqmllscodeaction.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
6
8
9Q_STATIC_LOGGING_CATEGORY(lsCodeActionSupport, "qt.languageserver.codeactionsupport")
10
11struct Tr
12{
13 Q_DECLARE_TR_FUNCTIONS(QQmlCodeActions)
14};
15
16using namespace Qt::StringLiterals;
17using namespace QLspSpecification;
18using namespace QQmlJS::Dom;
19
20using CodeActions = QList<std::variant<Command, CodeAction>>;
21using TextEdits = decltype(std::declval<TextDocumentEdit>().edits);
22
23static QList<std::pair<QString, QQmlJS::SourceLocation>>
24collectNestedIds(const QQmlLSUtils::ItemLocation &item)
25{
26 const auto filePtr =
27 item.domItem.goToFile(item.domItem.canonicalFilePath()).ownerAs<QQmlJS::Dom::QmlFile>();
28 const QString code = filePtr ? filePtr->code() : QString();
29
30 QList<std::pair<QString, QQmlJS::SourceLocation>> innerIds;
31 FileLocations::visitTree(
32 item.fileLocation,
33 [&code, &innerIds](const auto &, const FileLocations::Tree &t) -> bool {
34 const auto idNameLoc = t->info().regions[FileLocationRegion::IdNameRegion];
35 if (idNameLoc.isValid()) {
36 innerIds.append({ code.mid(idNameLoc.begin(), idNameLoc.length), idNameLoc });
37 }
38 return true;
39 });
40 return innerIds;
41}
42
43static CodeActions quickfixes(const QList<Diagnostic> &diagnostics)
44{
45 CodeActions codeActions;
46
47 for (const Diagnostic &diagnostic : diagnostics) {
48 if (!diagnostic.data.has_value())
49 continue;
50
51 const auto &data = diagnostic.data.value();
52
53 int version = data[u"version"].toInt();
54 QJsonArray suggestions = data[u"suggestions"].toArray();
55
56 QList<WorkspaceEdit::DocumentChange> edits;
57 QString message;
58 for (const QJsonValue &suggestion : std::as_const(suggestions)) {
59 message += suggestion[u"message"_s].toString() + u'\n';
60 const auto &documentEdits = suggestion[u"documentEdits"_s].toArray();
61 TextDocumentEdit textDocEdit;
62 for (const auto &documentEdit : documentEdits) {
63 TextEdit textEdit;
64 textEdit.range = {
65 Position{
66 static_cast<unsigned>(documentEdit[u"lspBeginLine"].toDouble()),
67 static_cast<unsigned>(documentEdit[u"lspBeginCharacter"].toDouble()) },
68 Position{ static_cast<unsigned>(documentEdit[u"lspEndLine"].toDouble()),
69 static_cast<unsigned>(documentEdit[u"lspEndCharacter"].toDouble()) }
70 };
71
72 textEdit.newText = documentEdit[u"replacement"_s].toString().toUtf8();
73 QString filename = documentEdit[u"filename"_s].toString();
74 textDocEdit.textDocument = { { filename.toUtf8() }, version };
75 textDocEdit.edits.append(textEdit);
76 }
77 edits.append(textDocEdit);
78 }
79 message.chop(1);
80 WorkspaceEdit edit;
81 edit.documentChanges = edits;
82
83 CodeAction action;
84 // VS Code and QtC ignore everything that is not a 'quickfix'.
85 action.kind = CodeActionKind::QuickFix;
86 action.edit = edit;
87 action.title = message.toUtf8();
88
89 codeActions.append(action);
90 }
91 return codeActions;
92}
93
94static TextEdit todoComment(const Position &pos, const QString &loaderId, const QString &maybeId,
96{
97 const QString todo = Tr::tr("TODO:");
98 const QString prefix = "//"_L1 + QString(2 + todo.size(), u' ');
99
100 QString comment = "// "_L1 + todo + u' '
101 + Tr::tr("Move position bindings from the component to the Loader.") + u'\n'
102 + prefix
103 + Tr::tr("Check all uses of 'parent' inside the root element of the component.") + u'\n';
104
105 if (!maybeId.isEmpty()) {
106 comment += prefix
107 + Tr::tr("Rename all outer uses of the id \"%1\" to \"%2.item\".").arg(maybeId, loaderId) + u'\n';
108 }
109 for (const auto &id : nestedIds) {
110 comment += prefix
111 + Tr::tr("Rename all outer uses of the id \"%1\" to \"%2.item.%1\".").arg(id.first, loaderId) + u'\n';
112 }
113
114 return { { pos, pos }, comment.toUtf8() };
115}
116
117static TextEdits wrapIntoComponent(const Range &itemRange, const QString &componentId)
118{
119 const QString componentOpen = QString::fromLatin1("Component {\n"
120 " id: %1\n")
121 .arg(componentId);
122 const QString componentClose = QString::fromLatin1("\n}\n");
123 return { TextEdit{ { itemRange.start, itemRange.start }, componentOpen.toUtf8() },
124 TextEdit{ { itemRange.end, itemRange.end }, componentClose.toUtf8() } };
125}
126
127static TextEdit addLoader(const Position &pos, const QString &loaderId, const QString &componentId)
128{
129 const QString loader = QString::fromLatin1("Loader {\n"
130 " id: %2\n"
131 " sourceComponent: %1\n"
132 "}\n")
133 .arg(componentId, loaderId);
134
135 return { { pos, pos }, loader.toUtf8() };
136}
137
138static TextEdits exposeNestedIds(const QQmlJS::SourceLocation &openingBrace,
140{
141 const QLatin1StringView nestedIdPrefix("inner_");
142 QString idAliases = QString::fromLatin1("\n");
143 for (auto &id : nestedIds) {
144 idAliases += QString::fromLatin1("property alias %1: %2%1\n").arg(id.first, nestedIdPrefix);
145 }
146
147 TextEdits edits;
148
149 const auto posAfterLBrace =
150 Position{ static_cast<unsigned int>(static_cast<int>(openingBrace.startLine - 1)),
151 static_cast<unsigned int>(
152 static_cast<int>(openingBrace.startColumn)) /* after { */ };
153 // edit introducing property aliases
154 edits.append(TextEdit{ { posAfterLBrace, posAfterLBrace }, idAliases.toUtf8() });
155
156 // edits appending prefix "inner_" to each nested id
157 for (auto &id : nestedIds) {
158 const auto idPos =
159 Position{ static_cast<unsigned int>(static_cast<int>(id.second.startLine - 1)),
160 static_cast<unsigned int>(static_cast<int>(id.second.startColumn - 1)) };
161 edits.append(TextEdit{ { idPos, idPos }, nestedIdPrefix.toUtf8() });
162 }
163 return edits;
164}
165
166static TextEdits wrapInLoaderTextEdits(const QQmlLSUtils::ItemLocation &item)
167{
168 const auto generateId = [&item](const QString &base) -> QString {
169 const auto ids = item.domItem.component().field(Fields::ids).keys();
170 if (!ids.contains(base)) {
171 return base;
172 };
173 int extraNumber = 1;
174 for (; extraNumber < ids.size(); ++extraNumber) {
175 QString id = base + QString::number(extraNumber);
176 if (!ids.contains(id)) {
177 return id;
178 }
179 }
180 return base + QString::number(extraNumber);
181 };
182
183 const auto objId = item.domItem.idStr();
184 const auto objName = objId.isEmpty() ? item.domItem.name() : objId;
185 const QString componentId = generateId(QLatin1StringView("component_") + objName);
186 const QString loaderId = generateId(QLatin1StringView("loader_") + objName);
187
188 const auto itemRange = QQmlLSUtils::qmlLocationToLspLocation(
189 QQmlLSUtils::Location::tryFrom(item.domItem.canonicalFilePath(),
190 item.fileLocation->info().fullRegion, item.domItem)
191 .value_or(QQmlLSUtils::Location{}));
192
193 QList<std::pair<QString, QQmlJS::SourceLocation>> nestedIds = collectNestedIds(item);
194 if (!objId.isEmpty()) {
195 // We expect the first found nested Id to be an object id.
196 // Watch out for collectNestedIds changes
197 Q_ASSERT(nestedIds.front().first == objId);
198 nestedIds.removeFirst();
199 }
200
201 TextEdits edits;
202 edits << todoComment(itemRange.start, loaderId, objId, nestedIds)
203 << exposeNestedIds(item.fileLocation->info().regions[FileLocationRegion::LeftBraceRegion],
204 nestedIds)
205 << wrapIntoComponent(itemRange, componentId)
206 << addLoader(itemRange.end, loaderId, componentId);
207 return edits;
208}
209
210static inline std::optional<QQmlLSUtils::ItemLocation>
211qmlObjectDefinedAt(const QQmlLSUtils::ItemLocation &item)
212{
213 if (item.domItem.internalKind() != DomType::ScriptIdentifierExpression) {
214 return std::nullopt;
215 }
216 auto parentObject = item.domItem.qmlObject();
217 const auto objectLoc = FileLocations::treeOf(parentObject);
218
219 if (item.fileLocation->info().fullRegion.begin() != objectLoc->info().fullRegion.begin()) {
220 return std::nullopt;
221 }
222
223 return QQmlLSUtils::ItemLocation{ parentObject, objectLoc };
224}
225
226static CodeActions
227wrapComponentInLoader(const OptionalVersionedTextDocumentIdentifier &textDocument,
228 const QQmlLSUtils::ItemLocation &item)
229{
230 if (item.domItem.internalKind() != DomType::QmlObject) {
231 // If the current item is actually the identifier that defines a QML object,
232 // treat it as if the object itself was selected
233 if (auto qmlObject = qmlObjectDefinedAt(item)) {
234 return wrapComponentInLoader(textDocument, *qmlObject);
235 }
236 // applicable only to objects
237 return {};
238 }
239 if (item.domItem == item.domItem.component().field(Fields::objects).index(0)) {
240 // not applicable for a root object
241 return {};
242 }
243 if (item.domItem.canonicalPath().last() == Path::fromField(Fields::value)) {
244 // not supported for the binding value, i.e. p: Item{}
245 return {};
246 }
247
248 TextDocumentEdit textDocEdit{ textDocument, wrapInLoaderTextEdits(item) };
249
250 WorkspaceEdit edit;
251 edit.documentChanges = { textDocEdit };
252
253 CodeAction action;
254 action.kind = CodeActionKind::RefactorRewrite;
255 action.title = Tr::tr("Wrap Component in Loader").toUtf8();
256 action.edit = edit;
257 return { action };
258}
259
260static CodeActions refactorings(const OptionalVersionedTextDocumentIdentifier &textDocument,
261 const QQmlLSUtils::ItemLocation &item)
262{
263 CodeActions codeActions;
264 codeActions.append(wrapComponentInLoader(textDocument, item));
265 return codeActions;
266}
267
268QQmlCodeActionSupport::QQmlCodeActionSupport(QmlLsp::QQmlCodeModelManager *model) : BaseT(model) { }
269
270void QQmlCodeActionSupport::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
271{
272 caps.codeActionProvider = true;
273}
274
275void QQmlCodeActionSupport::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
276{
277 protocol->registerCodeActionRequestHandler(getRequestHandler());
278}
279
280void QQmlCodeActionSupport::process(QQmlCodeActionSupport::RequestPointerArgument request)
281{
282 CodeActions codeActions;
283 codeActions.append(quickfixes(request->m_parameters.context.diagnostics));
284
285 const auto &maybeDoc = tryOpenDocument(request->m_parameters.textDocument.uri);
286 if (!maybeDoc.has_value()) {
287 qCWarning(lsCodeActionSupport) << maybeDoc.error().message;
288 return request->m_response.sendResponse(codeActions);
289 }
290
291 const auto &itemsFound = tryLocateItems(maybeDoc.value(), request->m_parameters.range.start);
292 if (!itemsFound.has_value()) {
293 qCWarning(lsCodeActionSupport) << itemsFound.error().message;
294 return request->m_response.sendResponse(codeActions);
295 }
296
297 codeActions.append(refactorings(
298 { request->m_parameters.textDocument, maybeDoc.value().snapshot.validDocVersion },
299 itemsFound.value().front()));
300
301 request->m_response.sendResponse(codeActions);
302}
303
304QT_END_NAMESPACE
void process(RequestPointerArgument req) override
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override
QQmlCodeActionSupport(QmlLsp::QQmlCodeModelManager *codeModel)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Combined button and popup list for selecting options.
static CodeActions quickfixes(const QList< Diagnostic > &diagnostics)
static TextEdits exposeNestedIds(const QQmlJS::SourceLocation &openingBrace, const QList< std::pair< QString, QQmlJS::SourceLocation > > &nestedIds)
static CodeActions wrapComponentInLoader(const OptionalVersionedTextDocumentIdentifier &textDocument, const QQmlLSUtils::ItemLocation &item)
static TextEdit addLoader(const Position &pos, const QString &loaderId, const QString &componentId)
static TextEdits wrapInLoaderTextEdits(const QQmlLSUtils::ItemLocation &item)
static TextEdits wrapIntoComponent(const Range &itemRange, const QString &componentId)
static CodeActions refactorings(const OptionalVersionedTextDocumentIdentifier &textDocument, const QQmlLSUtils::ItemLocation &item)
static TextEdit todoComment(const Position &pos, const QString &loaderId, const QString &maybeId, const QList< std::pair< QString, QQmlJS::SourceLocation > > &nestedIds)
static std::optional< QQmlLSUtils::ItemLocation > qmlObjectDefinedAt(const QQmlLSUtils::ItemLocation &item)