Serve references from reflection provenance chains

reflection::references_at joins the selected entity's links with every
origin elsewhere naming the same entity, deduplicated and bounded,
with a registered test. The server serves textDocument/references over
a recorded reflection snapshot and advertises referencesProvider when
one is loaded. A real stdio session returned the splice and the
reflected declaration ranges.
This commit is contained in:
faumaray committed 2026-12-06 03:13:51 +03:00
1 parent 5f41870a75
commit 98ddf08eaf
6 files changed
+122 -3

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints.
| textDocument sync (full + incremental, UTF-8/UTF-16) | document suite, real stdio corpora |
| publishDiagnostics (stock GCC, in-process default profile) | diagnostic suite, stdio corpus |
| textDocument/hover, definition | stock occurrence corpus |
| textDocument/hover and definition over reflection snapshots | recorded-snapshot stdio session |
| textDocument/hover, definition and references over reflection snapshots | recorded-snapshot stdio session |
| textDocument/references, prepareRename, rename | navigation suite, stock corpus refusals |
| textDocument/completion (+ completionItem/resolve) | `vls_feature_tests`, real stdio session |
| textDocument/signatureHelp | `vls_feature_tests`, real stdio session |
+14
View File
@@ -61,6 +61,20 @@ start coordinate only, so ranges are zero-width until an end is available.
LSP adapters and index/rename/debugging integration remain open. `vbs test`
passed all 17 native suites; CMake passed the reflection and trace suites.
## December 6 reflection references
`reflection::references_at` collects every source link of the entity covering a
position: the links named by its origins join all origins elsewhere that name
the same entity id, deduplicated and bounded (registered
`vls_reflection_tests` case, 27 total). The server serves
`textDocument/references` over a recorded reflection snapshot and advertises
`referencesProvider` when one is loaded. Real stdio session:
```
references inside: [{"range": {"start": {"line": 0, "character": 8}, "end": {"line": 0, "character": 13}}, "uri": "file:///.../main.cpp"},
{"range": {"start": {"line": 1, "character": 5}, "end": {"line": 1, "character": 10}}, "uri": "file:///.../main.cpp"}]
```
All 18 native suites and all 18 CMake tests passed.
## December 6 versioned workspace edits in the VS Code client
`applyRename` accepts a full WorkspaceEdit: a `changes` map or versioned
+5
View File
@@ -255,4 +255,9 @@ struct Position {
// reflected-declaration link, else the first source link of its chain.
[[nodiscard]] std::optional<Range> definition_at(const Snapshot &, std::string_view path, Position,
const Limits & = Limits{});
// Every source link of the entity covering POSITION (its own chain plus any
// origin elsewhere that names the same entity), deduplicated and bounded.
[[nodiscard]] std::vector<Range> references_at(const Snapshot &, std::string_view path, Position,
std::size_t limit = 256);
} // namespace vls::reflection
+37 -2
View File
@@ -337,6 +337,7 @@ DispatchResult Server::handle(std::string_view body) {
if (reflection_) {
capabilities.emplace("hoverProvider", Json{true});
capabilities.emplace("definitionProvider", Json{true});
capabilities.emplace("referencesProvider", Json{true});
}
if (diagnostics_) {
capabilities.emplace("documentSymbolProvider", Json{true});
@@ -411,7 +412,9 @@ DispatchResult Server::handle(std::string_view body) {
diagnostics_ ? diagnostics_->status(*uri)
: Json{Json::Object{{"state", Json{std::string{"disabled"}}}}});
}
if ((*method == "textDocument/hover" || *method == "textDocument/definition") && reflection_) {
if ((*method == "textDocument/hover" || *method == "textDocument/definition" ||
*method == "textDocument/references") &&
reflection_) {
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
const auto *position = member(params, "position");
const auto *line = position ? member(position, "line") : nullptr;
@@ -429,7 +432,9 @@ DispatchResult Server::handle(std::string_view body) {
Json result{nullptr};
if (diagnostics_ && diagnostics_->supports_occurrences())
result = *method == "textDocument/hover" ? diagnostics_->hover(*uri, selected)
: diagnostics_->definition(*uri, selected);
: *method == "textDocument/definition"
? diagnostics_->definition(*uri, selected)
: diagnostics_->references(*uri, selected).result;
if (std::holds_alternative<std::nullptr_t>(result.value)) {
// Reflection ranges are compiler byte columns; convert the client
// position through the snapshot before matching.
@@ -446,6 +451,36 @@ DispatchResult Server::handle(std::string_view body) {
{"value", Json{reflected->markup}}};
result = Json{Json::Object{{"contents", Json{std::move(content)}}}};
}
} else if (*method == "textDocument/references") {
const auto links = reflection::references_at(
*reflection_, path, {selected.line, byte_character});
Json::Array locations;
const auto point = [](const auto &value) {
return Json{Json::Object{
{"line", Json{static_cast<std::int64_t>(value.line)}},
{"character", Json{static_cast<std::int64_t>(value.character)}}}};
};
for (const auto &range : links) {
const auto start_line_offset =
document->line_index.line_start_offset(range.line);
const auto end_line_offset =
document->line_index.line_start_offset(range.endLine);
if (!start_line_offset || !end_line_offset)
continue;
const auto start =
document->offset_to_position(*start_line_offset + range.character);
const auto end =
document->offset_to_position(*end_line_offset + range.endCharacter);
if (!start || !end)
continue;
Json::Object location_range{{"start", point(*start)},
{"end", point(*end)}};
locations.push_back(Json{Json::Object{
{"uri", Json{Void::LanguageServer::Documents::path_to_file_uri(
range.path)}},
{"range", Json{std::move(location_range)}}}});
}
result = Json{std::move(locations)};
} else if (auto target = reflection::definition_at(
*reflection_, path, {selected.line, byte_character})) {
// Convert the compiler byte range back to the client encoding.
+39
View File
@@ -395,6 +395,45 @@ std::optional<Range> definition_at(const Snapshot &snapshot, std::string_view pa
return std::nullopt;
}
std::vector<Range> references_at(const Snapshot &snapshot, std::string_view path, Position position,
std::size_t limit) {
const EntityId *selected{};
Range best_range{};
for (const auto &entity : snapshot.entities()) {
for (const auto &origin : entity.provenance.origins) {
if (!origin.range || origin.range->path != path)
continue;
const auto &range = *origin.range;
const Position start{range.line, range.character};
const Position end{range.endLine, range.endCharacter};
if (position < start || end < position)
continue;
if (!selected || start > Position{best_range.line, best_range.character}) {
selected = &entity.id;
best_range = range;
}
}
}
std::vector<Range> result;
if (!selected)
return result;
std::set<EntityId> targets{*selected};
for (const auto &entity : snapshot.entities())
if (entity.id == *selected)
for (const auto &origin : entity.provenance.origins)
if (origin.entity)
targets.insert(*origin.entity);
for (const auto &entity : snapshot.entities())
for (const auto &origin : entity.provenance.origins)
if (origin.range && origin.entity && targets.contains(*origin.entity) &&
std::ranges::find(result, *origin.range) == result.end()) {
if (result.size() == limit)
return result;
result.push_back(*origin.range);
}
return result;
}
Snapshot snapshot_from_splices(std::span<const SpliceOrigin> splices, Limits limits) {
Snapshot snapshot{limits};
const auto range_at = [](std::string_view path, std::uint32_t line, std::uint32_t column) {
+26
View File
@@ -528,3 +528,29 @@ VOID_TEST(Reflection, definition_at_prefers_the_reflected_declaration) {
expect(target->path == "/p/b.hpp" && target->line == 1);
expect(!definition_at(snapshot, "/p/a.cpp", {9, 0}).has_value());
}
VOID_TEST(Reflection, references_at_collects_every_link_of_one_entity) {
Snapshot snapshot;
Entity first = entity("first");
first.provenance = chain(
{Origin{.kind = OriginKind::splice, .range = range("/p/a.cpp", 4), .entity = "shared"},
Origin{.kind = OriginKind::reflected_declaration,
.range = range("/p/b.hpp", 1),
.entity = "shared"}});
Entity second = entity("second");
second.provenance = chain(
{Origin{.kind = OriginKind::splice, .range = range("/p/c.cpp", 8), .entity = "shared"}});
Entity unrelated = entity("unrelated");
unrelated.provenance = chain(
{Origin{.kind = OriginKind::splice, .range = range("/p/d.cpp", 2), .entity = "other"}});
require(snapshot.add(std::move(first)));
require(snapshot.add(std::move(second)));
require(snapshot.add(std::move(unrelated)));
const auto result = references_at(snapshot, "/p/a.cpp", {4, 1});
require(result.size() == 3);
expect(result[0].path == "/p/a.cpp");
expect(result[1].path == "/p/b.hpp");
expect(result[2].path == "/p/c.cpp");
expect(references_at(snapshot, "/p/a.cpp", {4, 1}, 1).size() == 1);
expect(references_at(snapshot, "/p/a.cpp", {9, 0}).empty());
}