Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmlgototypedefinitionsupport.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
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
11
12using namespace Qt::StringLiterals;
13
15 : BaseT(codeModel)
16{
17}
18
20{
21 return u"QmlNavigationSupport"_s;
22}
23
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.typeDefinitionProvider = true;
32}
33
35 QLanguageServerProtocol *protocol)
36{
37 protocol->registerTypeDefinitionRequestHandler(getRequestHandler());
38}
39
41{
43 QScopeGuard onExit([&results, &request]() { request->m_response.sendResponse(results); });
44
45 auto itemsFound = itemsForRequest(request);
46 if (!itemsFound) {
47 return;
48 }
49
50 QQmlJS::Dom::DomItem base = QQmlLSUtils::findTypeDefinitionOf(itemsFound->first().domItem);
51 if (base.domKind() == QQmlJS::Dom::DomKind::Empty) {
52 qWarning() << u"Could not obtain the type definition, was the type correctly resolved?"_s
53 << u"\n Obtained type was:\n"_s << base.toString()
54 << u"\nbut selected item was:\n"
55 << itemsFound->first().domItem.toString();
56 return;
57 }
58
59 if (base.domKind() == QQmlJS::Dom::DomKind::Empty) {
60 qWarning() << u"Could not obtain the base from the item"_s;
61 return;
62 }
64 if (!locationInfo) {
65 qWarning()
66 << u"Could not obtain the text location from the base item, was it correctly resolved?\nBase was "_s
67 << base.toString();
68 return;
69 }
70
71 QQmlJS::Dom::DomItem fileOfBase = base.containingFile();
72 auto fileOfBasePtr = fileOfBase.ownerAs<QQmlJS::Dom::QmlFile>();
73 if (!fileOfBasePtr) {
74 qWarning() << u"Could not obtain the file of the base."_s;
75 return;
76 }
77
78 QLspSpecification::Location l;
79 l.uri = QUrl::fromLocalFile(fileOfBasePtr->canonicalFilePath()).toEncoded();
80
81 const QString qmlCode = fileOfBasePtr->code();
82 l.range = QQmlLSUtils::qmlLocationToLspLocation(qmlCode, locationInfo->fullRegion);
83
84 results.append(l);
85}
86
Implements a server for the language server protocol.
Definition qlist.h:74
void append(parameter_type t)
Definition qlist.h:441
std::shared_ptr< T > ownerAs()
static const FileLocations * fileLocationsOf(DomItem &)
static QQmlJS::Dom::DomItem findTypeDefinitionOf(QQmlJS::Dom::DomItem item)
Extracts the QML object type of an \l DomItem.
static QLspSpecification::Range qmlLocationToLspLocation(const QString &code, QQmlJS::SourceLocation qmlLocation)
Converts a QQmlJS::SourceLocation to a LSP Range.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString first(qsizetype n) const
Definition qstring.h:337
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3354
QByteArray toEncoded(FormattingOptions options=FullyEncoded) const
Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returne...
Definition qurl.cpp:2964
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
void process(RequestPointerArgument request) override
QmlGoToTypeDefinitionSupport(QmlLsp::QQmlCodeModel *codeModel)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:162
QNetworkRequest request(url)
std::optional< QList< QQmlLSUtilsItemLocation > > itemsForRequest(const RequestPointer &request)