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.
This commit is contained in:
faumaray committed 2026-12-06 02:20:22 +03:00
1 parent 1f01bcbfd5
commit 7e3ce6fc6c
6 files changed
+109 -9

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 | | textDocument sync (full + incremental, UTF-8/UTF-16) | document suite, real stdio corpora |
| publishDiagnostics (stock GCC, in-process default profile) | diagnostic suite, stdio corpus | | publishDiagnostics (stock GCC, in-process default profile) | diagnostic suite, stdio corpus |
| textDocument/hover, definition | stock occurrence 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/references, prepareRename, rename | navigation suite, stock corpus refusals |
| textDocument/completion (+ completionItem/resolve) | `vls_feature_tests`, real stdio session | | textDocument/completion (+ completionItem/resolve) | `vls_feature_tests`, real stdio session |
| textDocument/signatureHelp | `vls_feature_tests`, real stdio session | | textDocument/signatureHelp | `vls_feature_tests`, real stdio session |
+16
View File
@@ -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` LSP adapters and index/rename/debugging integration remain open. `vbs test`
passed all 17 native suites; CMake passed the reflection and trace suites. 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 ## December 6 base-class tokens in semantic tokens
Semantic tokens now also cover the observer-reported base-class name spans Semantic tokens now also cover the observer-reported base-class name spans
+5
View File
@@ -250,4 +250,9 @@ struct Position {
// than a lexical guess. // than a lexical guess.
[[nodiscard]] std::optional<Hover> hover_at(const Snapshot &, std::string_view path, Position, [[nodiscard]] std::optional<Hover> hover_at(const Snapshot &, std::string_view path, Position,
const Limits & = Limits{}); 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<Range> definition_at(const Snapshot &, std::string_view path, Position,
const Limits & = Limits{});
} // namespace vls::reflection } // namespace vls::reflection
+43 -8
View File
@@ -334,6 +334,10 @@ DispatchResult Server::handle(std::string_view body) {
Json{Json::Object{{"resolveProvider", Json{false}}}}); Json{Json::Object{{"resolveProvider", Json{false}}}});
if (diagnostics_) if (diagnostics_)
capabilities.emplace("documentFormattingProvider", Json{true}); capabilities.emplace("documentFormattingProvider", Json{true});
if (reflection_) {
capabilities.emplace("hoverProvider", Json{true});
capabilities.emplace("definitionProvider", Json{true});
}
if (diagnostics_) { if (diagnostics_) {
capabilities.emplace("documentSymbolProvider", Json{true}); capabilities.emplace("documentSymbolProvider", Json{true});
capabilities.emplace("selectionRangeProvider", Json{true}); capabilities.emplace("selectionRangeProvider", Json{true});
@@ -407,7 +411,7 @@ DispatchResult Server::handle(std::string_view body) {
diagnostics_ ? diagnostics_->status(*uri) diagnostics_ ? diagnostics_->status(*uri)
: Json{Json::Object{{"state", Json{std::string{"disabled"}}}}}); : 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 *uri = as_string(member(member(params, "textDocument"), "uri"));
const auto *position = member(params, "position"); const auto *position = member(params, "position");
const auto *line = position ? member(position, "line") : nullptr; const auto *line = position ? member(position, "line") : nullptr;
@@ -424,7 +428,8 @@ DispatchResult Server::handle(std::string_view body) {
static_cast<std::uint32_t>(*line_value), static_cast<std::uint32_t>(*character_value)}; static_cast<std::uint32_t>(*line_value), static_cast<std::uint32_t>(*character_value)};
Json result{nullptr}; Json result{nullptr};
if (diagnostics_ && diagnostics_->supports_occurrences()) 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<std::nullptr_t>(result.value)) { if (std::holds_alternative<std::nullptr_t>(result.value)) {
// Reflection ranges are compiler byte columns; convert the client // Reflection ranges are compiler byte columns; convert the client
// position through the snapshot before matching. // 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); const auto line_start = document->line_index.line_start_offset(selected.line);
if (offset && line_start && *offset >= *line_start) { if (offset && line_start && *offset >= *line_start) {
const auto byte_character = static_cast<std::uint32_t>(*offset - *line_start); const auto byte_character = static_cast<std::uint32_t>(*offset - *line_start);
if (auto reflected = reflection::hover_at( const auto path = document->identity.canonical_path.native();
*reflection_, document->identity.canonical_path.native(), if (*method == "textDocument/hover") {
{selected.line, byte_character})) { if (auto reflected = reflection::hover_at(
Json::Object content{{"kind", Json{std::string{"markdown"}}}, *reflection_, path, {selected.line, byte_character})) {
{"value", Json{reflected->markup}}}; Json::Object content{{"kind", Json{std::string{"markdown"}}},
result = Json{Json::Object{{"contents", Json{std::move(content)}}}}; {"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<std::int64_t>(value.line)}},
{"character",
Json{static_cast<std::int64_t>(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)};
}
}
} }
} }
} }
+30
View File
@@ -365,6 +365,36 @@ std::optional<Hover> hover_at(const Snapshot &snapshot, std::string_view path, P
return hover(*best, *expansion, limits); return hover(*best, *expansion, limits);
} }
std::optional<Range> 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<const SpliceOrigin> splices, Limits limits) { Snapshot snapshot_from_splices(std::span<const SpliceOrigin> splices, Limits limits) {
Snapshot snapshot{limits}; Snapshot snapshot{limits};
const auto range_at = [](std::string_view path, std::uint32_t line, std::uint32_t column) { const auto range_at = [](std::string_view path, std::uint32_t line, std::uint32_t column) {
+14
View File
@@ -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/a.cpp", {9, 0}).has_value());
expect(!hover_at(snapshot, "/p/b.cpp", {5, 6}).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());
}