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

+43 -8
View File
@@ -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<std::uint32_t>(*line_value), static_cast<std::uint32_t>(*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<std::nullptr_t>(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<std::uint32_t>(*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<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)};
}
}
}
}
}