diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index c41c963..73bc575 100644 --- a/docs/agent-workflow/LSP-PARITY.md +++ b/docs/agent-workflow/LSP-PARITY.md @@ -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 | diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 68025f8..00f02cc 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -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 diff --git a/include/vls/reflection/Provenance.hpp b/include/vls/reflection/Provenance.hpp index 7634808..fa5e72d 100644 --- a/include/vls/reflection/Provenance.hpp +++ b/include/vls/reflection/Provenance.hpp @@ -255,4 +255,9 @@ struct Position { // reflected-declaration link, else the first source link of its chain. [[nodiscard]] std::optional 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 references_at(const Snapshot &, std::string_view path, Position, + std::size_t limit = 256); } // namespace vls::reflection diff --git a/src/Server.cpp b/src/Server.cpp index fb2def0..d17f54e 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -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(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(value.line)}}, + {"character", Json{static_cast(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. diff --git a/src/reflection/Provenance.cpp b/src/reflection/Provenance.cpp index 0db9272..9cc1b2d 100644 --- a/src/reflection/Provenance.cpp +++ b/src/reflection/Provenance.cpp @@ -395,6 +395,45 @@ std::optional definition_at(const Snapshot &snapshot, std::string_view pa return std::nullopt; } +std::vector 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 result; + if (!selected) + return result; + std::set 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 splices, Limits limits) { Snapshot snapshot{limits}; const auto range_at = [](std::string_view path, std::uint32_t line, std::uint32_t column) { diff --git a/tests/reflection/ProvenanceTests.cpp b/tests/reflection/ProvenanceTests.cpp index 909d06b..14e7238 100644 --- a/tests/reflection/ProvenanceTests.cpp +++ b/tests/reflection/ProvenanceTests.cpp @@ -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()); +}