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 "qqmlgotodefinitionsupport_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
10QT_BEGIN_NAMESPACE
11
12using namespace Qt::StringLiterals;
13
14QmlGoToDefinitionSupport::QmlGoToDefinitionSupport(QmlLsp::QQmlCodeModel *codeModel)
15 : BaseT(codeModel)
16{
17}
18
19QString QmlGoToDefinitionSupport::name() const
20{
21 return u"QmlDefinitionSupport"_s;
22}
23
24void QmlGoToDefinitionSupport::setupCapabilities(
25 const QLspSpecification::InitializeParams &,
26 QLspSpecification::InitializeResult &serverCapabilities)
27{
28 // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now
29 // handle the TypeDefinitionOptions and TypeDefinitionRegistrationOptions cases later on, if
30 // needed (as they just allow more fancy go-to-type-definition action).
31 serverCapabilities.capabilities.definitionProvider = true;
32}
33
34void QmlGoToDefinitionSupport::registerHandlers(QLanguageServer *,
35 QLanguageServerProtocol *protocol)
36{
37 protocol->registerDefinitionRequestHandler(handler: getRequestHandler());
38}
39
40void QmlGoToDefinitionSupport::process(RequestPointerArgument request)
41{
42 QList<QLspSpecification::Location> results;
43 ResponseScopeGuard guard(results, request->m_response);
44
45 auto itemsFound = itemsForRequest(request);
46
47 if (guard.setErrorFrom(itemsFound))
48 return;
49
50 auto &front = std::get<QList<QQmlLSUtils::ItemLocation>>(v&: itemsFound).front();
51
52 auto location = QQmlLSUtils::findDefinitionOf(item: front.domItem);
53 if (!location)
54 return;
55
56 QLspSpecification::Location l;
57 l.uri = QUrl::fromLocalFile(localfile: location->filename()).toEncoded();
58 l.range = QQmlLSUtils::qmlLocationToLspLocation(qmlLocation: *location);
59
60 results.append(t: l);
61}
62QT_END_NAMESPACE
63

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtdeclarative/src/qmlls/qqmlgotodefinitionsupport.cpp