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 | |
4 | #include "qqmlfindusagessupport_p.h" |
5 | #include "qqmllsutils_p.h" |
6 | #include <QtLanguageServer/private/qlanguageserverspectypes_p.h> |
7 | #include <QtQmlDom/private/qqmldomexternalitems_p.h> |
8 | #include <QtQmlDom/private/qqmldomtop_p.h> |
9 | #include <variant> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | using namespace Qt::StringLiterals; |
14 | |
15 | QQmlFindUsagesSupport::QQmlFindUsagesSupport(QmlLsp::QQmlCodeModel *codeModel) |
16 | : BaseT(codeModel) { } |
17 | |
18 | QString QQmlFindUsagesSupport::name() const |
19 | { |
20 | return u"QmlFindUsagesSupport"_s; |
21 | } |
22 | |
23 | void QQmlFindUsagesSupport::setupCapabilities( |
24 | const QLspSpecification::InitializeParams &, |
25 | QLspSpecification::InitializeResult &serverCapabilities) |
26 | { |
27 | // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now |
28 | // handle the ReferenceOptions later if needed (it adds the possibility to communicate the |
29 | // current progress). |
30 | serverCapabilities.capabilities.referencesProvider = true; |
31 | } |
32 | |
33 | void QQmlFindUsagesSupport::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol) |
34 | { |
35 | protocol->registerReferenceRequestHandler(handler: getRequestHandler()); |
36 | } |
37 | |
38 | void QQmlFindUsagesSupport::process(QQmlFindUsagesSupport::RequestPointerArgument request) |
39 | { |
40 | QList<QLspSpecification::Location> results; |
41 | ResponseScopeGuard guard(results, request->m_response); |
42 | |
43 | auto itemsFound = itemsForRequest(request); |
44 | if (guard.setErrorFrom(itemsFound)) |
45 | return; |
46 | |
47 | QQmlLSUtils::ItemLocation &front = |
48 | std::get<QList<QQmlLSUtils::ItemLocation>>(v&: itemsFound).front(); |
49 | |
50 | auto usages = QQmlLSUtils::findUsagesOf(item: front.domItem); |
51 | |
52 | QQmlJS::Dom::DomItem files = front.domItem.top().field(name: QQmlJS::Dom::Fields::qmlFileWithPath); |
53 | |
54 | // note: ignore usages in filenames here as that is not supported by the protocol. |
55 | for (const auto &usage : usages.usagesInFile()) { |
56 | QLspSpecification::Location location; |
57 | location.uri = QUrl::fromLocalFile(localfile: usage.filename()).toEncoded(); |
58 | location.range = QQmlLSUtils::qmlLocationToLspLocation(qmlLocation: usage); |
59 | |
60 | results.append(t: location); |
61 | } |
62 | } |
63 | QT_END_NAMESPACE |
64 | |
65 |