Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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{
42 QList<QLspSpecification::Location> results;
43 ResponseScopeGuard guard(results, request->m_response);
44
45 auto itemsFound = itemsForRequest(request);
46 if (guard.setErrorFrom(itemsFound))
47 return;
48
49 QQmlLSUtilsItemLocation &front = std::get<QList<QQmlLSUtilsItemLocation>>(itemsFound).front();
50
52
53 if (!base) {
54 qDebug() << u"Could not obtain the base from the item"_s;
55 return;
56 }
57
58 QQmlJS::Dom::DomItem fileOfBase = front.domItem.goToFile(base->filename);
59 auto fileOfBasePtr = fileOfBase.ownerAs<QQmlJS::Dom::QmlFile>();
60 if (!fileOfBasePtr) {
61 qDebug() << u"Could not obtain the file of the base."_s;
62 return;
63 }
64
65 QLspSpecification::Location l;
66 l.uri = QUrl::fromLocalFile(fileOfBasePtr->canonicalFilePath()).toEncoded();
67
68 const QString qmlCode = fileOfBasePtr->code();
69 l.range = QQmlLSUtils::qmlLocationToLspLocation(qmlCode, base->sourceLocation);
70
71 results.append(l);
72}
73
Implements a server for the language server protocol.
void append(parameter_type t)
Definition qlist.h:458
std::shared_ptr< T > ownerAs() const
DomItem goToFile(const QString &filePath) const
A QmlFile, when loaded in a DomEnvironment that has the DomCreationOption::WithSemanticAnalysis,...
static QLspSpecification::Range qmlLocationToLspLocation(const QString &code, QQmlJS::SourceLocation qmlLocation)
Converts a QQmlJS::SourceLocation to a LSP Range.
static std::optional< QQmlLSUtilsLocation > findTypeDefinitionOf(const DomItem &item)
Returns the location of the type definition pointed by object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
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 qDebug
[1]
Definition qlogging.h:164
static const uint base
Definition qurlidna.cpp:20
QNetworkRequest request(url)
std::variant< QList< QQmlLSUtilsItemLocation >, QQmlLSUtilsErrorMessage > itemsForRequest(const RequestPointer &request)
QQmlJS::Dom::DomItem domItem
This class sends a result or an error when going out of scope.
bool setErrorFrom(const std::variant< T... > &variant)