aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qqmlfindusagessupport.cpp
blob: 8a3dca7e3a0bba5f78be00a46ee8aa8a85f7743e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmlfindusagessupport_p.h"
#include "qqmllsutils_p.h"
#include <QtLanguageServer/private/qlanguageserverspectypes_p.h>
#include <QtQmlDom/private/qqmldomexternalitems_p.h>
#include <QtQmlDom/private/qqmldomtop_p.h>
#include <variant>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

QQmlFindUsagesSupport::QQmlFindUsagesSupport(QmlLsp::QQmlCodeModel *codeModel)
    : BaseT(codeModel) { }

QString QQmlFindUsagesSupport::name() const
{
    return u"QmlFindUsagesSupport"_s;
}

void QQmlFindUsagesSupport::setupCapabilities(
        const QLspSpecification::InitializeParams &,
        QLspSpecification::InitializeResult &serverCapabilities)
{
    // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now
    // handle the ReferenceOptions later if needed (it adds the possibility to communicate the
    // current progress).
    serverCapabilities.capabilities.referencesProvider = true;
}

void QQmlFindUsagesSupport::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
{
    protocol->registerReferenceRequestHandler(getRequestHandler());
}

void QQmlFindUsagesSupport::process(QQmlFindUsagesSupport::RequestPointerArgument request)
{
    QList<QLspSpecification::Location> results;
    ResponseScopeGuard guard(results, request->m_response);

    auto itemsFound = itemsForRequest(request);
    if (guard.setErrorFrom(itemsFound))
        return;

    QQmlLSUtilsItemLocation &front = std::get<QList<QQmlLSUtilsItemLocation>>(itemsFound).front();

    auto usages = QQmlLSUtils::findUsagesOf(front.domItem);

    QQmlJS::Dom::DomItem files = front.domItem.top().field(QQmlJS::Dom::Fields::qmlFileWithPath);

    QHash<QString, QString> codeCache;

    for (const auto &usage : usages) {
        QLspSpecification::Location location;
        location.uri = QUrl::fromLocalFile(usage.filename).toEncoded();

        auto cacheEntry = codeCache.find(usage.filename);
        if (cacheEntry == codeCache.end()) {
            auto file = files.key(usage.filename)
                                .field(QQmlJS::Dom::Fields::currentItem)
                                .ownerAs<QQmlJS::Dom::QmlFile>();
            if (!file) {
                qDebug() << "File" << usage.filename << "not found in DOM! Available files are"
                         << files.keys();
                continue;
            }
            cacheEntry = codeCache.insert(usage.filename, file->code());
        }

        location.range =
                QQmlLSUtils::qmlLocationToLspLocation(cacheEntry.value(), usage.sourceLocation);

        results.append(location);
    }
}
QT_END_NAMESPACE