Serve references from reflection provenance chains
reflection::references_at joins the selected entity's links with every origin elsewhere naming the same entity, deduplicated and bounded, with a registered test. The server serves textDocument/references over a recorded reflection snapshot and advertises referencesProvider when one is loaded. A real stdio session returned the splice and the reflected declaration ranges.
This commit is contained in:
1 parent
5f41870a75
commit
98ddf08eaf
6 files changed
+122
-3
No files matched your search
+37
-2
@@ -337,6 +337,7 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
if (reflection_) {
|
||||
capabilities.emplace("hoverProvider", Json{true});
|
||||
capabilities.emplace("definitionProvider", Json{true});
|
||||
capabilities.emplace("referencesProvider", Json{true});
|
||||
}
|
||||
if (diagnostics_) {
|
||||
capabilities.emplace("documentSymbolProvider", Json{true});
|
||||
@@ -411,7 +412,9 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
diagnostics_ ? diagnostics_->status(*uri)
|
||||
: Json{Json::Object{{"state", Json{std::string{"disabled"}}}}});
|
||||
}
|
||||
if ((*method == "textDocument/hover" || *method == "textDocument/definition") && reflection_) {
|
||||
if ((*method == "textDocument/hover" || *method == "textDocument/definition" ||
|
||||
*method == "textDocument/references") &&
|
||||
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;
|
||||
@@ -429,7 +432,9 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
Json result{nullptr};
|
||||
if (diagnostics_ && diagnostics_->supports_occurrences())
|
||||
result = *method == "textDocument/hover" ? diagnostics_->hover(*uri, selected)
|
||||
: diagnostics_->definition(*uri, selected);
|
||||
: *method == "textDocument/definition"
|
||||
? diagnostics_->definition(*uri, selected)
|
||||
: diagnostics_->references(*uri, selected).result;
|
||||
if (std::holds_alternative<std::nullptr_t>(result.value)) {
|
||||
// Reflection ranges are compiler byte columns; convert the client
|
||||
// position through the snapshot before matching.
|
||||
@@ -446,6 +451,36 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
{"value", Json{reflected->markup}}};
|
||||
result = Json{Json::Object{{"contents", Json{std::move(content)}}}};
|
||||
}
|
||||
} else if (*method == "textDocument/references") {
|
||||
const auto links = reflection::references_at(
|
||||
*reflection_, path, {selected.line, byte_character});
|
||||
Json::Array locations;
|
||||
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)}}}};
|
||||
};
|
||||
for (const auto &range : links) {
|
||||
const auto start_line_offset =
|
||||
document->line_index.line_start_offset(range.line);
|
||||
const auto end_line_offset =
|
||||
document->line_index.line_start_offset(range.endLine);
|
||||
if (!start_line_offset || !end_line_offset)
|
||||
continue;
|
||||
const auto start =
|
||||
document->offset_to_position(*start_line_offset + range.character);
|
||||
const auto end =
|
||||
document->offset_to_position(*end_line_offset + range.endCharacter);
|
||||
if (!start || !end)
|
||||
continue;
|
||||
Json::Object location_range{{"start", point(*start)},
|
||||
{"end", point(*end)}};
|
||||
locations.push_back(Json{Json::Object{
|
||||
{"uri", Json{Void::LanguageServer::Documents::path_to_file_uri(
|
||||
range.path)}},
|
||||
{"range", Json{std::move(location_range)}}}});
|
||||
}
|
||||
result = Json{std::move(locations)};
|
||||
} else if (auto target = reflection::definition_at(
|
||||
*reflection_, path, {selected.line, byte_character})) {
|
||||
// Convert the compiler byte range back to the client encoding.
|
||||
|
||||
Reference in new issue
Block a user