Serve type hierarchy from observer class facts

Occurrences carry their result type uid, decoded types are retained per
editor entry, and a bounded core selects direct bases and inverse
subtypes. The server advertises typeHierarchyProvider and serves
prepareTypeHierarchy, supertypes and subtypes. Registered feature tests
cover the selection; a real stdio session prepared DerivedA and
returned Base as supertype and DerivedA/DerivedB as subtypes of Base.
This commit is contained in:
faumaray committed 2026-12-05 23:36:05 +03:00
1 parent b1c8a4a625
commit 86b9923532
14 files changed
+357 -15

No files matched your search

+22 -7
View File
@@ -332,6 +332,8 @@ DispatchResult Server::handle(std::string_view body) {
}
if (diagnostics_ && diagnostics_->supports_occurrences())
capabilities.emplace("callHierarchyProvider", Json{true});
if (diagnostics_ && diagnostics_->supports_occurrences())
capabilities.emplace("typeHierarchyProvider", Json{true});
if (diagnostics_ && diagnostics_->supports_occurrences()) {
capabilities.emplace("codeLensProvider",
Json{Json::Object{{"resolveProvider", Json{false}}}});
@@ -434,7 +436,8 @@ DispatchResult Server::handle(std::string_view body) {
return reply(id, "result", std::move(result));
}
if (*method == "textDocument/hover" || *method == "textDocument/definition" ||
*method == "textDocument/signatureHelp" || *method == "textDocument/documentHighlight") {
*method == "textDocument/signatureHelp" || *method == "textDocument/documentHighlight" ||
*method == "textDocument/prepareTypeHierarchy") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())
return error(id, -32601, "Method not found");
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
@@ -451,12 +454,13 @@ DispatchResult Server::handle(std::string_view body) {
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)};
auto result = *method == "textDocument/hover" ? diagnostics_->hover(*uri, selected)
: *method == "textDocument/definition"
? diagnostics_->definition(*uri, selected)
: *method == "textDocument/signatureHelp"
? diagnostics_->signature_help(*uri, selected)
: diagnostics_->document_highlights(*uri, selected);
auto result =
*method == "textDocument/hover" ? diagnostics_->hover(*uri, selected)
: *method == "textDocument/definition" ? diagnostics_->definition(*uri, selected)
: *method == "textDocument/signatureHelp" ? diagnostics_->signature_help(*uri, selected)
: *method == "textDocument/documentHighlight"
? diagnostics_->document_highlights(*uri, selected)
: diagnostics_->prepare_type_hierarchy(*uri, selected);
return reply(id, "result", std::move(result));
}
if (*method == "completionItem/resolve") {
@@ -595,6 +599,17 @@ DispatchResult Server::handle(std::string_view body) {
*uri, {static_cast<std::uint32_t>(*line_value),
static_cast<std::uint32_t>(*character_value)}));
}
if (*method == "typeHierarchy/supertypes" || *method == "typeHierarchy/subtypes") {
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 type hierarchy item");
return reply(id, "result",
*method == "typeHierarchy/supertypes"
? diagnostics_->type_hierarchy_supertypes(*item)
: diagnostics_->type_hierarchy_subtypes(*item));
}
if (*method == "callHierarchy/outgoingCalls") {
if (!diagnostics_ || !diagnostics_->supports_occurrences())
return error(id, -32601, "Method not found");
+47
View File
@@ -0,0 +1,47 @@
#include <vls/features/TypeHierarchy.hpp>
#include <set>
#include <tuple>
namespace vls::features::type_hierarchy {
namespace {
using Key = std::tuple<std::string, std::string, std::uint32_t, std::uint32_t>;
Key key_of(const TypeRef &type) {
return {type.name, type.path, type.range.start.line, type.range.start.character};
}
void push_unique(std::vector<TypeRef> &out, std::set<Key> &seen, const TypeRef &type,
const Limits &limits) {
if (out.size() == limits.items)
return;
if (seen.insert(key_of(type)).second)
out.push_back(type);
}
} // namespace
std::vector<TypeRef> supertypes(std::span<const Type> types, const TypeRef &target,
const Limits &limits) {
std::vector<TypeRef> result;
std::set<Key> seen;
for (const auto &type : types) {
if (key_of(type.self) != key_of(target))
continue;
for (const auto &base : type.bases)
push_unique(result, seen, base, limits);
}
return result;
}
std::vector<TypeRef> subtypes(std::span<const Type> types, const TypeRef &target,
const Limits &limits) {
std::vector<TypeRef> result;
std::set<Key> seen;
for (const auto &type : types)
for (const auto &base : type.bases)
if (key_of(base) == key_of(target))
push_unique(result, seen, type.self, limits);
return result;
}
} // namespace vls::features::type_hierarchy
+136
View File
@@ -11,6 +11,7 @@
#include <vls/features/SemanticTokens.hpp>
#include <vls/features/SignatureHelp.hpp>
#include <vls/features/TextStructure.hpp>
#include <vls/features/TypeHierarchy.hpp>
#include <vls/index/SymbolIndex.hpp>
#include <vls/modules/ArtifactCache.hpp>
#include <vls/modules/GccArtifacts.hpp>
@@ -171,6 +172,7 @@ struct Entry {
std::shared_ptr<const Receipt> receipt;
std::vector<PublishedDiagnostic> published;
std::shared_ptr<const std::vector<Occurrence>> occurrences;
std::shared_ptr<const std::vector<ReflectedType>> types;
std::string occurrence_state{"disabled"}, occurrence_reason;
std::string module_state{"disabled"}, module_detail;
std::string module_artifact_state{"disabled"}, module_artifact_detail,
@@ -365,6 +367,8 @@ struct EditorDiagnostics::State {
entry.other_messages = std::move(report.other_messages);
entry.receipt = std::move(receipt);
if (result->occurrences) {
entry.types = std::make_shared<const std::vector<ReflectedType>>(
std::move(result->occurrences->types));
entry.occurrences = std::make_shared<const std::vector<Occurrence>>(
std::move(result->occurrences->occurrences));
entry.occurrence_state = !result->occurrences->complete
@@ -1682,6 +1686,138 @@ Json EditorDiagnostics::call_hierarchy_outgoing(const Json &item) const {
return Json{std::move(result)};
}
namespace {
Json hierarchy_item(const features::type_hierarchy::TypeRef &type) {
const auto range =
range_json(Documents::Range{{type.range.start.line, type.range.start.character},
{type.range.end.line, type.range.end.character}});
Json::Object data{{"uid", Json{static_cast<std::int64_t>(type.uid)}},
{"name", Json{type.name}},
{"path", Json{type.path}},
{"line", Json{static_cast<std::int64_t>(type.range.start.line)}},
{"character", Json{static_cast<std::int64_t>(type.range.start.character)}},
{"endLine", Json{static_cast<std::int64_t>(type.range.end.line)}},
{"endCharacter", Json{static_cast<std::int64_t>(type.range.end.character)}}};
Json::Object item{{"name", Json{type.name}},
{"kind", Json{std::int64_t{5}}},
{"uri", Json{Documents::path_to_file_uri(type.path)}},
{"range", range},
{"selectionRange", range},
{"data", Json{std::move(data)}}};
return Json{std::move(item)};
}
std::optional<features::type_hierarchy::TypeRef> hierarchy_target(const Json &item) {
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;
const auto uid = data ? P::as_int64(P::find_member(*data, "uid")) : std::nullopt;
if (!name || !path || !line || !character || !end_line || !end_character || !uid)
return std::nullopt;
return features::type_hierarchy::TypeRef{
static_cast<std::uint32_t>(*uid),
*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::type_hierarchy::Type>
hierarchy_types(const std::shared_ptr<const std::vector<ReflectedType>> &types) {
std::vector<features::type_hierarchy::Type> result;
if (!types)
return result;
for (const auto &type : *types) {
features::type_hierarchy::Type entry;
entry.self = {type.self.uid,
type.self.name,
type.self.path,
{{type.self.range.start.line, type.self.range.start.character},
{type.self.range.end.line, type.self.range.end.character}}};
for (const auto &base : type.bases)
entry.bases.push_back({base.uid,
base.name,
base.path,
{{base.range.start.line, base.range.start.character},
{base.range.end.line, base.range.end.character}}});
result.push_back(std::move(entry));
}
return result;
}
} // namespace
Json EditorDiagnostics::prepare_type_hierarchy(std::string_view uri,
Documents::Position position) const {
auto &s = *state_;
auto key = Documents::client_uri_key(uri);
std::shared_ptr<const Receipt> receipt;
std::shared_ptr<const std::vector<Occurrence>> facts;
std::shared_ptr<const std::vector<ReflectedType>> types;
{
std::lock_guard lock{s.mutex};
const auto i = key ? s.entries.find(*key) : s.entries.end();
if (i == s.entries.end())
return Json{Json::Array{}};
receipt = i->second.receipt;
facts = i->second.occurrences;
types = i->second.types;
}
if (!receipt || !facts || !types || !s.valid(*receipt))
return Json{Json::Array{}};
const auto *match = covering(*receipt, *facts, *key, position);
if (!match || !match->result_type_uid)
return Json{Json::Array{}};
const auto found = std::ranges::find_if(
*types, [&](const ReflectedType &type) { return type.self.uid == match->result_type_uid; });
if (found == types->end())
return Json{Json::Array{}};
const auto target =
hierarchy_types(types)[static_cast<std::size_t>(found - types->begin())].self;
return Json{Json::Array{hierarchy_item(target)}};
}
Json EditorDiagnostics::type_hierarchy_supertypes(const Json &item) const {
auto &s = *state_;
const auto target = hierarchy_target(item);
if (!target)
return Json{Json::Array{}};
std::vector<features::type_hierarchy::Type> types;
{
std::lock_guard lock{s.mutex};
for (const auto &[key, entry] : s.entries)
for (const auto &type : hierarchy_types(entry.types))
types.push_back(type);
}
Json::Array result;
for (const auto &type : features::type_hierarchy::supertypes(types, *target))
result.push_back(hierarchy_item(type));
return Json{std::move(result)};
}
Json EditorDiagnostics::type_hierarchy_subtypes(const Json &item) const {
auto &s = *state_;
const auto target = hierarchy_target(item);
if (!target)
return Json{Json::Array{}};
std::vector<features::type_hierarchy::Type> types;
{
std::lock_guard lock{s.mutex};
for (const auto &[key, entry] : s.entries)
for (const auto &type : hierarchy_types(entry.types))
types.push_back(type);
}
Json::Array result;
for (const auto &type : features::type_hierarchy::subtypes(types, *target))
result.push_back(hierarchy_item(type));
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);
+12 -4
View File
@@ -217,8 +217,9 @@ decode_occurrences(std::string_view report, std::string_view nonce,
return std::unexpected{OccurrenceError::invalid_report};
const auto read = [&](const Json *value) -> std::optional<TypeRef> {
const auto *fields = value ? P::as_object(value) : nullptr;
if (!fields || fields->size() != 6 || !integer(P::find_member(*fields, "uid")) ||
!P::as_string(P::find_member(*fields, "name")))
const auto uid = integer(P::find_member(*fields, "uid"));
const auto *type_name = P::as_string(P::find_member(*fields, "name"));
if (!fields || fields->size() != 6 || !uid || !type_name)
return std::nullopt;
Json::Object trimmed;
for (const auto *key : {"path", "line", "startByte", "endByte"}) {
@@ -234,7 +235,8 @@ decode_occurrences(std::string_view report, std::string_view nonce,
auto bound = bind_span(*raw, {}, capture);
if (!bound)
return std::nullopt;
return TypeRef{std::string{raw->path}, raw->path, bound->second};
return TypeRef{*type_name, raw->path, bound->second,
static_cast<std::uint32_t>(*uid)};
};
auto parsed_self = read(self);
if (!parsed_self)
@@ -251,7 +253,7 @@ decode_occurrences(std::string_view report, std::string_view nonce,
std::set<std::tuple<const void *, std::uint32_t, std::uint32_t>> uses;
for (const auto &item : *items) {
const auto *fields = P::as_object(&item);
if (!fields || (fields->size() != 5 && fields->size() != 6))
if (!fields || (fields->size() != 5 && fields->size() != 6 && fields->size() != 7))
return std::unexpected{OccurrenceError::invalid_report};
const auto *name = P::as_string(P::find_member(*fields, "name"));
const auto *type = P::as_string(P::find_member(*fields, "type"));
@@ -271,6 +273,12 @@ decode_occurrences(std::string_view report, std::string_view nonce,
.second)
return std::unexpected{OccurrenceError::invalid_report};
Occurrence occurrence;
if (const auto *result_type = P::find_member(*fields, "resultTypeUid")) {
const auto uid = integer(result_type);
if (!uid || *uid < 0 || *uid > std::numeric_limits<std::uint32_t>::max())
return std::unexpected{OccurrenceError::invalid_report};
occurrence.result_type_uid = static_cast<std::uint32_t>(*uid);
}
occurrence.name = *name;
occurrence.type = *type;
occurrence.canonical_type = *canonical;