aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qqmlgototypedefinitionsupport.cpp
blob: 7e2a123af208113eb4ef37f82591b4756b57c8b5 (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
80
81
82
83
84
85
86
87
88
// 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 "qqmlgototypedefinitionsupport_p.h"
#include "qqmllsutils_p.h"
#include <QtLanguageServer/private/qlanguageserverspectypes_p.h>
#include <QtQmlDom/private/qqmldomexternalitems_p.h>
#include <QtQmlDom/private/qqmldomtop_p.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

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

QString QmlGoToTypeDefinitionSupport::name() const
{
    return u"QmlNavigationSupport"_s;
}

void QmlGoToTypeDefinitionSupport::setupCapabilities(
        const QLspSpecification::InitializeParams &,
        QLspSpecification::InitializeResult &serverCapabilities)
{
    // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now
    // handle the TypeDefinitionOptions and TypeDefinitionRegistrationOptions cases later on, if
    // needed (as they just allow more fancy go-to-type-definition action).
    serverCapabilities.capabilities.typeDefinitionProvider = true;
}

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

void QmlGoToTypeDefinitionSupport::process(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();

    QQmlJS::Dom::DomItem base = QQmlLSUtils::findTypeDefinitionOf(front.domItem);
    if (base.domKind() == QQmlJS::Dom::DomKind::Empty) {
        qWarning() << u"Could not obtain the type definition, was the type correctly resolved?"_s
                   << u"\n Obtained type was:\n"_s << base.toString()
                   << u"\nbut selected item was:\n"
                   << front.domItem.toString();
        return;
    }

    if (base.domKind() == QQmlJS::Dom::DomKind::Empty) {
        qDebug() << u"Could not obtain the base from the item"_s;
        return;
    }
    auto locationInfo = QQmlJS::Dom::FileLocations::fileLocationsOf(base);
    if (!locationInfo) {
        qDebug()
                << u"Could not obtain the text location from the base item, was it correctly resolved?\nBase was "_s
                << base.toString();
        return;
    }

    QQmlJS::Dom::DomItem fileOfBase = base.containingFile();
    auto fileOfBasePtr = fileOfBase.ownerAs<QQmlJS::Dom::QmlFile>();
    if (!fileOfBasePtr) {
        qDebug() << u"Could not obtain the file of the base."_s;
        return;
    }

    QLspSpecification::Location l;
    l.uri = QUrl::fromLocalFile(fileOfBasePtr->canonicalFilePath()).toEncoded();

    const QString qmlCode = fileOfBasePtr->code();
    l.range = QQmlLSUtils::qmlLocationToLspLocation(qmlCode, locationInfo->fullRegion);

    results.append(l);
}

QT_END_NAMESPACE