Simplify hierarchies

This commit is contained in:
Fangrui Song 2018-02-25 19:10:02 -08:00
parent 95797be730
commit c166f3bca8
3 changed files with 33 additions and 42 deletions

View File

@ -83,7 +83,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy::Entry,
children);
MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy, jsonrpc, id, result);
void Expand(MessageHandler* m,
bool Expand(MessageHandler* m,
Out_CqueryCallHierarchy::Entry* entry,
bool callee,
CallType call_type,
@ -93,28 +93,16 @@ void Expand(MessageHandler* m,
const QueryFunc::Def* def = func.AnyDef();
entry->numChildren = 0;
if (!def)
return;
return false;
auto handle = [&](Use use, CallType call_type) {
entry->numChildren++;
if (levels > 0) {
QueryFunc& rel_func = m->db->GetFunc(use);
const QueryFunc::Def* rel_def = rel_func.AnyDef();
if (!rel_def)
return;
if (optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, use)) {
Out_CqueryCallHierarchy::Entry entry1;
entry1.id = QueryFuncId(use.id);
if (detailed_name)
entry1.name = rel_def->detailed_name;
else
entry1.name = rel_def->ShortName();
entry1.location = *loc;
entry1.callType = call_type;
Expand(m, &entry1, callee, call_type, detailed_name, levels - 1);
if (Expand(m, &entry1, callee, call_type, detailed_name, levels - 1))
entry->children.push_back(std::move(entry1));
}
}
};
auto handle_uses = [&](const QueryFunc& func, CallType call_type) {
if (callee) {
@ -131,6 +119,15 @@ void Expand(MessageHandler* m,
std::unordered_set<Usr> seen;
std::vector<const QueryFunc*> stack;
if (detailed_name)
entry->name = def->detailed_name;
else
entry->name = def->ShortName();
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, *def->spell))
entry->location = *loc;
}
handle_uses(func, CallType::Direct);
// Callers/callees of base functions.
@ -167,6 +164,7 @@ void Expand(MessageHandler* m,
});
}
}
return true;
}
struct CqueryCallHierarchyInitialHandler
@ -182,15 +180,7 @@ struct CqueryCallHierarchyInitialHandler
Out_CqueryCallHierarchy::Entry entry;
entry.id = root_id;
if (detailed_name)
entry.name = def->detailed_name;
else
entry.name = def->ShortName();
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(db, working_files, *def->spell))
entry.location = *loc;
}
entry.callType = CallType::Direct;
Expand(this, &entry, callee, call_type, detailed_name, levels);
return entry;
}
@ -231,7 +221,7 @@ struct CqueryCallHierarchyExpandHandler
if (params.id) {
Out_CqueryCallHierarchy::Entry entry;
entry.id = *params.id;
// entry.name is empty as it is known by the client.
entry.callType = CallType::Direct;
if (entry.id.id < db->funcs.size())
Expand(this, &entry, params.callee, params.callType,
params.detailedName, params.levels);

View File

@ -100,7 +100,6 @@ bool ExpandHelper(MessageHandler* m,
entry->location = *loc;
}
if (derived) {
entry->numChildren = int(entity.derived.size());
if (levels > 0) {
for (auto id : entity.derived) {
Out_CqueryInheritanceHierarchy::Entry entry1;
@ -110,9 +109,9 @@ bool ExpandHelper(MessageHandler* m,
entry->children.push_back(std::move(entry1));
}
entry->numChildren = int(entry->children.size());
}
} else
entry->numChildren = int(entity.derived.size());
} else {
entry->numChildren = int(def->bases.size());
if (levels > 0) {
for (auto id : def->bases) {
Out_CqueryInheritanceHierarchy::Entry entry1;
@ -122,7 +121,8 @@ bool ExpandHelper(MessageHandler* m,
entry->children.push_back(std::move(entry1));
}
entry->numChildren = int(entry->children.size());
}
} else
entry->numChildren = int(def->bases.size());
}
return true;
}

View File

@ -62,7 +62,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry,
children);
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result);
void Expand(MessageHandler* m,
bool Expand(MessageHandler* m,
Out_CqueryMemberHierarchy::Entry* entry,
bool detailed_name,
int levels) {
@ -100,13 +100,12 @@ void Expand(MessageHandler* m,
}
if (!def) {
entry->numChildren = 0;
return;
return false;
}
if (detailed_name)
entry->name = def->detailed_name;
else
entry->name = def->ShortName();
entry->numChildren = int(def->vars.size());
if (levels > 0) {
EachDefinedEntity(m->db->vars, def->vars, [&](QueryVar& var) {
const QueryVar::Def* def1 = var.AnyDef();
@ -123,11 +122,13 @@ void Expand(MessageHandler* m,
GetLsLocation(m->db, m->working_files, *def1->spell))
entry1.location = *loc;
}
Expand(m, &entry1, detailed_name, levels - 1);
if (Expand(m, &entry1, detailed_name, levels - 1))
entry->children.push_back(std::move(entry1));
});
entry->numChildren = int(entry->children.size());
}
} else
entry->numChildren = int(def->vars.size());
return true;
}
struct CqueryMemberHierarchyInitialHandler
@ -193,8 +194,8 @@ struct CqueryMemberHierarchyExpandHandler
Out_CqueryMemberHierarchy::Entry entry;
entry.id = *request->params.id;
// entry.name is empty as it is known by the client.
if (entry.id.id < db->types.size())
Expand(this, &entry, params.detailedName, params.levels);
if (entry.id.id < db->types.size() &&
Expand(this, &entry, params.detailedName, params.levels))
out.result = std::move(entry);
}