Serve semantic tokens from stock occurrences
A bounded core normalizes occurrence tokens (sorted, deduplicated, overlap-free, capped) and delta-encodes them. EditorDiagnostics emits one function token per proven call occurrence in the owner snapshot, and the server advertises a one-type legend and serves textDocument/semanticTokens/full. Registered tests cover normalization, encoding, overlap and bounds; a real stdio session decoded the fnv1a64 callee token.
This commit is contained in:
1 parent
946b1a005e
commit
12a71ff692
12 files changed
+220
-8
No files matched your search
@@ -266,6 +266,14 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
capabilities.emplace("selectionRangeProvider", Json{true});
|
||||
capabilities.emplace("foldingRangeProvider", Json{true});
|
||||
}
|
||||
if (diagnostics_ && diagnostics_->supports_occurrences())
|
||||
capabilities.emplace(
|
||||
"semanticTokensProvider",
|
||||
Json{Json::Object{
|
||||
{"legend", Json{Json::Object{
|
||||
{"tokenTypes", Json{Json::Array{Json{std::string{"function"}}}}},
|
||||
{"tokenModifiers", Json{Json::Array{}}}}}},
|
||||
{"full", Json{true}}}});
|
||||
if (diagnostics_ && diagnostics_->supports_occurrences())
|
||||
capabilities.emplace(
|
||||
"codeActionProvider",
|
||||
@@ -378,6 +386,14 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
}
|
||||
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
|
||||
}
|
||||
if (*method == "textDocument/semanticTokens/full") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
|
||||
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri))
|
||||
return error(id, -32602, "Expected textDocument.uri");
|
||||
return reply(id, "result", diagnostics_->semantic_tokens(*uri));
|
||||
}
|
||||
if (*method == "textDocument/codeAction") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <vls/features/SemanticTokens.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
|
||||
namespace vls::features::semantic_tokens {
|
||||
|
||||
std::vector<Token> normalize(std::span<const Token> tokens, const Limits &limits) {
|
||||
std::vector<Token> sorted;
|
||||
sorted.reserve(tokens.size());
|
||||
for (const auto &token : tokens)
|
||||
if (token.length != 0 && token.length <= limits.length)
|
||||
sorted.push_back(token);
|
||||
std::ranges::sort(sorted);
|
||||
std::vector<Token> result;
|
||||
for (const auto &token : sorted) {
|
||||
if (result.size() >= limits.tokens)
|
||||
break;
|
||||
if (!result.empty()) {
|
||||
const auto &last = result.back();
|
||||
if (last == token)
|
||||
continue;
|
||||
// Overlap on the same line: keep the earlier token.
|
||||
if (last.line == token.line && last.character + last.length > token.character)
|
||||
continue;
|
||||
}
|
||||
result.push_back(token);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::uint32_t> encode(std::span<const Token> tokens) {
|
||||
std::vector<std::uint32_t> data;
|
||||
data.reserve(tokens.size() * 5);
|
||||
std::uint32_t previous_line{};
|
||||
std::uint32_t previous_character{};
|
||||
for (const auto &token : tokens) {
|
||||
const std::uint32_t delta_line = token.line - previous_line;
|
||||
const std::uint32_t delta_start =
|
||||
delta_line == 0 ? token.character - previous_character : token.character;
|
||||
data.push_back(delta_line);
|
||||
data.push_back(delta_start);
|
||||
data.push_back(token.length);
|
||||
data.push_back(token.type);
|
||||
data.push_back(token.modifiers);
|
||||
previous_line = token.line;
|
||||
previous_character = token.character;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
} // namespace vls::features::semantic_tokens
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <vls/features/Completion.hpp>
|
||||
#include <vls/features/DocumentSymbols.hpp>
|
||||
#include <vls/features/IndexNavigation.hpp>
|
||||
#include <vls/features/SemanticTokens.hpp>
|
||||
#include <vls/features/SignatureHelp.hpp>
|
||||
#include <vls/features/TextStructure.hpp>
|
||||
#include <vls/index/SymbolIndex.hpp>
|
||||
@@ -1159,6 +1160,45 @@ Json EditorDiagnostics::code_actions(std::string_view uri, Documents::Range rang
|
||||
return Json{std::move(actions)};
|
||||
}
|
||||
|
||||
Json EditorDiagnostics::semantic_tokens(std::string_view uri) const {
|
||||
auto &s = *state_;
|
||||
auto key = Documents::client_uri_key(uri);
|
||||
std::shared_ptr<const Receipt> receipt;
|
||||
std::shared_ptr<const std::vector<Occurrence>> facts;
|
||||
{
|
||||
std::lock_guard lock{s.mutex};
|
||||
const auto i = key ? s.entries.find(*key) : s.entries.end();
|
||||
if (i == s.entries.end())
|
||||
return Json{Json::Object{{"data", Json{Json::Array{}}}}};
|
||||
receipt = i->second.receipt;
|
||||
facts = i->second.occurrences;
|
||||
}
|
||||
if (!receipt || !facts || !s.valid(*receipt))
|
||||
return Json{Json::Object{{"data", Json{Json::Array{}}}}};
|
||||
const Documents::DocumentSnapshot *snapshot = nullptr;
|
||||
for (const auto &entry : receipt->capture->entries())
|
||||
if (entry.snapshot && entry.snapshot->identity.canonical_uri == *key) {
|
||||
snapshot = entry.snapshot.get();
|
||||
break;
|
||||
}
|
||||
if (!snapshot)
|
||||
return Json{Json::Object{{"data", Json{Json::Array{}}}}};
|
||||
std::vector<features::semantic_tokens::Token> tokens;
|
||||
for (const auto &occurrence : *facts) {
|
||||
if (occurrence.use_file.get() != snapshot ||
|
||||
occurrence.use.start.line != occurrence.use.end.line ||
|
||||
occurrence.use.end.character <= occurrence.use.start.character)
|
||||
continue;
|
||||
tokens.push_back({occurrence.use.start.line, occurrence.use.start.character,
|
||||
occurrence.use.end.character - occurrence.use.start.character});
|
||||
}
|
||||
const auto normalized = features::semantic_tokens::normalize(tokens);
|
||||
Json::Array data;
|
||||
for (const auto value : features::semantic_tokens::encode(normalized))
|
||||
data.push_back(Json{static_cast<std::int64_t>(value)});
|
||||
return Json{Json::Object{{"data", Json{std::move(data)}}}};
|
||||
}
|
||||
|
||||
void EditorDiagnostics::set_transactional_rename(bool supported) {
|
||||
std::lock_guard lock{state_->mutex};
|
||||
state_->transactional_rename = supported;
|
||||
|
||||
Reference in new issue
Block a user