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 auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
100 if (encoded.isEmpty()) {
103 result = SemanticTokens{cached.resultId, std::move(encoded)};
125 QQmlBaseModule<SemanticTokensDeltaRequest>::RequestPointerArgument request)
128 qCWarning(semanticTokens) <<
"No semantic token request is available!";
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) {
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 };
143 const auto cachedHighlights = QmlHighlighting::Utils::encodeSemanticTokens(cached.highlights);
144 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
145 result = QLspSpecification::SemanticTokensDelta{
147 QmlHighlighting::Utils::computeDiff(cachedHighlights, encoded)
169 QQmlBaseModule<SemanticTokensRangeRequest>::RequestPointerArgument request)
172 qCWarning(semanticTokens) <<
"No semantic token request is available!";
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;
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(
189 QmlHighlighting::HighlightsRange{ startOffset, endOffset },
191 if (encodedTokens.isEmpty()) {
193 result = SemanticTokens{ cached.resultId, std::move(encodedTokens) };
209 m_full.registerHandlers(server, protocol);
210 m_delta.registerHandlers(server, protocol);
211 m_range.registerHandlers(server, protocol);
213 QObject::connect(server, &QLanguageServer::clientInitialized,
this,
214 &QQmlHighlightSupport::clientInitialized, Qt::SingleShotConnection);
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);
231 QLspSpecification::SemanticTokensOptions options;
232 options.range =
true;
233 options.full = QJsonObject({ { u"delta"_s,
true } });
235 options.legend.tokenTypes = extendedTokenTypesList();
236 options.legend.tokenModifiers = defaultTokenModifiersList();
237 caps.semanticTokensProvider = options;