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
qqmlhighlightsupport.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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
6
8
9using namespace Qt::StringLiterals;
10using namespace QLspSpecification;
11using namespace QQmlJS::Dom;
12
19template <typename EnumType>
20static QList<QByteArray> enumToByteArray()
21{
22 QList<QByteArray> result;
23 QMetaEnum metaEnum = QMetaEnum::fromType<EnumType>();
24 for (auto i = 0; i < metaEnum.keyCount(); ++i) {
25 auto &&enumName = QByteArray(metaEnum.key(i));
26 enumName.front() = std::tolower(enumName.front());
27 result.emplace_back(std::move(enumName));
28 }
29
30 return result;
31}
32
33static QList<QByteArray> tokenTypesList()
34{
35 return enumToByteArray<SemanticTokenTypes>();
36}
37
38static QList<QByteArray> tokenModifiersList()
39{
40 return enumToByteArray<SemanticTokenModifiers>();
41}
42
53
56{
57 if (!request) {
58 qCWarning(semanticTokens) << "No semantic token request is available!";
59 return;
60 }
61
62 Responses::SemanticTokensResultType result;
63 ResponseScopeGuard guard(result, request->m_response);
64 const auto doc = m_codeModel->openDocumentByUrl(
65 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
66 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
67 Highlights highlights;
68 auto &&encoded = highlights.collectTokens(file, std::nullopt);
69 auto &registeredTokens = m_codeModel->registeredTokens();
70 if (!encoded.isEmpty()) {
71 HighlightingUtils::updateResultID(registeredTokens.resultId);
72 result = SemanticTokens{ registeredTokens.resultId, encoded };
73 registeredTokens.lastTokens = std::move(encoded);
74 } else {
75 result = nullptr;
76 }
77}
78
79void SemanticTokenFullHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
80{
81 protocol->registerSemanticTokensRequestHandler(getRequestHandler());
82}
83
95
98{
99 if (!request) {
100 qCWarning(semanticTokens) << "No semantic token request is available!";
101 return;
102 }
103
104 Responses::SemanticTokensDeltaResultType result;
105 ResponseScopeGuard guard(result, request->m_response);
106 const auto doc = m_codeModel->openDocumentByUrl(
107 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
108 DomItem file = doc.snapshot.validDoc.fileObject(GoTo::MostLikely);
109 Highlights highlights;
110 auto newEncoded = highlights.collectTokens(file, std::nullopt);
111 auto &registeredTokens = m_codeModel->registeredTokens();
112 const auto lastResultId = registeredTokens.resultId;
113 HighlightingUtils::updateResultID(registeredTokens.resultId);
114
115 // Return full token list if result ids not align
116 // otherwise compute the delta.
117 if (lastResultId == request->m_parameters.previousResultId) {
118 result = QLspSpecification::SemanticTokensDelta {
119 registeredTokens.resultId,
120 HighlightingUtils::computeDiff(registeredTokens.lastTokens, newEncoded)
121 };
122 } else if (!newEncoded.isEmpty()){
123 result = QLspSpecification::SemanticTokens{ registeredTokens.resultId, newEncoded };
124 } else {
125 result = nullptr;
126 }
127 registeredTokens.lastTokens = std::move(newEncoded);
128}
129
130void SemanticTokenDeltaHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
131{
132 protocol->registerSemanticTokensDeltaRequestHandler(getRequestHandler());
133}
134
145
148{
149 if (!request) {
150 qCWarning(semanticTokens) << "No semantic token request is available!";
151 return;
152 }
153
154 Responses::SemanticTokensRangeResultType result;
155 ResponseScopeGuard guard(result, request->m_response);
156 const auto doc = m_codeModel->openDocumentByUrl(
157 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
158 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
159 const auto qmlFile = file.as<QmlFile>();
160 if (!qmlFile)
161 return;
162 const QString &code = qmlFile->code();
163 const auto range = request->m_parameters.range;
164 int startOffset = int(QQmlLSUtils::textOffsetFrom(code, range.start.line, range.end.character));
165 int endOffset = int(QQmlLSUtils::textOffsetFrom(code, range.end.line, range.end.character));
166 Highlights highlights;
167 auto &&encoded = highlights.collectTokens(file, HighlightsRange{startOffset, endOffset});
168 auto &registeredTokens = m_codeModel->registeredTokens();
169 if (!encoded.isEmpty()) {
170 HighlightingUtils::updateResultID(registeredTokens.resultId);
171 result = SemanticTokens{ registeredTokens.resultId, std::move(encoded) };
172 } else {
173 result = nullptr;
174 }
175}
176
177void SemanticTokenRangeHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
178{
179 protocol->registerSemanticTokensRangeRequestHandler(getRequestHandler());
180}
181
183 : m_full(codeModel), m_delta(codeModel), m_range(codeModel)
184{
185}
186
188{
189 return "QQmlHighlightSupport"_L1;
190}
191
192void QQmlHighlightSupport::registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol)
193{
194 m_full.registerHandlers(server, protocol);
195 m_delta.registerHandlers(server, protocol);
196 m_range.registerHandlers(server, protocol);
197}
198
200 const QLspSpecification::InitializeParams &,
201 QLspSpecification::InitializeResult &serverCapabilities)
202{
203 QLspSpecification::SemanticTokensOptions options;
204 options.range = true;
205 options.full = QJsonObject({ { u"delta"_s, true } });
206 options.legend.tokenTypes = tokenTypesList();
207 options.legend.tokenModifiers = tokenModifiersList();
208
209 serverCapabilities.capabilities.semanticTokensProvider = options;
210}
211
QList< int > collectTokens(const QQmlJS::Dom::DomItem &item, const std::optional< HighlightsRange > &range)
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
Implements a server for the language server protocol.
\inmodule QtCore
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
QQmlHighlightSupport(QmlLsp::QQmlCodeModel *codeModel)
QString name() const override
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
A QmlFile, when loaded in a DomEnvironment that has the DomCreationOption::WithSemanticAnalysis,...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
iterator end()
Returns an \l{STL-style iterators}{STL-style iterator} pointing just after the last character in the ...
Definition qstring.h:1357
OpenDocument openDocumentByUrl(const QByteArray &url)
RegisteredSemanticTokens & registeredTokens()
void process(QQmlBaseModule< SemanticTokensDeltaRequest >::RequestPointerArgument req) override
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
SemanticTokenDeltaHandler(QmlLsp::QQmlCodeModel *codeModel)
void process(QQmlBaseModule< SemanticTokensRequest >::RequestPointerArgument req) override
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
SemanticTokenFullHandler(QmlLsp::QQmlCodeModel *codeModel)
SemanticTokenRangeHandler(QmlLsp::QQmlCodeModel *codeModel)
void process(QQmlBaseModule< SemanticTokensRangeRequest >::RequestPointerArgument req) override
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
qsizetype textOffsetFrom(const QString &text, int row, int column)
Convert a text position from (line, column) into an offset.
QByteArray lspUriToQmlUrl(const QByteArray &uri)
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qCWarning(category,...)
GLsizei range
GLuint64EXT * result
[6]
static QList< QByteArray > tokenModifiersList()
static QList< QByteArray > tokenTypesList()
static QList< QByteArray > enumToByteArray()
QFile file
[0]
QNetworkRequest request(url)
static void updateResultID(QByteArray &resultID)
static QList< QLspSpecification::SemanticTokensEdit > computeDiff(const QList< int > &, const QList< int > &)
RequestPointer && RequestPointerArgument
This class sends a result or an error when going out of scope.