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<int> 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 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
99
100 if (encoded.isEmpty()) {
101 result = nullptr;
102 return;
103 } else {
104 result = SemanticTokens{cached.resultId, std::move(encoded)};
105 }
106}
107
108void SemanticTokenFullHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
109{
110 protocol->registerSemanticTokensRequestHandler(getRequestHandler());
111}
112
113/*!
114\internal
115A wrapper class that handles the semantic tokens delta request for a file
116https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_deltaRequest
117Sends either SemanticTokens or SemanticTokensDelta data as response.
118This is generally requested when the text document is edited after receiving full highlighting data.
119*/
120SemanticTokenDeltaHandler::SemanticTokenDeltaHandler(QmlLsp::QQmlCodeModelManager *codeModelManager)
122{
123}
124
126 QQmlBaseModule<SemanticTokensDeltaRequest>::RequestPointerArgument request)
127{
128 if (!request) {
129 qCWarning(semanticTokens) << "No semantic token request is available!";
130 return;
131 }
132
133 Responses::SemanticTokensDeltaResultType result;
134 ResponseScopeGuard guard(result, request->m_response);
135 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
136 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
137 auto &cached = m_codeModelManager->registeredTokens(uri);
138 if (cached.resultId != request->m_parameters.previousResultId) {
139 // The client is out of sync, send full tokens
140 cached.resultId = request->m_parameters.previousResultId;
141 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
142 result = QLspSpecification::SemanticTokens{ cached.resultId, encoded };
143 } else {
144 const auto cachedHighlights = QmlHighlighting::Utils::encodeSemanticTokens(cached.highlights);
145 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
146 result = QLspSpecification::SemanticTokensDelta{
147 cached.resultId,
148 QmlHighlighting::Utils::computeDiff(cachedHighlights, encoded)
149 };
150 }
151}
152
153void SemanticTokenDeltaHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
154{
155 protocol->registerSemanticTokensDeltaRequestHandler(getRequestHandler());
156}
157
158/*!
159\internal
160A wrapper class that handles the semantic tokens range request for a file
161https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#semanticTokens_rangeRequest
162Sends a QLspSpecification::SemanticTokens data as response that is generated for a range of file.
163*/
164SemanticTokenRangeHandler::SemanticTokenRangeHandler(QmlLsp::QQmlCodeModelManager *codeModelManager)
166{
167}
168
170 QQmlBaseModule<SemanticTokensRangeRequest>::RequestPointerArgument request)
171{
172 if (!request) {
173 qCWarning(semanticTokens) << "No semantic token request is available!";
174 return;
175 }
176
177 Responses::SemanticTokensRangeResultType result;
178 ResponseScopeGuard guard(result, request->m_response);
179 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
180 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
181 const QString code = doc.textDocument->toPlainText();
182 const auto range = request->m_parameters.range;
183 int startOffset =
184 int(QQmlLSUtils::textOffsetFrom(code, range.start.line, range.end.character));
185 int endOffset = int(QQmlLSUtils::textOffsetFrom(code, range.end.line, range.end.character));
186 auto &cached = m_codeModelManager->registeredTokens(uri);
187 const auto encodedTokens = generateHighlights(
188 cached,
189 doc,
190 QmlHighlighting::HighlightsRange{ startOffset, endOffset },
191 m_mode);
192 if (encodedTokens.isEmpty()) {
193 result = nullptr;
194 } else {
195 result = SemanticTokens{ cached.resultId, std::move(encodedTokens) };
196 }
197}
198
199void SemanticTokenRangeHandler::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
200{
201 protocol->registerSemanticTokensRangeRequestHandler(getRequestHandler());
202}
203
204QQmlHighlightSupport::QQmlHighlightSupport(QmlLsp::QQmlCodeModelManager *codeModelManager)
206{
207}
208
210{
211 return "QQmlHighlightSupport"_L1;
212}
213
214void QQmlHighlightSupport::registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol)
215{
216 m_full.registerHandlers(server, protocol);
217 m_delta.registerHandlers(server, protocol);
218 m_range.registerHandlers(server, protocol);
219}
220
222 const QLspSpecification::InitializeParams &clientCapabilities,
223 QLspSpecification::InitializeResult &serverCapabilities)
224{
225 QLspSpecification::SemanticTokensOptions options;
226 options.range = true;
227 options.full = QJsonObject({ { u"delta"_s, true } });
228
229 if (auto clientInitOptions = clientCapabilities.initializationOptions) {
230 if ((*clientInitOptions)[u"qtCreatorHighlighting"_s].toBool(false)) {
231 const auto mode = HighlightingMode::QtCHighlighting;
232 m_delta.setHighlightingMode(mode);
233 m_full.setHighlightingMode(mode);
234 m_range.setHighlightingMode(mode);
235 }
236 }
237 options.legend.tokenTypes = extendedTokenTypesList();
238 options.legend.tokenModifiers = defaultTokenModifiersList();
239 serverCapabilities.capabilities.semanticTokensProvider = options;
240}
241
242QT_END_NAMESPACE
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
QString name() const override
QQmlHighlightSupport(QmlLsp::QQmlCodeModelManager *codeModel)
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
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.
static QList< int > generateHighlights(QmlLsp::RegisteredSemanticTokens &cached, const QmlLsp::OpenDocument &doc, const std::optional< HighlightsRange > &range, HighlightingMode mode)
QList< QByteArray > extendedTokenTypesList()
QList< QByteArray > defaultTokenModifiersList()
static QList< QByteArray > enumToByteArray()