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:
1 parent
b1c8a4a625
commit
86b9923532
14 files changed
+357
-15
No files matched your search
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user