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())
+15 -6
View File
@@ -15,7 +15,7 @@ namespace Workspace = Void::LanguageServer::Workspace;
struct Options {
std::optional<std::string> root, user_file, provider, database, selector, build_root,
producer_cwd, profile, gcc_profile;
producer_cwd, profile, gcc_profile, reflection_snapshot;
bool gcc_diagnostics{};
};
@@ -23,8 +23,8 @@ std::expected<vls::WorkspaceLaunch, std::string> workspace_launch(const Options
vls::WorkspaceLaunch launch;
if (!options.root) {
if (options.user_file || options.provider || options.database || options.selector ||
options.build_root || options.producer_cwd || options.profile ||
options.gcc_profile || options.gcc_diagnostics)
options.build_root || options.producer_cwd || options.profile || options.gcc_profile ||
options.gcc_diagnostics || options.reflection_snapshot)
return std::unexpected{"workspace options require --workspace-root"};
return launch;
}
@@ -58,6 +58,13 @@ std::expected<vls::WorkspaceLaunch, std::string> workspace_launch(const Options
return std::unexpected{!build ? build.error() : producer.error()};
launch.build_root = std::move(*build);
launch.producer_cwd = std::move(*producer);
if (options.reflection_snapshot) {
const fs::path snapshot{*options.reflection_snapshot};
std::error_code snapshot_error;
if (!snapshot.is_absolute() || !fs::is_regular_file(snapshot, snapshot_error))
return std::unexpected{"--reflection-snapshot requires an existing absolute file"};
launch.reflection_snapshot = snapshot;
}
Workspace::SettingsOverrides overrides;
overrides.compdb_path = options.database;
overrides.vbs_manifest_path = options.selector;
@@ -70,9 +77,8 @@ std::expected<vls::WorkspaceLaunch, std::string> workspace_launch(const Options
const bool diagnostics_default =
!options.gcc_profile && (!options.provider || *options.provider != "none");
if (options.gcc_profile || options.gcc_diagnostics || diagnostics_default) {
auto profile = options.gcc_profile
? vls::semantic::GccProfile::load(*options.gcc_profile)
: vls::semantic::GccProfile::prepare("g++");
auto profile = options.gcc_profile ? vls::semantic::GccProfile::load(*options.gcc_profile)
: vls::semantic::GccProfile::prepare("g++");
if (!profile) {
if (options.gcc_profile || options.gcc_diagnostics)
return std::unexpected{profile.error()};
@@ -125,6 +131,9 @@ int main(int argc, char **argv) {
"--gcc-profile", "",
"enable stock GCC diagnostics for this workspace using an absolute prepared profile",
options.gcc_profile);
parser.option("--reflection-snapshot", "",
"versioned reflection provenance snapshot served as read-only hover",
options.reflection_snapshot);
parser.flag("--gcc-diagnostics", "",
"prepare stock GCC diagnostics in process from the selected GCC driver "
"(enabled by default when a workspace is selected)",
+27
View File
@@ -338,6 +338,33 @@ Hover hover(const Entity &entity, const Expansion &expansion, const Limits &limi
return result;
}
std::optional<Hover> hover_at(const Snapshot &snapshot, std::string_view path, Position position,
const Limits &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;
const auto expansion = snapshot.expand(best->id);
if (!expansion)
return std::nullopt;
return hover(*best, *expansion, limits);
}
Snapshot snapshot_from_splices(std::span<const SpliceOrigin> splices, Limits limits) {
Snapshot snapshot{limits};
const auto range_at = [](std::string_view path, std::uint32_t line, std::uint32_t column) {