Re-design $cquery/memberHierarchy{Initial,Expand}

This commit is contained in:
Fangrui Song 2018-02-25 09:23:38 -08:00
parent 8a427dfa9e
commit cd3da90152

View File

@ -6,8 +6,18 @@ namespace {
struct Ipc_CqueryMemberHierarchyInitial struct Ipc_CqueryMemberHierarchyInitial
: public RequestMessage<Ipc_CqueryMemberHierarchyInitial> { : public RequestMessage<Ipc_CqueryMemberHierarchyInitial> {
const static IpcId kIpcId = IpcId::CqueryMemberHierarchyInitial; const static IpcId kIpcId = IpcId::CqueryMemberHierarchyInitial;
lsTextDocumentPositionParams params; struct Params {
lsTextDocumentIdentifier textDocument;
lsPosition position;
int levels = 1;
};
Params params;
}; };
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial::Params,
textDocument,
position,
levels);
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial, id, params); MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial, id, params);
REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyInitial); REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyInitial);
@ -15,70 +25,75 @@ struct Ipc_CqueryMemberHierarchyExpand
: public RequestMessage<Ipc_CqueryMemberHierarchyExpand> { : public RequestMessage<Ipc_CqueryMemberHierarchyExpand> {
const static IpcId kIpcId = IpcId::CqueryMemberHierarchyExpand; const static IpcId kIpcId = IpcId::CqueryMemberHierarchyExpand;
struct Params { struct Params {
Maybe<QueryTypeId> type_id; Maybe<QueryTypeId> id;
int levels = 1;
}; };
Params params; Params params;
}; };
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand::Params, type_id); MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand::Params, id, levels);
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand, id, params); MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand, id, params);
REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyExpand); REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyExpand);
struct Out_CqueryMemberHierarchy struct Out_CqueryMemberHierarchy
: public lsOutMessage<Out_CqueryMemberHierarchy> { : public lsOutMessage<Out_CqueryMemberHierarchy> {
struct Entry { struct Entry {
QueryTypeId id;
std::string_view name; std::string_view name;
QueryTypeId type_id;
lsLocation location; lsLocation location;
int numChildren;
// Empty if the |levels| limit is reached.
std::vector<Entry> children;
}; };
lsRequestId id; lsRequestId id;
std::vector<Entry> result; optional<Entry> result;
}; };
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry, name, type_id, location); MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry,
id,
name,
location,
numChildren,
children);
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result);
std::vector<Out_CqueryMemberHierarchy::Entry> void Expand(MessageHandler* m, Out_CqueryMemberHierarchy::Entry* entry, int levels) {
BuildInitial(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) { const QueryType::Def* def = m->db->types[entry->id.id].AnyDef();
const auto* root_type = db->types[root.id].AnyDef(); if (!def) {
if (!root_type || !root_type->spell) entry->numChildren = 0;
return {}; return;
optional<lsLocation> def_loc =
GetLsLocation(db, working_files, *root_type->spell);
if (!def_loc)
return {};
Out_CqueryMemberHierarchy::Entry entry;
entry.type_id = root;
entry.name = root_type->ShortName();
entry.location = *def_loc;
return {entry};
}
std::vector<Out_CqueryMemberHierarchy::Entry>
ExpandNode(QueryDatabase* db, WorkingFiles* working_files, QueryTypeId root) {
QueryType& root_type = db->types[root.id];
const QueryType::Def* def = root_type.AnyDef();
if (!def)
return {};
std::vector<Out_CqueryMemberHierarchy::Entry> ret;
EachDefinedEntity(db->vars, def->vars, [&](QueryVar& var) {
const QueryVar::Def* def1 = var.AnyDef();
Out_CqueryMemberHierarchy::Entry entry;
entry.name = def1->ShortName();
entry.type_id = def1->type ? *def1->type : QueryTypeId();
if (def->spell) {
optional<lsLocation> loc = GetLsLocation(db, working_files, *def1->spell);
// TODO invalid location
if (loc)
entry.location = *loc;
} }
ret.push_back(std::move(entry)); if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, *def->spell))
entry->location = *loc;
}
entry->numChildren = int(def->vars.size());
if (levels > 0) {
EachDefinedEntity(m->db->vars, def->vars, [&](QueryVar& var) {
const QueryVar::Def* def1 = var.AnyDef();
Out_CqueryMemberHierarchy::Entry entry1;
entry1.name = def1->ShortName();
entry1.id = def1->type ? *def1->type : QueryTypeId();
Expand(m, &entry1, levels - 1);
entry->children.push_back(std::move(entry1));
}); });
return ret; }
} }
struct CqueryMemberHierarchyInitialHandler struct CqueryMemberHierarchyInitialHandler
: BaseMessageHandler<Ipc_CqueryMemberHierarchyInitial> { : BaseMessageHandler<Ipc_CqueryMemberHierarchyInitial> {
optional<Out_CqueryMemberHierarchy::Entry> BuildInitial(QueryTypeId root_id,
int levels) {
const auto* def = db->types[root_id.id].AnyDef();
if (!def)
return {};
Out_CqueryMemberHierarchy::Entry entry;
entry.id = root_id;
entry.name = def->ShortName();
Expand(this, &entry, levels);
return entry;
}
void Run(Ipc_CqueryMemberHierarchyInitial* request) override { void Run(Ipc_CqueryMemberHierarchyInitial* request) override {
QueryFile* file; QueryFile* file;
if (!FindFileOrFail(db, project, request->id, if (!FindFileOrFail(db, project, request->id,
@ -93,13 +108,13 @@ struct CqueryMemberHierarchyInitialHandler
for (const SymbolRef& sym : for (const SymbolRef& sym :
FindSymbolsAtLocation(working_file, file, request->params.position)) { FindSymbolsAtLocation(working_file, file, request->params.position)) {
if (sym.kind == SymbolKind::Type) { if (sym.kind == SymbolKind::Type) {
out.result = BuildInitial(db, working_files, QueryTypeId(sym.id)); out.result = BuildInitial(QueryTypeId(sym.id), request->params.levels);
break; break;
} }
if (sym.kind == SymbolKind::Var) { if (sym.kind == SymbolKind::Var) {
const QueryVar::Def* def = db->GetVar(sym).AnyDef(); const QueryVar::Def* def = db->GetVar(sym).AnyDef();
if (def && def->type) if (def && def->type)
out.result = BuildInitial(db, working_files, *def->type); out.result = BuildInitial(QueryTypeId(*def->type), request->params.levels);
break; break;
} }
} }
@ -114,9 +129,14 @@ struct CqueryMemberHierarchyExpandHandler
void Run(Ipc_CqueryMemberHierarchyExpand* request) override { void Run(Ipc_CqueryMemberHierarchyExpand* request) override {
Out_CqueryMemberHierarchy out; Out_CqueryMemberHierarchy out;
out.id = request->id; out.id = request->id;
// |ExpandNode| uses -1 to indicate invalid |type_id|. if (request->params.id) {
if (request->params.type_id) Out_CqueryMemberHierarchy::Entry entry;
out.result = ExpandNode(db, working_files, *request->params.type_id); entry.id = *request->params.id;
// entry.name is empty and it is known by the client.
if (entry.id.id < db->types.size())
Expand(this, &entry, request->params.levels);
out.result = std::move(entry);
}
QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out); QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out);
} }