Serve reference code lenses and the showReferences command

A bounded core builds one lens per document position with its proven
reference count. EditorDiagnostics counts index records by name and
definition and serves textDocument/codeLens; workspace/executeCommand
implements vls.showReferences via the navigation binding. Registered
feature tests cover titles, arguments, deduplication and bounds. A
real stdio session showed the one-reference lens at the fnv1a64 call
and resolved it to the source location.
This commit is contained in:
faumaray committed 2026-12-05 17:50:38 +03:00
1 parent ec86a79a1a
commit 0bf5873069
12 files changed
+279 -5

No files matched your search

+42
View File
@@ -312,6 +312,14 @@ DispatchResult Server::handle(std::string_view body) {
}
if (diagnostics_ && diagnostics_->supports_occurrences())
capabilities.emplace("callHierarchyProvider", Json{true});
if (diagnostics_ && diagnostics_->supports_occurrences()) {
capabilities.emplace("codeLensProvider",
Json{Json::Object{{"resolveProvider", Json{false}}}});
capabilities.emplace(
"executeCommandProvider",
Json{Json::Object{
{"commands", Json{Json::Array{Json{std::string{"vls.showReferences"}}}}}}});
}
if (diagnostics_ && diagnostics_->supports_occurrences())
capabilities.emplace("inlayHintProvider", Json{true});
if (diagnostics_ && diagnostics_->supports_occurrences())
@@ -434,6 +442,40 @@ DispatchResult Server::handle(std::string_view body) {
}
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
}
if (*method == "textDocument/codeLens") {
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_->code_lenses(*uri));
}
if (*method == "workspace/executeCommand") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())
return error(id, -32601, "Method not found");
const auto *command = as_string(member(params, "command"));
const auto *arguments = as_array(member(params, "arguments"));
if (!command || !arguments || arguments->size() != 2)
return error(id, -32602, "Expected a command and two arguments");
if (*command != "vls.showReferences")
return error(id, -32601, "Unregistered command");
const auto *uri = as_string(&(*arguments)[0]);
const auto *position = as_object(&(*arguments)[1]);
const auto line = position ? find_member(*position, "line") : nullptr;
const auto character = position ? find_member(*position, "character") : nullptr;
const auto *line_value = line ? std::get_if<std::int64_t>(&line->value) : nullptr;
const auto *character_value =
character ? std::get_if<std::int64_t>(&character->value) : nullptr;
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri) || !line_value ||
!character_value || *line_value < 0 || *character_value < 0 ||
*line_value > Void::LanguageServer::Documents::kMaxPositionValue ||
*character_value > Void::LanguageServer::Documents::kMaxPositionValue)
return error(id, -32602, "Expected textDocument.uri and a bounded position");
auto navigation =
diagnostics_->references(*uri, {static_cast<std::uint32_t>(*line_value),
static_cast<std::uint32_t>(*character_value)});
return reply(id, "result", std::move(navigation.result));
}
if (*method == "textDocument/prepareCallHierarchy") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())
return error(id, -32601, "Method not found");
+43
View File
@@ -0,0 +1,43 @@
#include <vls/features/CodeLens.hpp>
#include <algorithm>
#include <set>
#include <tuple>
#include <utility>
namespace vls::features::code_lens {
namespace {
std::string title(std::size_t references) {
return std::to_string(references) + (references == 1 ? " reference" : " references");
}
} // namespace
std::vector<Lens> build(std::span<const Entry> entries, const Limits &limits) {
std::vector<Lens> result;
std::set<std::tuple<std::string, Range>> seen;
for (const auto &entry : entries) {
if (entry.path.empty() || entry.name.empty() || entry.name.size() > limits.nameBytes ||
entry.range.end < entry.range.start)
continue;
if (!seen.emplace(entry.path, entry.range).second)
continue;
auto text = title(entry.references);
if (text.size() > limits.titleBytes)
text.resize(limits.titleBytes);
result.push_back({entry.range,
std::move(text),
{entry.path, std::to_string(entry.range.start.line),
std::to_string(entry.range.start.character)}});
}
std::ranges::sort(result, [](const Lens &left, const Lens &right) {
return std::tie(left.range.start, left.range.end) <
std::tie(right.range.start, right.range.end);
});
if (result.size() > limits.lenses)
result.resize(limits.lenses);
return result;
}
} // namespace vls::features::code_lens
+52
View File
@@ -2,6 +2,7 @@
#include <vls/features/CallHierarchy.hpp>
#include <vls/features/CodeActions.hpp>
#include <vls/features/CodeLens.hpp>
#include <vls/features/Completion.hpp>
#include <vls/features/DocumentSymbols.hpp>
#include <vls/features/IndexNavigation.hpp>
@@ -1038,6 +1039,57 @@ Json EditorDiagnostics::completion_resolve(const Json &item) const {
return Json{std::move(enriched)};
}
Json EditorDiagnostics::code_lenses(std::string_view uri) const {
auto &s = *state_;
const auto identity = Documents::normalize_uri(uri);
if (!identity || !identity->is_file_uri)
return Json{nullptr};
const auto path = identity->canonical_path.native();
index::Result records;
std::map<std::tuple<std::string, std::string, std::uint32_t, std::uint32_t>, std::size_t>
counts;
{
std::lock_guard lock{s.mutex};
records = s.symbols.find_by_path(path);
if (const auto generation = s.symbols.current())
for (const auto &record : generation->symbols()) {
const auto &where = record.definition ? *record.definition : record.location;
++counts[{record.name, where.path, where.line, where.character}];
}
}
std::vector<features::code_lens::Entry> entries;
for (const auto &record : records.symbols) {
const auto &where = record.definition ? *record.definition : record.location;
const auto found = counts.find({record.name, where.path, where.line, where.character});
entries.push_back({{record.name},
record.location.path,
{{record.location.line, record.location.character},
{record.location.endLine, record.location.endCharacter}},
found == counts.end() ? 0 : found->second});
}
const auto lenses = features::code_lens::build(entries);
Json::Array result;
for (const auto &lens : lenses) {
const auto &arguments = lens.arguments;
if (arguments.size() != 3)
continue;
result.push_back(Json{Json::Object{
{"range",
range_json(Documents::Range{{lens.range.start.line, lens.range.start.character},
{lens.range.end.line, lens.range.end.character}})},
{"command",
Json{Json::Object{
{"title", Json{lens.title}},
{"command", Json{std::string{"vls.showReferences"}}},
{"arguments",
Json{Json::Array{
Json{Documents::path_to_file_uri(arguments[0])},
Json{Json::Object{{"line", Json{std::stoll(arguments[1])}},
{"character", Json{std::stoll(arguments[2])}}}}}}}}}}}});
}
return Json{std::move(result)};
}
Json EditorDiagnostics::document_symbols(std::string_view uri) const {
auto &s = *state_;
const auto identity = Documents::normalize_uri(uri);