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
qqmlfindusagessupport.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#include <variant>
10
12
13using namespace Qt::StringLiterals;
14
17
19{
20 return u"QmlFindUsagesSupport"_s;
21}
22
24 const QLspSpecification::InitializeParams &,
25 QLspSpecification::InitializeResult &serverCapabilities)
26{
27 // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now
28 // handle the ReferenceOptions later if needed (it adds the possibility to communicate the
29 // current progress).
30 serverCapabilities.capabilities.referencesProvider = true;
31}
32
33void QQmlFindUsagesSupport::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
34{
35 protocol->registerReferenceRequestHandler(getRequestHandler());
36}
37
39{
40 QList<QLspSpecification::Location> results;
41 ResponseScopeGuard guard(results, request->m_response);
42
43 auto itemsFound = itemsForRequest(request);
44 if (guard.setErrorFrom(itemsFound))
45 return;
46
47 QQmlLSUtilsItemLocation &front = std::get<QList<QQmlLSUtilsItemLocation>>(itemsFound).front();
48
49 auto usages = QQmlLSUtils::findUsagesOf(front.domItem);
50
51 QQmlJS::Dom::DomItem files = front.domItem.top().field(QQmlJS::Dom::Fields::qmlFileWithPath);
52
53 QHash<QString, QString> codeCache;
54
55 for (const auto &usage : usages) {
56 QLspSpecification::Location location;
57 location.uri = QUrl::fromLocalFile(usage.filename).toEncoded();
58
59 auto cacheEntry = codeCache.find(usage.filename);
60 if (cacheEntry == codeCache.end()) {
61 auto file = files.key(usage.filename)
62 .field(QQmlJS::Dom::Fields::currentItem)
63 .ownerAs<QQmlJS::Dom::QmlFile>();
64 if (!file) {
65 qDebug() << "File" << usage.filename << "not found in DOM! Available files are"
66 << files.keys();
67 continue;
68 }
69 cacheEntry = codeCache.insert(usage.filename, file->code());
70 }
71
72 location.range =
73 QQmlLSUtils::qmlLocationToLspLocation(cacheEntry.value(), usage.sourceLocation);
74
76 }
77}
79
Implements a server for the language server protocol.
void append(parameter_type t)
Definition qlist.h:458
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
QQmlFindUsagesSupport(QmlLsp::QQmlCodeModel *codeModel)
void process(RequestPointerArgument request) override
QString name() const override
DomItem field(QStringView name) const
DomItem top() const
A QmlFile, when loaded in a DomEnvironment that has the DomCreationOption::WithSemanticAnalysis,...
static QList< QQmlLSUtilsLocation > findUsagesOf(const DomItem &item)
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:129
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
Combined button and popup list for selecting options.
#define qDebug
[1]
Definition qlogging.h:164
GLint location
GLsizeiptr const void GLenum usage
Definition qopenglext.h:543
QFile file
[0]
QStringList files
[8]
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)