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);
59 const QString code = doc.textDocument->toPlainText();
60 const HighlightsContainer highlights =
61 QmlHighlighting::Utils::regexFallbackHighlights(code, range);
62 if (highlights.isEmpty())
64 return QmlHighlighting::Utils::encodeSemanticTokens(highlights, mode);
67 HighlightsContainer highlights = QmlHighlighting::Utils::visitTokens(file, range);
68 if (highlights.isEmpty())
71 if (!range.has_value() )
72 cached.highlights = highlights;
74 return QmlHighlighting::Utils::encodeSemanticTokens(highlights, mode);
90 QQmlBaseModule<SemanticTokensRequest>::RequestPointerArgument request)
93 qCWarning(semanticTokens) <<
"No semantic token request is available!";
97 Responses::SemanticTokensResultType result;
98 ResponseScopeGuard guard(result, request->m_response);
99 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
100 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
101 auto &cached = m_codeModelManager->registeredTokens(uri);
102 auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
104 if (encoded.isEmpty()) {
107 result = SemanticTokens{cached.resultId, std::move(encoded)};
129 QQmlBaseModule<SemanticTokensDeltaRequest>::RequestPointerArgument request)
132 qCWarning(semanticTokens) <<
"No semantic token request is available!";
136 Responses::SemanticTokensDeltaResultType result;
137 ResponseScopeGuard guard(result, request->m_response);
138 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
139 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
140 auto &cached = m_codeModelManager->registeredTokens(uri);
141 if (cached.resultId != request->m_parameters.previousResultId) {
143 cached.resultId = request->m_parameters.previousResultId;
144 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
145 result = QLspSpecification::SemanticTokens{ cached.resultId, encoded };
147 const auto cachedHighlights = QmlHighlighting::Utils::encodeSemanticTokens(cached.highlights);
148 const auto encoded = generateHighlights(cached, doc, std::nullopt, m_mode);
149 result = QLspSpecification::SemanticTokensDelta{
151 QmlHighlighting::Utils::computeDiff(cachedHighlights, encoded)
173 QQmlBaseModule<SemanticTokensRangeRequest>::RequestPointerArgument request)
176 qCWarning(semanticTokens) <<
"No semantic token request is available!";
180 Responses::SemanticTokensRangeResultType result;
181 ResponseScopeGuard guard(result, request->m_response);
182 const QByteArray uri = QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri);
183 const auto doc = m_codeModelManager->openDocumentByUrl(uri);
184 const QString code = doc.textDocument->toPlainText();
185 const auto range = request->m_parameters.range;
187 int(QQmlLSUtils::textOffsetFrom(code, range.start.line, range.end.character));
188 int endOffset =
int(QQmlLSUtils::textOffsetFrom(code, range.end.line, range.end.character));
189 auto &cached = m_codeModelManager->registeredTokens(uri);
190 auto encodedTokens = generateHighlights(
193 QmlHighlighting::HighlightsRange{ startOffset, endOffset },
195 if (encodedTokens.isEmpty()) {
197 result = SemanticTokens{ cached.resultId, std::move(encodedTokens) };
213 m_full.registerHandlers(server, protocol);
214 m_delta.registerHandlers(server, protocol);
215 m_range.registerHandlers(server, protocol);
217 QObject::connect(server, &QLanguageServer::clientInitialized,
this,
218 &QQmlHighlightSupport::clientInitialized, Qt::SingleShotConnection);
221 m_full.manager(), &QmlLsp::QQmlCodeModelManager::configurationChanged,
this,
222 [protocol]() { protocol->requestWorkspaceSemanticTokensRefresh({ }, []() { }); });
227 if (
auto clientInitOptions = server->clientInfo().initializationOptions) {
228 if ((*clientInitOptions)[u"qtCreatorHighlighting"_s].toBool(
false)) {
229 const auto mode = HighlightingMode::QtCHighlighting;
230 m_delta.setHighlightingMode(mode);
231 m_full.setHighlightingMode(mode);
232 m_range.setHighlightingMode(mode);
239 QLspSpecification::SemanticTokensOptions options;
240 options.range =
true;
241 options.full = QJsonObject({ { u"delta"_s,
true } });
243 options.legend.tokenTypes = extendedTokenTypesList();
244 options.legend.tokenModifiers = defaultTokenModifiersList();
245 caps.semanticTokensProvider = options;