2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-31 03:16:33 +00:00
|
|
|
MethodType kMethodType = "$ccls/inheritanceHierarchy";
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
struct In_CclsInheritanceHierarchy : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2018-02-26 02:45:46 +00:00
|
|
|
struct Params {
|
2018-03-20 02:51:42 +00:00
|
|
|
// If id+kind are specified, expand a node; otherwise textDocument+position
|
|
|
|
// should be specified for building the root and |levels| of nodes below.
|
2018-02-26 02:45:46 +00:00
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-02-28 05:56:40 +00:00
|
|
|
SymbolKind kind = SymbolKind::Invalid;
|
|
|
|
|
2018-02-26 02:45:46 +00:00
|
|
|
// true: derived classes/functions; false: base classes/functions
|
|
|
|
bool derived = false;
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified = true;
|
2018-02-26 02:45:46 +00:00
|
|
|
int levels = 1;
|
|
|
|
};
|
|
|
|
Params params;
|
|
|
|
};
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsInheritanceHierarchy::Params,
|
2018-02-26 02:45:46 +00:00
|
|
|
textDocument,
|
|
|
|
position,
|
|
|
|
id,
|
|
|
|
kind,
|
|
|
|
derived,
|
2018-04-06 00:00:07 +00:00
|
|
|
qualified,
|
2018-02-26 02:45:46 +00:00
|
|
|
levels);
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsInheritanceHierarchy, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_CclsInheritanceHierarchy);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
struct Out_CclsInheritanceHierarchy
|
|
|
|
: public lsOutMessage<Out_CclsInheritanceHierarchy> {
|
2018-02-26 02:45:46 +00:00
|
|
|
struct Entry {
|
2018-04-30 04:49:03 +00:00
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-02-26 02:45:46 +00:00
|
|
|
SymbolKind kind;
|
2018-02-11 05:36:15 +00:00
|
|
|
std::string_view name;
|
2018-02-26 02:45:46 +00:00
|
|
|
lsLocation location;
|
|
|
|
// For unexpanded nodes, this is an upper bound because some entities may be
|
|
|
|
// undefined. If it is 0, there are no members.
|
|
|
|
int numChildren;
|
|
|
|
// Empty if the |levels| limit is reached.
|
|
|
|
std::vector<Entry> children;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
lsRequestId id;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Entry> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CclsInheritanceHierarchy::Entry,
|
2018-02-26 02:45:46 +00:00
|
|
|
id,
|
|
|
|
kind,
|
2017-12-06 04:39:44 +00:00
|
|
|
name,
|
|
|
|
location,
|
2018-02-26 02:45:46 +00:00
|
|
|
numChildren,
|
2017-12-06 04:39:44 +00:00
|
|
|
children);
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CclsInheritanceHierarchy, jsonrpc, id, result);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-02-26 02:45:46 +00:00
|
|
|
bool Expand(MessageHandler* m,
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry* entry,
|
2018-02-26 02:45:46 +00:00
|
|
|
bool derived,
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified,
|
2018-02-26 02:45:46 +00:00
|
|
|
int levels);
|
|
|
|
|
|
|
|
template <typename Q>
|
|
|
|
bool ExpandHelper(MessageHandler* m,
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry* entry,
|
2018-02-26 02:45:46 +00:00
|
|
|
bool derived,
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified,
|
2018-02-26 02:45:46 +00:00
|
|
|
int levels,
|
|
|
|
Q& entity) {
|
|
|
|
const auto* def = entity.AnyDef();
|
|
|
|
if (!def) {
|
|
|
|
entry->numChildren = 0;
|
|
|
|
return false;
|
2017-12-19 06:15:46 +00:00
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
entry->name = def->Name(qualified);
|
2018-02-26 02:45:46 +00:00
|
|
|
if (def->spell) {
|
2018-03-31 03:16:33 +00:00
|
|
|
if (std::optional<lsLocation> loc =
|
2018-02-26 02:45:46 +00:00
|
|
|
GetLsLocation(m->db, m->working_files, *def->spell))
|
|
|
|
entry->location = *loc;
|
|
|
|
}
|
|
|
|
if (derived) {
|
|
|
|
if (levels > 0) {
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto usr : entity.derived) {
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry entry1;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = std::to_string(usr);
|
|
|
|
entry1.usr = usr;
|
2018-02-26 02:45:46 +00:00
|
|
|
entry1.kind = entry->kind;
|
2018-04-06 00:00:07 +00:00
|
|
|
if (Expand(m, &entry1, derived, qualified, levels - 1))
|
2018-02-26 02:45:46 +00:00
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
entry->numChildren = int(entry->children.size());
|
2018-02-26 03:10:02 +00:00
|
|
|
} else
|
|
|
|
entry->numChildren = int(entity.derived.size());
|
2018-02-26 02:45:46 +00:00
|
|
|
} else {
|
|
|
|
if (levels > 0) {
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto usr : def->bases) {
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry entry1;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = std::to_string(usr);
|
|
|
|
entry1.usr = usr;
|
2018-02-26 02:45:46 +00:00
|
|
|
entry1.kind = entry->kind;
|
2018-04-06 00:00:07 +00:00
|
|
|
if (Expand(m, &entry1, derived, qualified, levels - 1))
|
2018-02-26 02:45:46 +00:00
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
entry->numChildren = int(entry->children.size());
|
2018-02-26 03:10:02 +00:00
|
|
|
} else
|
|
|
|
entry->numChildren = int(def->bases.size());
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
2017-12-06 04:39:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 02:45:46 +00:00
|
|
|
bool Expand(MessageHandler* m,
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry* entry,
|
2018-02-26 02:45:46 +00:00
|
|
|
bool derived,
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified,
|
2018-02-26 02:45:46 +00:00
|
|
|
int levels) {
|
|
|
|
if (entry->kind == SymbolKind::Func)
|
2018-04-06 00:00:07 +00:00
|
|
|
return ExpandHelper(m, entry, derived, qualified, levels,
|
2018-04-30 04:49:03 +00:00
|
|
|
m->db->Func(entry->usr));
|
2018-02-26 02:45:46 +00:00
|
|
|
else
|
2018-04-06 00:00:07 +00:00
|
|
|
return ExpandHelper(m, entry, derived, qualified, levels,
|
2018-04-30 04:49:03 +00:00
|
|
|
m->db->Type(entry->usr));
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
struct Handler_CclsInheritanceHierarchy
|
|
|
|
: BaseMessageHandler<In_CclsInheritanceHierarchy> {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Out_CclsInheritanceHierarchy::Entry>
|
2018-04-06 00:00:07 +00:00
|
|
|
BuildInitial(SymbolRef sym, bool derived, bool qualified, int levels) {
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry entry;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry.id = std::to_string(sym.usr);
|
|
|
|
entry.usr = sym.usr;
|
2018-02-26 02:45:46 +00:00
|
|
|
entry.kind = sym.kind;
|
2018-04-06 00:00:07 +00:00
|
|
|
Expand(this, &entry, derived, qualified, levels);
|
2018-02-26 02:45:46 +00:00
|
|
|
return entry;
|
2017-12-06 04:39:44 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
void Run(In_CclsInheritanceHierarchy* request) override {
|
2018-04-08 17:03:50 +00:00
|
|
|
auto& params = request->params;
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy out;
|
2017-12-06 03:32:33 +00:00
|
|
|
out.id = request->id;
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
if (params.id.size()) {
|
|
|
|
try {
|
|
|
|
params.usr = std::stoull(params.id);
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsInheritanceHierarchy::Entry entry;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry.id = std::to_string(params.usr);
|
|
|
|
entry.usr = params.usr;
|
2018-02-26 02:45:46 +00:00
|
|
|
entry.kind = params.kind;
|
2018-04-30 04:49:03 +00:00
|
|
|
if (((entry.kind == SymbolKind::Func && db->usr2func.count(entry.usr)) ||
|
|
|
|
(entry.kind == SymbolKind::Type && db->usr2type.count(entry.usr))) &&
|
|
|
|
Expand(this, &entry, params.derived, params.qualified, params.levels))
|
2018-02-26 02:45:46 +00:00
|
|
|
out.result = std::move(entry);
|
2018-02-28 05:56:40 +00:00
|
|
|
} else {
|
|
|
|
QueryFile* file;
|
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
|
|
|
params.textDocument.uri.GetPath(), &file))
|
|
|
|
return;
|
2018-04-08 17:03:50 +00:00
|
|
|
WorkingFile* wfile =
|
2018-02-28 05:56:40 +00:00
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
2018-04-08 17:03:50 +00:00
|
|
|
for (SymbolRef sym :
|
|
|
|
FindSymbolsAtLocation(wfile, file, params.position)) {
|
2018-02-28 05:56:40 +00:00
|
|
|
if (sym.kind == SymbolKind::Func || sym.kind == SymbolKind::Type) {
|
2018-04-06 00:00:07 +00:00
|
|
|
out.result = BuildInitial(sym, params.derived, params.qualified,
|
2018-02-28 05:56:40 +00:00
|
|
|
params.levels);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
QueueManager::WriteStdout(kMethodType, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-31 03:16:33 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_CclsInheritanceHierarchy);
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|