From 7e3ce6fc6c95b79637816aea91446d928f15a0ea Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 02:20:22 +0300 Subject: [PATCH] Serve definition from reflection provenance chains reflection::definition_at returns the innermost covering entity's first reflected-declaration link (else its first chain link), with a registered test. The server serves textDocument/definition over a recorded reflection snapshot and now advertises hover and definition whenever a snapshot is loaded, not only with the occurrence observer. A real stdio session resolved the splice site to the reflected declaration range. --- docs/agent-workflow/LSP-PARITY.md | 2 +- docs/agent-workflow/VERIFICATION.md | 16 +++++++++ include/vls/reflection/Provenance.hpp | 5 +++ src/Server.cpp | 51 ++++++++++++++++++++++----- src/reflection/Provenance.cpp | 30 ++++++++++++++++ tests/reflection/ProvenanceTests.cpp | 14 ++++++++ 6 files changed, 109 insertions(+), 9 deletions(-) diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index df2552c..6223dda 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 over reflection snapshots | recorded-snapshot stdio session | +| textDocument/hover and definition 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 ce4b07f..dbb6211 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -61,6 +61,22 @@ 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 definition and capability advertisement + +`reflection::definition_at` selects the innermost entity covering a position +and returns its first reflected-declaration link (else the first chain link), +with a registered `vls_reflection_tests` case. The server now serves +`textDocument/definition` over a recorded reflection snapshot, and advertises +hover and definition whenever a snapshot is loaded (they previously worked but +were only advertised with the occurrence observer). Real stdio session: +``` +definition inside: {"range": {"start": {"character": 5, "line": 1}, + "end": {"character": 10, "line": 1}}, + "uri": "file:///.../main.cpp"} +hover outside: null +``` +All 18 native suites and all 18 CMake tests passed. + ## December 6 base-class tokens in semantic tokens Semantic tokens now also cover the observer-reported base-class name spans diff --git a/include/vls/reflection/Provenance.hpp b/include/vls/reflection/Provenance.hpp index 34de89d..7634808 100644 --- a/include/vls/reflection/Provenance.hpp +++ b/include/vls/reflection/Provenance.hpp @@ -250,4 +250,9 @@ struct Position { // than a lexical guess. [[nodiscard]] std::optional hover_at(const Snapshot &, std::string_view path, Position, const Limits & = Limits{}); + +// Navigation target for the innermost entity covering POSITION: the first +// 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{}); } // namespace vls::reflection diff --git a/src/Server.cpp b/src/Server.cpp index e546e58..fb2def0 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -334,6 +334,10 @@ DispatchResult Server::handle(std::string_view body) { Json{Json::Object{{"resolveProvider", Json{false}}}}); if (diagnostics_) capabilities.emplace("documentFormattingProvider", Json{true}); + if (reflection_) { + capabilities.emplace("hoverProvider", Json{true}); + capabilities.emplace("definitionProvider", Json{true}); + } if (diagnostics_) { capabilities.emplace("documentSymbolProvider", Json{true}); capabilities.emplace("selectionRangeProvider", Json{true}); @@ -407,7 +411,7 @@ DispatchResult Server::handle(std::string_view body) { diagnostics_ ? diagnostics_->status(*uri) : Json{Json::Object{{"state", Json{std::string{"disabled"}}}}}); } - if (*method == "textDocument/hover" && reflection_) { + if ((*method == "textDocument/hover" || *method == "textDocument/definition") && 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; @@ -424,7 +428,8 @@ DispatchResult Server::handle(std::string_view body) { static_cast(*line_value), static_cast(*character_value)}; Json result{nullptr}; if (diagnostics_ && diagnostics_->supports_occurrences()) - result = diagnostics_->hover(*uri, selected); + result = *method == "textDocument/hover" ? diagnostics_->hover(*uri, selected) + : diagnostics_->definition(*uri, selected); if (std::holds_alternative(result.value)) { // Reflection ranges are compiler byte columns; convert the client // position through the snapshot before matching. @@ -433,12 +438,42 @@ DispatchResult Server::handle(std::string_view body) { const auto line_start = document->line_index.line_start_offset(selected.line); if (offset && line_start && *offset >= *line_start) { const auto byte_character = static_cast(*offset - *line_start); - if (auto reflected = reflection::hover_at( - *reflection_, document->identity.canonical_path.native(), - {selected.line, byte_character})) { - Json::Object content{{"kind", Json{std::string{"markdown"}}}, - {"value", Json{reflected->markup}}}; - result = Json{Json::Object{{"contents", Json{std::move(content)}}}}; + const auto path = document->identity.canonical_path.native(); + if (*method == "textDocument/hover") { + if (auto reflected = reflection::hover_at( + *reflection_, path, {selected.line, byte_character})) { + Json::Object content{{"kind", Json{std::string{"markdown"}}}, + {"value", Json{reflected->markup}}}; + result = Json{Json::Object{{"contents", Json{std::move(content)}}}}; + } + } else if (auto target = reflection::definition_at( + *reflection_, path, {selected.line, byte_character})) { + // Convert the compiler byte range back to the client encoding. + const auto line_offset = + document->line_index.line_start_offset(target->line); + const auto end_line_offset = + document->line_index.line_start_offset(target->endLine); + if (line_offset && end_line_offset) { + const auto start = + document->offset_to_position(*line_offset + target->character); + const auto end = document->offset_to_position(*end_line_offset + + target->endCharacter); + if (start && end) { + const auto point = [](const auto &value) { + return Json{Json::Object{ + {"line", Json{static_cast(value.line)}}, + {"character", + Json{static_cast(value.character)}}}}; + }; + Json::Object selected_range{{"start", point(*start)}, + {"end", point(*end)}}; + Json::Object location{ + {"uri", Json{Void::LanguageServer::Documents::path_to_file_uri( + target->path)}}, + {"range", Json{std::move(selected_range)}}}; + result = Json{std::move(location)}; + } + } } } } diff --git a/src/reflection/Provenance.cpp b/src/reflection/Provenance.cpp index 04b6b63..0db9272 100644 --- a/src/reflection/Provenance.cpp +++ b/src/reflection/Provenance.cpp @@ -365,6 +365,36 @@ std::optional hover_at(const Snapshot &snapshot, std::string_view path, P return hover(*best, *expansion, limits); } +std::optional definition_at(const Snapshot &snapshot, std::string_view path, + Position position, const Limits &) { + const Entity *best{}; + 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 (!best || start > Position{best_range.line, best_range.character}) { + best = &entity; + best_range = range; + } + } + } + if (!best) + return std::nullopt; + for (const auto &origin : best->provenance.origins) + if (origin.kind == OriginKind::reflected_declaration && origin.range) + return origin.range; + for (const auto &origin : best->provenance.origins) + if (origin.range) + return origin.range; + return std::nullopt; +} + 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 1d27f30..909d06b 100644 --- a/tests/reflection/ProvenanceTests.cpp +++ b/tests/reflection/ProvenanceTests.cpp @@ -514,3 +514,17 @@ VOID_TEST(Reflection, hover_at_selects_the_innermost_covering_entity) { expect(!hover_at(snapshot, "/p/a.cpp", {9, 0}).has_value()); expect(!hover_at(snapshot, "/p/b.cpp", {5, 6}).has_value()); } + +VOID_TEST(Reflection, definition_at_prefers_the_reflected_declaration) { + Snapshot snapshot; + Entity node = entity("node"); + node.name = "node"; + node.provenance = + chain({Origin{.kind = OriginKind::splice, .range = range("/p/a.cpp", 4)}, + Origin{.kind = OriginKind::reflected_declaration, .range = range("/p/b.hpp", 1)}}); + require(snapshot.add(std::move(node))); + const auto target = definition_at(snapshot, "/p/a.cpp", {4, 1}); + require(target.has_value()); + expect(target->path == "/p/b.hpp" && target->line == 1); + expect(!definition_at(snapshot, "/p/a.cpp", {9, 0}).has_value()); +}