2018-01-18 07:14:29 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
|
|
|
#include "queue_manager.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct Ipc_CqueryMemberHierarchyInitial
|
2018-01-19 09:01:56 +00:00
|
|
|
: public RequestMessage<Ipc_CqueryMemberHierarchyInitial> {
|
2018-01-18 07:14:29 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryMemberHierarchyInitial;
|
2018-02-25 17:23:38 +00:00
|
|
|
struct Params {
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
|
|
|
int levels = 1;
|
|
|
|
};
|
|
|
|
Params params;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 17:23:38 +00:00
|
|
|
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial::Params,
|
|
|
|
textDocument,
|
|
|
|
position,
|
|
|
|
levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyInitial);
|
|
|
|
|
|
|
|
struct Ipc_CqueryMemberHierarchyExpand
|
2018-01-19 09:01:56 +00:00
|
|
|
: public RequestMessage<Ipc_CqueryMemberHierarchyExpand> {
|
2018-01-18 07:14:29 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryMemberHierarchyExpand;
|
|
|
|
struct Params {
|
2018-02-25 17:23:38 +00:00
|
|
|
Maybe<QueryTypeId> id;
|
|
|
|
int levels = 1;
|
2018-01-18 08:09:13 +00:00
|
|
|
};
|
|
|
|
Params params;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 17:23:38 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand::Params, id, levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyExpand);
|
|
|
|
|
|
|
|
struct Out_CqueryMemberHierarchy
|
|
|
|
: public lsOutMessage<Out_CqueryMemberHierarchy> {
|
|
|
|
struct Entry {
|
2018-02-25 17:23:38 +00:00
|
|
|
QueryTypeId id;
|
2018-01-31 08:35:04 +00:00
|
|
|
std::string_view name;
|
2018-02-25 22:53:57 +00:00
|
|
|
std::string_view field_name;
|
2018-01-18 07:14:29 +00:00
|
|
|
lsLocation location;
|
2018-02-25 22:53:57 +00:00
|
|
|
// For unexpanded nodes, this is an upper bound because some entities may be
|
|
|
|
// undefined. If it is 0, there are no members.
|
2018-02-25 17:23:38 +00:00
|
|
|
int numChildren;
|
|
|
|
// Empty if the |levels| limit is reached.
|
|
|
|
std::vector<Entry> children;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
|
|
|
lsRequestId id;
|
2018-02-25 17:23:38 +00:00
|
|
|
optional<Entry> result;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 17:23:38 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry,
|
|
|
|
id,
|
|
|
|
name,
|
2018-02-25 22:53:57 +00:00
|
|
|
field_name,
|
2018-02-25 17:23:38 +00:00
|
|
|
location,
|
|
|
|
numChildren,
|
|
|
|
children);
|
2018-01-18 07:14:29 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result);
|
|
|
|
|
2018-02-25 17:23:38 +00:00
|
|
|
void Expand(MessageHandler* m, Out_CqueryMemberHierarchy::Entry* entry, int levels) {
|
|
|
|
const QueryType::Def* def = m->db->types[entry->id.id].AnyDef();
|
|
|
|
if (!def) {
|
|
|
|
entry->numChildren = 0;
|
|
|
|
return;
|
|
|
|
}
|
2018-02-25 22:53:57 +00:00
|
|
|
entry->name = def->ShortName();
|
2018-02-25 17:23:38 +00:00
|
|
|
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.id = def1->type ? *def1->type : QueryTypeId();
|
2018-02-25 22:53:57 +00:00
|
|
|
entry1.field_name = def1->ShortName();
|
2018-02-25 17:23:38 +00:00
|
|
|
Expand(m, &entry1, levels - 1);
|
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
});
|
2018-02-25 22:53:57 +00:00
|
|
|
entry->numChildren = int(entry->children.size());
|
2018-02-25 17:23:38 +00:00
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 17:23:38 +00:00
|
|
|
struct CqueryMemberHierarchyInitialHandler
|
|
|
|
: 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 {};
|
2018-01-18 07:14:29 +00:00
|
|
|
|
|
|
|
Out_CqueryMemberHierarchy::Entry entry;
|
2018-02-25 17:23:38 +00:00
|
|
|
entry.id = root_id;
|
|
|
|
Expand(this, &entry, levels);
|
|
|
|
return entry;
|
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
|
|
|
|
void Run(Ipc_CqueryMemberHierarchyInitial* request) override {
|
|
|
|
QueryFile* file;
|
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
|
|
|
request->params.textDocument.uri.GetPath(), &file))
|
|
|
|
return;
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
Out_CqueryMemberHierarchy out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-02-09 17:42:10 +00:00
|
|
|
for (const SymbolRef& sym :
|
2018-01-30 00:27:43 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.position)) {
|
2018-02-09 17:42:10 +00:00
|
|
|
if (sym.kind == SymbolKind::Type) {
|
2018-02-25 17:23:38 +00:00
|
|
|
out.result = BuildInitial(QueryTypeId(sym.id), request->params.levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-09 17:42:10 +00:00
|
|
|
if (sym.kind == SymbolKind::Var) {
|
2018-02-18 05:52:14 +00:00
|
|
|
const QueryVar::Def* def = db->GetVar(sym).AnyDef();
|
|
|
|
if (def && def->type)
|
2018-02-25 17:23:38 +00:00
|
|
|
out.result = BuildInitial(QueryTypeId(*def->type), request->params.levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyInitial, out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryMemberHierarchyInitialHandler);
|
|
|
|
|
|
|
|
struct CqueryMemberHierarchyExpandHandler
|
|
|
|
: BaseMessageHandler<Ipc_CqueryMemberHierarchyExpand> {
|
|
|
|
void Run(Ipc_CqueryMemberHierarchyExpand* request) override {
|
|
|
|
Out_CqueryMemberHierarchy out;
|
|
|
|
out.id = request->id;
|
2018-02-25 17:23:38 +00:00
|
|
|
if (request->params.id) {
|
|
|
|
Out_CqueryMemberHierarchy::Entry entry;
|
|
|
|
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);
|
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
|
|
|
|
QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryMemberHierarchyExpandHandler);
|
|
|
|
} // namespace
|