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));
46 const QmlLsp::OpenDocument &doc,
47 const std::optional<HighlightsRange> &range,
48 HighlightingMode mode)
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);
63 HighlightsContainer highlights = QmlHighlighting::Utils::visitTokens(file, range);
64 if (highlights.isEmpty())
67 if (!range.has_value() )
68 cached.highlights = highlights;
70 return QmlHighlighting::Utils::encodeSemanticTokens(highlights, mode);
86 QQmlBaseModule<SemanticTokensRequest>::RequestPointerArgument request)
89 qCWarning(semanticTokens) <<
"No semantic token request is available!";
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);
100 if (encoded.isEmpty()) {
104 result = SemanticTokens{cached.resultId, std::move(encoded)};
126 QQmlBaseModule<SemanticTokensDeltaRequest>::RequestPointerArgument request)
129 qCWarning(semanticTokens) <<
"No semantic token request is available!";
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) {
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 };
144 const auto cachedHighlights = QmlHighlighting::Utils::encodeSemanticTokens(cached.highlights);
145 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
146 result = QLspSpecification::SemanticTokensDelta{
148 QmlHighlighting::Utils::computeDiff(cachedHighlights, encoded)
170 QQmlBaseModule<SemanticTokensRangeRequest>::RequestPointerArgument request)
173 qCWarning(semanticTokens) <<
"No semantic token request is available!";
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;
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(
190 QmlHighlighting::HighlightsRange{ startOffset, endOffset },
192 if (encodedTokens.isEmpty()) {
195 result = SemanticTokens{ cached.resultId, std::move(encodedTokens) };
216 m_full.registerHandlers(server, protocol);
217 m_delta.registerHandlers(server, protocol);
218 m_range.registerHandlers(server, protocol);
222 const QLspSpecification::InitializeParams &clientCapabilities,
223 QLspSpecification::InitializeResult &serverCapabilities)
225 QLspSpecification::SemanticTokensOptions options;
226 options.range =
true;
227 options.full = QJsonObject({ { u"delta"_s,
true } });
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);
237 options.legend.tokenTypes = extendedTokenTypesList();
238 options.legend.tokenModifiers = defaultTokenModifiersList();
239 serverCapabilities.capabilities.semanticTokensProvider = options;