Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
5#include <qqmlhighlightsupport_p.h>
6#include <qqmldiffer_p.h>
7
9
10using namespace Qt::StringLiterals;
11using namespace QLspSpecification;
12using namespace QQmlJS::Dom;
13using namespace QmlHighlighting;
14
15/*!
16\internal
17Make a list of enum names to register the supported token
18types and modifiers. It is case-sensitive in the protocol
19thus we need to lower the first characters of the enum names.
20*/
21template <typename EnumType>
23{
24 QList<QByteArray> result;
25 QMetaEnum metaEnum = QMetaEnum::fromType<EnumType>();
26 for (auto i = 0; i < metaEnum.keyCount(); ++i) {
27 auto &&enumName = QByteArray(metaEnum.key(i));
28 enumName.front() = std::tolower(enumName.front());
29 result.emplace_back(std::move(enumName));
30 }
31
32 return result;
33}
34
36{
37 return enumToByteArray<QLspSpecification::SemanticTokenModifiers>();
38}
39
41{
42 return enumToByteArray<SemanticTokenProtocolTypes>();
43}
44
45static QList<unsigned> generateHighlights(QmlLsp::RegisteredSemanticTokens &cached,
46 const QmlLsp::OpenDocument &doc,
47 const std::optional<HighlightsRange> &range,
48 HighlightingMode mode)
49{
50 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
51 const auto fileObject = file.ownerAs<QmlFile>();
52 QmlHighlighting::Utils::updateResultID(cached.resultId);
53 if (!fileObject || !(fileObject && fileObject->isValid())) {
54 if (const auto lastValidItem = doc.snapshot.validDoc.ownerAs<QmlFile>()) {
55 const auto shiftedHighlights = QmlHighlighting::Utils::shiftHighlights(
56 cached.highlights, lastValidItem->code(), doc.textDocument->toPlainText());
57 return QmlHighlighting::Utils::encodeSemanticTokens(shiftedHighlights, mode);
58 } else {
59 // TODO: Implement regexp based fallback highlighting
60 return {};
61 }
62 } else {
63 HighlightsContainer highlights = QmlHighlighting::Utils::visitTokens(file, range);
64 if (highlights.isEmpty())
65 return {};
66 // Record the highlights for future diffs, only record full highlights
67 if (!range.has_value() )
68 cached.highlights = highlights;
69
70 return QmlHighlighting::Utils::encodeSemanticTokens(highlights, mode);
71 }
72}
73
74/*!
75\internal
76A wrapper class that handles the semantic tokens request for a whole file as described in
77https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_fullRequest
78Sends a QLspSpecification::SemanticTokens data as response that is generated for the entire file.
79*/
84
86 QQmlBaseModule<SemanticTokensRequest>::RequestPointerArgument request)
87{
88 if (!request) {
89 qCWarning(semanticTokens) << "No semantic token request is available!";
90 return;
91 }
92
93 Responses::SemanticTokensResultType result;
94 ResponseScopeGuard guard(result, request->m_response);
95 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
96 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
97 auto &cached = m_codeModelManager->registeredTokens(uri);
98 auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
99
100 if (encoded.isEmpty()) {
101 return;
102 } else {
103 result = SemanticTokens{cached.resultId, std::move(encoded)};
104 }
105}
106
107void SemanticTokenFullHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
108{
109 protocol->registerSemanticTokensRequestHandler(getRequestHandler());
110}
111
112/*!
113\internal
114A wrapper class that handles the semantic tokens delta request for a file
115https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_deltaRequest
116Sends either SemanticTokens or SemanticTokensDelta data as response.
117This is generally requested when the text document is edited after receiving full highlighting data.
118*/
119SemanticTokenDeltaHandler::SemanticTokenDeltaHandler(QmlLsp::QQmlCodeModelManager *codeModelManager)
121{
122}
123
125 QQmlBaseModule<SemanticTokensDeltaRequest>::RequestPointerArgument request)
126{
127 if (!request) {
128 qCWarning(semanticTokens) << "No semantic token request is available!";
129 return;
130 }
131
132 Responses::SemanticTokensDeltaResultType result;
133 ResponseScopeGuard guard(result, request->m_response);
134 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
135 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
136 auto &cached = m_codeModelManager->registeredTokens(uri);
137 if (cached.resultId != request->m_parameters.previousResultId) {
138 // The client is out of sync, send full tokens
139 cached.resultId = request->m_parameters.previousResultId;
140 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
141 result = QLspSpecification::SemanticTokens{ cached.resultId, encoded };
142 } else {
143 const auto cachedHighlights = QmlHighlighting::Utils::encodeSemanticTokens(cached.highlights);
144 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
145 result = QLspSpecification::SemanticTokensDelta{
146 cached.resultId,
147 QmlHighlighting::Utils::computeDiff(cachedHighlights, encoded)
148 };
149 }
150}
151
152void SemanticTokenDeltaHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
153{
154 protocol->registerSemanticTokensDeltaRequestHandler(getRequestHandler());
155}
156
157/*!
158\internal
159A wrapper class that handles the semantic tokens range request for a file
160https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_rangeRequest
161Sends a QLspSpecification::SemanticTokens data as response that is generated for a range of file.
162*/
163SemanticTokenRangeHandler::SemanticTokenRangeHandler(QmlLsp::QQmlCodeModelManager *codeModelManager)
165{
166}
167
169 QQmlBaseModule<SemanticTokensRangeRequest>::RequestPointerArgument request)
170{
171 if (!request) {
172 qCWarning(semanticTokens) << "No semantic token request is available!";
173 return;
174 }
175
176 Responses::SemanticTokensRangeResultType result;
177 ResponseScopeGuard guard(result, request->m_response);
178 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
179 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
180 const QString code = doc.textDocument->toPlainText();
181 const auto range = request->m_parameters.range;
182 int startOffset =
183 int(QQmlLSUtils::textOffsetFrom(code, range.start.line, range.end.character));
184 int endOffset = int(QQmlLSUtils::textOffsetFrom(code, range.end.line, range.end.character));
185 auto &cached = m_codeModelManager->registeredTokens(uri);
186 auto encodedTokens = generateHighlights(
187 cached,
188 doc,
189 QmlHighlighting::HighlightsRange{ startOffset, endOffset },
190 m_mode);
191 if (encodedTokens.isEmpty()) {
192 } else {
193 result = SemanticTokens{ cached.resultId, std::move(encodedTokens) };
194 }
195}
196
197void SemanticTokenRangeHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
198{
199 protocol->registerSemanticTokensRangeRequestHandler(getRequestHandler());
200}
201
202QQmlHighlightSupport::QQmlHighlightSupport(QmlLsp::QQmlCodeModelManager *codeModelManager)
204{
205}
206
207void QQmlHighlightSupport::registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol)
208{
209 m_full.registerHandlers(server, protocol);
210 m_delta.registerHandlers(server, protocol);
211 m_range.registerHandlers(server, protocol);
212
213 QObject::connect(server, &QLanguageServer::clientInitialized, this,
214 &QQmlHighlightSupport::clientInitialized, Qt::SingleShotConnection);
215}
216
217void QQmlHighlightSupport::clientInitialized(QLanguageServer *server)
218{
219 if (auto clientInitOptions = server->clientInfo().initializationOptions) {
220 if ((*clientInitOptions)[u"qtCreatorHighlighting"_s].toBool(false)) {
221 const auto mode = HighlightingMode::QtCHighlighting;
222 m_delta.setHighlightingMode(mode);
223 m_full.setHighlightingMode(mode);
224 m_range.setHighlightingMode(mode);
225 }
226 }
227}
228
229void QQmlHighlightSupport::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
230{
231 QLspSpecification::SemanticTokensOptions options;
232 options.range = true;
233 options.full = QJsonObject({ { u"delta"_s, true } });
234
235 options.legend.tokenTypes = extendedTokenTypesList();
236 options.legend.tokenModifiers = defaultTokenModifiersList();
237 caps.semanticTokensProvider = options;
238}
239
240QT_END_NAMESPACE
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override
QQmlHighlightSupport(QmlLsp::QQmlCodeModelManager *codeModel)
void process(QQmlBaseModule< SemanticTokensDeltaRequest >::RequestPointerArgument req) override
SemanticTokenDeltaHandler(QmlLsp::QQmlCodeModelManager *codeModel)
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
void process(QQmlBaseModule< SemanticTokensRequest >::RequestPointerArgument req) override
SemanticTokenFullHandler(QmlLsp::QQmlCodeModelManager *codeModel)
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
SemanticTokenRangeHandler(QmlLsp::QQmlCodeModelManager *codeModel)
void process(QQmlBaseModule< SemanticTokensRangeRequest >::RequestPointerArgument req) override
void registerHandlers(QLanguageServer *, QLanguageServerProtocol *) override
Combined button and popup list for selecting options.
QList< QByteArray > extendedTokenTypesList()
QList< QByteArray > defaultTokenModifiersList()
static QList< QByteArray > enumToByteArray()
static QList< unsigned > generateHighlights(QmlLsp::RegisteredSemanticTokens &cached, const QmlLsp::OpenDocument &doc, const std::optional< HighlightsRange > &range, HighlightingMode mode)