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
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
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"QmlDefinitionSupport"_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.definitionProvider = true;
32}
33
35 QLanguageServerProtocol *protocol)
36{
37 protocol->registerDefinitionRequestHandler(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 QQmlLSUtilsItemLocation &front = std::get<QList<QQmlLSUtilsItemLocation>>(itemsFound).front();
51
53 if (!location)
54 return;
55
56 QLspSpecification::Location l;
57 l.uri = QUrl::fromLocalFile(location->filename).toEncoded();
58
60 auto fileOfBasePtr = file.ownerAs<QQmlJS::Dom::QmlFile>();
61 if (!fileOfBasePtr) {
62 qDebug() << "Could not find file" << location->filename << "in the dom!";
63 return;
64 }
65 const QString qmlCode = fileOfBasePtr->code();
66 l.range = QQmlLSUtils::qmlLocationToLspLocation(qmlCode, location->sourceLocation);
67
68 results.append(l);
69}
Implements a server for the language server protocol.
void append(parameter_type t)
Definition qlist.h:458
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 > findDefinitionOf(const DomItem &item)
\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 process(RequestPointerArgument request) override
QmlGoToDefinitionSupport(QmlLsp::QQmlCodeModel *codeModel)
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
Combined button and popup list for selecting options.
#define qDebug
[1]
Definition qlogging.h:164
GLint location
QFile file
[0]
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)