Serve reflection hover from a recorded snapshot

Per the user's decision, the launcher accepts --reflection-snapshot and
the server parses the versioned snapshot at startup. reflection::hover_at
selects the innermost entity whose provenance covers a position and
returns its bounded virtual view; hover falls back to it after compiler
occurrence hover, converting the client position because reflection
columns are bytes. Registered reflection tests grew to 25 cases, and a
real stdio session returned the Markdown view with source links inside
the recorded range and null outside it.
This commit is contained in:
faumaray committed 2026-12-05 20:46:41 +03:00
1 parent 62d6ad01e8
commit 7acbfc4029
11 files changed
+148 -9

No files matched your search

+49
View File
@@ -10,6 +10,8 @@
#include <deque>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <mutex>
#include <ostream>
#include <thread>
@@ -132,6 +134,15 @@ DispatchResult error(const Json &id, std::int64_t code, std::string_view message
} // namespace
Server::Server(WorkspaceLaunch launch, std::function<bool(std::string)> send) : workspace_(launch) {
if (launch.reflection_snapshot) {
std::ifstream stream{*launch.reflection_snapshot, std::ios::binary};
std::string bytes{std::istreambuf_iterator<char>{stream}, {}};
if (!bytes.empty()) {
auto parsed = reflection::Snapshot::parse(bytes);
if (parsed)
reflection_ = std::make_shared<const reflection::Snapshot>(std::move(*parsed));
}
}
if (launch.root && launch.diagnostics_profile && send)
diagnostics_ = std::make_unique<semantic::EditorDiagnostics>(
*launch.root, std::move(launch.diagnostics_profile), workspace_, documents_,
@@ -380,6 +391,44 @@ DispatchResult Server::handle(std::string_view body) {
diagnostics_ ? diagnostics_->status(*uri)
: Json{Json::Object{{"state", Json{std::string{"disabled"}}}}});
}
if (*method == "textDocument/hover" && 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;
const auto *character = position ? member(position, "character") : nullptr;
const auto *line_value = line ? std::get_if<std::int64_t>(&line->value) : nullptr;
const auto *character_value =
character ? std::get_if<std::int64_t>(&character->value) : nullptr;
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri) || !line_value ||
!character_value || *line_value < 0 || *character_value < 0 ||
*line_value > Void::LanguageServer::Documents::kMaxPositionValue ||
*character_value > Void::LanguageServer::Documents::kMaxPositionValue)
return error(id, -32602, "Expected textDocument.uri and a bounded position");
const Void::LanguageServer::Documents::Position selected{
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);
if (std::holds_alternative<std::nullptr_t>(result.value)) {
// Reflection ranges are compiler byte columns; convert the client
// position through the snapshot before matching.
if (const auto document = documents_.snapshot(*uri)) {
const auto offset = document->position_to_offset(selected);
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)}}}};
}
}
}
}
return reply(id, "result", std::move(result));
}
if (*method == "textDocument/hover" || *method == "textDocument/definition" ||
*method == "textDocument/signatureHelp" || *method == "textDocument/documentHighlight") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())