Serve outgoing call hierarchy
callHierarchy/outgoingCalls groups calls attributed to the prepared function's innermost enclosing definition by callee, with registered core tests for grouping, innermost attribution, unresolved-callee omission and bounds. Real stdio evidence shows the observer reports only the declarator span, so body calls cannot be attributed without guessing; the matrix records that limitation.
This commit is contained in:
1 parent
cec4e7d717
commit
a098e45dc9
9 files changed
+178
-2
No files matched your search
@@ -454,6 +454,14 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
*uri, {static_cast<std::uint32_t>(*line_value),
|
||||
static_cast<std::uint32_t>(*character_value)}));
|
||||
}
|
||||
if (*method == "callHierarchy/outgoingCalls") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
const auto *item = member(params, "item");
|
||||
if (!item)
|
||||
return error(id, -32602, "Expected a call hierarchy item");
|
||||
return reply(id, "result", diagnostics_->call_hierarchy_outgoing(*item));
|
||||
}
|
||||
if (*method == "callHierarchy/incomingCalls") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
|
||||
@@ -64,4 +64,40 @@ std::vector<Incoming> incoming(std::span<const Call> calls, std::span<const Defi
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<Outgoing> outgoing(std::span<const Call> calls, std::span<const Definition> known,
|
||||
const Definition &caller, const Limits &limits) {
|
||||
std::map<std::tuple<std::string, std::string, Range>, Outgoing> grouped;
|
||||
std::size_t total_ranges{};
|
||||
for (const auto &call : calls) {
|
||||
if (!call.callee_definition)
|
||||
continue;
|
||||
const Definition *enclosing{};
|
||||
for (const auto &definition : known) {
|
||||
if (definition.path != call.path || !contains(definition.range, call.range))
|
||||
continue;
|
||||
if (!enclosing || definition.range.start > enclosing->range.start)
|
||||
enclosing = &definition;
|
||||
}
|
||||
if (!enclosing || *enclosing != caller)
|
||||
continue;
|
||||
auto &slot = grouped[{call.callee_definition->name, call.callee_definition->path,
|
||||
call.callee_definition->range}];
|
||||
if (slot.to.name.empty())
|
||||
slot.to = *call.callee_definition;
|
||||
if (slot.from_ranges.size() < limits.ranges && total_ranges < limits.ranges &&
|
||||
std::ranges::find(slot.from_ranges, call.range) == slot.from_ranges.end()) {
|
||||
slot.from_ranges.push_back(call.range);
|
||||
++total_ranges;
|
||||
}
|
||||
}
|
||||
std::vector<Outgoing> result;
|
||||
for (auto &[key, entry] : grouped) {
|
||||
if (result.size() == limits.callers)
|
||||
break;
|
||||
std::ranges::sort(entry.from_ranges);
|
||||
result.push_back(std::move(entry));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace vls::features::call_hierarchy
|
||||
@@ -1503,6 +1503,71 @@ Json EditorDiagnostics::call_hierarchy_incoming(const Json &item) const {
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
|
||||
Json EditorDiagnostics::call_hierarchy_outgoing(const Json &item) const {
|
||||
auto &s = *state_;
|
||||
const auto *fields = P::as_object(&item);
|
||||
const auto *data = fields ? P::as_object(P::find_member(*fields, "data")) : nullptr;
|
||||
const auto *name = data ? P::as_string(P::find_member(*data, "name")) : nullptr;
|
||||
const auto *path = data ? P::as_string(P::find_member(*data, "path")) : nullptr;
|
||||
const auto line = data ? P::as_int64(P::find_member(*data, "line")) : std::nullopt;
|
||||
const auto character = data ? P::as_int64(P::find_member(*data, "character")) : std::nullopt;
|
||||
const auto end_line = data ? P::as_int64(P::find_member(*data, "endLine")) : std::nullopt;
|
||||
const auto end_character =
|
||||
data ? P::as_int64(P::find_member(*data, "endCharacter")) : std::nullopt;
|
||||
if (!name || !path || !line || !character || !end_line || !end_character)
|
||||
return Json{Json::Array{}};
|
||||
const features::call_hierarchy::Definition caller{
|
||||
*name,
|
||||
*path,
|
||||
{{static_cast<std::uint32_t>(*line), static_cast<std::uint32_t>(*character)},
|
||||
{static_cast<std::uint32_t>(*end_line), static_cast<std::uint32_t>(*end_character)}}};
|
||||
std::vector<features::call_hierarchy::Call> calls;
|
||||
std::vector<features::call_hierarchy::Definition> known;
|
||||
{
|
||||
std::lock_guard lock{s.mutex};
|
||||
for (const auto &[key, entry] : s.entries) {
|
||||
if (!entry.occurrences)
|
||||
continue;
|
||||
for (const auto &occurrence : *entry.occurrences) {
|
||||
features::call_hierarchy::Call call;
|
||||
call.callee = occurrence.name;
|
||||
call.path = occurrence.use_file->identity.canonical_path.native();
|
||||
call.range = {{occurrence.use.start.line, occurrence.use.start.character},
|
||||
{occurrence.use.end.line, occurrence.use.end.character}};
|
||||
if (occurrence.has_definition()) {
|
||||
features::call_hierarchy::Definition definition{
|
||||
occurrence.name,
|
||||
occurrence.definition_file->identity.canonical_path.native(),
|
||||
{{occurrence.definition.start.line, occurrence.definition.start.character},
|
||||
{occurrence.definition.end.line, occurrence.definition.end.character}}};
|
||||
call.callee_definition = definition;
|
||||
known.push_back(std::move(definition));
|
||||
}
|
||||
calls.push_back(std::move(call));
|
||||
}
|
||||
}
|
||||
}
|
||||
const auto grouped = features::call_hierarchy::outgoing(calls, known, caller);
|
||||
Json::Array result;
|
||||
for (const auto &callee : grouped) {
|
||||
Json::Array ranges;
|
||||
for (const auto &range : callee.from_ranges)
|
||||
ranges.push_back(range_json(Documents::Range{{range.start.line, range.start.character},
|
||||
{range.end.line, range.end.character}}));
|
||||
const auto span = range_json(
|
||||
Documents::Range{{callee.to.range.start.line, callee.to.range.start.character},
|
||||
{callee.to.range.end.line, callee.to.range.end.character}});
|
||||
Json::Object to{{"name", Json{callee.to.name}},
|
||||
{"kind", Json{std::int64_t{12}}},
|
||||
{"uri", Json{Documents::path_to_file_uri(callee.to.path)}},
|
||||
{"range", span},
|
||||
{"selectionRange", span}};
|
||||
result.push_back(Json{
|
||||
Json::Object{{"to", Json{std::move(to)}}, {"fromRanges", Json{std::move(ranges)}}}});
|
||||
}
|
||||
return Json{std::move(result)};
|
||||
}
|
||||
|
||||
Json EditorDiagnostics::hover(std::string_view uri, Documents::Position position) const {
|
||||
auto &s = *state_;
|
||||
auto key = Documents::client_uri_key(uri);
|
||||
|
||||
Reference in new issue
Block a user