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
qqmlrenamesymbolsupport.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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
7#include <utility>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt::StringLiterals;
12QQmlRenameSymbolSupport::QQmlRenameSymbolSupport(QmlLsp::QQmlCodeModelManager *model) : BaseT(model)
13{
14}
15
16void QQmlRenameSymbolSupport::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
17{
18 // use a bool for now. Alternatively, if the client supports "prepareSupport", one could
19 // use a RenameOptions here. See following page for more information about prepareSupport:
20 // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_prepareRename
21 caps.renameProvider = true;
22}
23
24void QQmlRenameSymbolSupport::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
25{
26 protocol->registerRenameRequestHandler(getRequestHandler());
27}
28
29void QQmlRenameSymbolSupport::process(QQmlRenameSymbolSupport::RequestPointerArgument request)
30{
31 QLspSpecification::WorkspaceEdit result;
32 ResponseScopeGuard guard(result, request->m_response);
33
34 auto itemsFound = itemsForRequest(request);
35 if (guard.setErrorFrom(itemsFound))
36 return;
37
39 std::get<QList<QQmlLSUtils::ItemLocation>>(itemsFound).front();
40
41 const QString newName = QString::fromUtf8(request->m_parameters.newName);
42 auto expressionType =
43 QQmlLSUtils::resolveExpressionType(front.domItem, QQmlLSUtils::ResolveOwnerType);
44
45 if (!expressionType) {
46 guard.setError(QQmlLSUtils::ErrorMessage{ 0, u"Cannot rename the requested object"_s });
47 return;
48 }
49
50 if (guard.setErrorFrom(QQmlLSUtils::checkNameForRename(front.domItem, newName, expressionType)))
51 return;
52
53 auto &editsByFileForResult = result.documentChanges.emplace();
54
55 // The QLspSpecification::WorkspaceEdit requires the changes to be grouped by files, so
56 // collect them into editsByFileUris.
57 QMap<QUrl, QList<QLspSpecification::TextEdit>> editsByFileUris;
58
59 const auto renames = QQmlLSUtils::renameUsagesOf(front.domItem, newName, expressionType);
60 for (const auto &rename : renames.renameInFile()) {
61 QLspSpecification::TextEdit edit;
62
63 const QUrl uri = QUrl::fromLocalFile(rename.location.filename());
64 edit.range = QQmlLSUtils::qmlLocationToLspLocation(rename.location);
65 edit.newText = rename.replacement.toUtf8();
66
67 editsByFileUris[uri].append(edit);
68 }
69
70 for (auto it = editsByFileUris.keyValueBegin(); it != editsByFileUris.keyValueEnd(); ++it) {
71 QLspSpecification::TextDocumentEdit editsForCurrentFile;
72 editsForCurrentFile.textDocument.uri = it->first.toEncoded();
73
74 // TODO: do we need to take care of the optional versioning in
75 // editsForCurrentFile.textDocument.version? see
76 // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#optionalVersionedTextDocumentIdentifier
77 // for more details
78
79 for (const auto &x : std::as_const(it->second)) {
80 editsForCurrentFile.edits.append(x);
81 }
82 editsByFileForResult.append(editsForCurrentFile);
83 }
84
85 // if files need to be renamed, then do it after the text edits
86 for (const auto &rename : renames.renameInFilename()) {
87 QLspSpecification::RenameFile currentRenameFile;
88 currentRenameFile.kind = "rename";
89 currentRenameFile.oldUri = QUrl::fromLocalFile(rename.oldFilename).toEncoded();
90 currentRenameFile.newUri = QUrl::fromLocalFile(rename.newFilename).toEncoded();
91 editsByFileForResult.append(currentRenameFile);
92 }
93}
94
95QT_END_NAMESPACE
void process(RequestPointerArgument request) override
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override