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
qqmlgotodefinitionsupport.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 <QtLanguageServer/private/qlanguageserverspectypes_p.h>
8#include <QtQmlDom/private/qqmldomexternalitems_p.h>
9#include <QtQmlDom/private/qqmldomtop_p.h>
10
12
13using namespace Qt::StringLiterals;
14
15QmlGoToDefinitionSupport::QmlGoToDefinitionSupport(QmlLsp::QQmlCodeModelManager *codeModel)
16 : BaseT(codeModel)
17{
18}
19
21{
22 return u"QmlDefinitionSupport"_s;
23}
24
26 const QLspSpecification::InitializeParams &,
27 QLspSpecification::InitializeResult &serverCapabilities)
28{
29 // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now
30 // handle the TypeDefinitionOptions and TypeDefinitionRegistrationOptions cases later on, if
31 // needed (as they just allow more fancy go-to-type-definition action).
32 serverCapabilities.capabilities.definitionProvider = true;
33}
34
36 QLanguageServerProtocol *protocol)
37{
38 protocol->registerDefinitionRequestHandler(getRequestHandler());
39}
40
41void QmlGoToDefinitionSupport::process(RequestPointerArgument request)
42{
43 QList<QLspSpecification::Location> results;
44 ResponseScopeGuard guard(results, request->m_response);
45
46 auto itemsFound = itemsForRequest(request);
47
48 if (guard.setErrorFrom(itemsFound))
49 return;
50
51 auto &front = std::get<QList<QQmlLSUtils::ItemLocation>>(itemsFound).front();
52
53 const QByteArray shortestRootUrl =
54 m_codeModelManager->shortestRootUrlForFile(request->m_parameters.textDocument.uri);
55
56 const QStringList headerDirectories = shortestRootUrl.isEmpty()
57 ? QStringList{}
58 : QStringList{ QUrl::fromEncoded(shortestRootUrl).toLocalFile() };
59
60 const auto location = QQmlLSUtils::findDefinitionOf(front.domItem, headerDirectories);
61 if (!location)
62 return;
63
64 QLspSpecification::Location l;
65 l.uri = QUrl::fromLocalFile(location->filename()).toEncoded();
66 l.range = QQmlLSUtils::qmlLocationToLspLocation(*location);
67
68 results.append(l);
69}
70QT_END_NAMESPACE
Implements a server for the language server protocol.
void process(RequestPointerArgument request) override
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
This class sends a result or an error when going out of scope.