Qt 6.x
The Qt SDK
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
11
12using namespace Qt::StringLiterals;
13
15 : BaseT(codeModel) { }
16
18{
19 return u"QmlFindUsagesSupport"_s;
20}
21
23 const QLspSpecification::InitializeParams &,
24 QLspSpecification::InitializeResult &serverCapabilities)
25{
26 // just assume serverCapabilities.capabilities.typeDefinitionProvider is a bool for now
27 // handle the ReferenceOptions later if needed (it adds the possibility to communicate the
28 // current progress).
29 serverCapabilities.capabilities.referencesProvider = true;
30}
31
32void QQmlFindUsagesSupport::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
33{
34 protocol->registerReferenceRequestHandler(getRequestHandler());
35}
36
38{
40 QScopeGuard onExit([&results, &request]() { request->m_response.sendResponse(results); });
41
42 auto itemsFound = itemsForRequest(request);
43 if (!itemsFound) {
44 return;
45 }
46
47 auto usages = QQmlLSUtils::findUsagesOf(itemsFound->front().domItem);
48
50 itemsFound->front().domItem.top().field(QQmlJS::Dom::Fields::qmlFileWithPath);
51
53
54 for (const auto &usage : usages) {
55 QLspSpecification::Location location;
57
58 auto cacheEntry = codeCache.find(usage.filename);
59 if (cacheEntry == codeCache.end()) {
60 auto file = files.key(usage.filename)
61 .field(QQmlJS::Dom::Fields::currentItem)
62 .ownerAs<QQmlJS::Dom::QmlFile>();
63 if (!file) {
64 qDebug() << "File" << usage.filename << "not found in DOM! Available files are"
65 << files.keys();
66 continue;
67 }
68 cacheEntry = codeCache.insert(usage.filename, file->code());
69 }
70
71 location.range = QQmlLSUtils::qmlLocationToLspLocation(cacheEntry.value(), usage.location);
72
74 }
75}
77
\inmodule QtCore
Definition qhash.h:818
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1258
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
Implements a server for the language server protocol.
Definition qlist.h:74
void append(parameter_type t)
Definition qlist.h:441
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
static QList< QQmlLSUtilsLocation > findUsagesOf(QQmlJS::Dom::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:127
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
Combined button and popup list for selecting options.
#define qDebug
[1]
Definition qlogging.h:160
GLint location
GLsizeiptr const void GLenum usage
Definition qopenglext.h:543
QFile file
[0]
QStringList files
[8]
QNetworkRequest request(url)
std::optional< QList< QQmlLSUtilsItemLocation > > itemsForRequest(const RequestPointer &request)