ccls/src/messages/ccls_inheritanceHierarchy.cc

215 lines
7.3 KiB
C++
Raw Normal View History

2018-08-21 05:27:52 +00:00
/* Copyright 2017-2018 ccls Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
2017-12-06 03:32:33 +00:00
#include "message_handler.h"
2018-05-28 00:50:02 +00:00
#include "pipeline.hh"
2018-08-04 06:01:01 +00:00
#include "query_utils.h"
2018-05-28 00:50:02 +00:00
using namespace ccls;
2017-12-06 03:32:33 +00:00
2018-08-04 06:01:01 +00:00
#include <queue>
#include <unordered_set>
namespace {
2018-03-31 03:16:33 +00:00
MethodType kMethodType = "$ccls/inheritanceHierarchy";
2018-03-31 03:16:33 +00:00
struct In_CclsInheritanceHierarchy : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
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.
lsTextDocumentIdentifier textDocument;
lsPosition position;
Usr usr;
std::string id;
SymbolKind kind = SymbolKind::Invalid;
// true: derived classes/functions; false: base classes/functions
bool derived = false;
bool qualified = true;
int levels = 1;
2018-08-04 06:01:01 +00:00
bool flat = false;
} params;
};
2018-08-04 06:01:01 +00:00
MAKE_REFLECT_STRUCT(In_CclsInheritanceHierarchy::Params, textDocument, position,
id, kind, derived, qualified, levels, flat);
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> {
struct Entry {
Usr usr;
std::string id;
SymbolKind kind;
std::string_view name;
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-08-09 17:08:14 +00:00
MAKE_REFLECT_STRUCT(Out_CclsInheritanceHierarchy::Entry, id, kind, name,
location, numChildren, children);
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsInheritanceHierarchy, jsonrpc,
id, result);
bool Expand(MessageHandler *m, Out_CclsInheritanceHierarchy::Entry *entry,
bool derived, bool qualified, int levels);
template <typename Q>
2018-08-09 17:08:14 +00:00
bool ExpandHelper(MessageHandler *m, Out_CclsInheritanceHierarchy::Entry *entry,
bool derived, bool qualified, int levels, Q &entity) {
const auto *def = entity.AnyDef();
if (def) {
entry->name = def->Name(qualified);
if (def->spell) {
if (auto loc = GetLsLocation(m->db, m->working_files, *def->spell))
entry->location = *loc;
} else if (entity.declarations.size()) {
if (auto loc = GetLsLocation(m->db, m->working_files, entity.declarations[0]))
entry->location = *loc;
}
} else if (!derived) {
entry->numChildren = 0;
return false;
2017-12-19 06:15:46 +00:00
}
std::unordered_set<Usr> seen;
if (derived) {
if (levels > 0) {
for (auto usr : entity.derived) {
2018-05-28 00:50:02 +00:00
if (!seen.insert(usr).second)
continue;
2018-03-31 03:16:33 +00:00
Out_CclsInheritanceHierarchy::Entry entry1;
entry1.id = std::to_string(usr);
entry1.usr = usr;
entry1.kind = entry->kind;
if (Expand(m, &entry1, derived, qualified, levels - 1))
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());
} else {
if (levels > 0) {
for (auto usr : def->bases) {
2018-05-28 00:50:02 +00:00
if (!seen.insert(usr).second)
continue;
2018-03-31 03:16:33 +00:00
Out_CclsInheritanceHierarchy::Entry entry1;
entry1.id = std::to_string(usr);
entry1.usr = usr;
entry1.kind = entry->kind;
if (Expand(m, &entry1, derived, qualified, levels - 1))
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());
}
return true;
2017-12-06 04:39:44 +00:00
}
2018-08-09 17:08:14 +00:00
bool Expand(MessageHandler *m, Out_CclsInheritanceHierarchy::Entry *entry,
bool derived, bool qualified, int levels) {
if (entry->kind == SymbolKind::Func)
return ExpandHelper(m, entry, derived, qualified, levels,
m->db->Func(entry->usr));
else
return ExpandHelper(m, entry, derived, qualified, levels,
m->db->Type(entry->usr));
}
2017-12-06 04:39:44 +00:00
2018-03-31 03:16:33 +00:00
struct Handler_CclsInheritanceHierarchy
: BaseMessageHandler<In_CclsInheritanceHierarchy> {
MethodType GetMethodType() const override { return kMethodType; }
2018-03-31 03:16:33 +00:00
std::optional<Out_CclsInheritanceHierarchy::Entry>
BuildInitial(SymbolRef sym, bool derived, bool qualified, int levels) {
2018-03-31 03:16:33 +00:00
Out_CclsInheritanceHierarchy::Entry entry;
entry.id = std::to_string(sym.usr);
entry.usr = sym.usr;
entry.kind = sym.kind;
Expand(this, &entry, derived, qualified, levels);
return entry;
2017-12-06 04:39:44 +00:00
}
2018-08-09 17:08:14 +00:00
void Run(In_CclsInheritanceHierarchy *request) override {
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-08-04 06:01:01 +00:00
if (!params.flat && params.id.size()) {
try {
params.usr = std::stoull(params.id);
} catch (...) {
return;
}
2018-03-31 03:16:33 +00:00
Out_CclsInheritanceHierarchy::Entry entry;
entry.id = std::to_string(params.usr);
entry.usr = params.usr;
entry.kind = params.kind;
if (((entry.kind == SymbolKind::Func && db->HasFunc(entry.usr)) ||
(entry.kind == SymbolKind::Type && db->HasType(entry.usr))) &&
Expand(this, &entry, params.derived, params.qualified, params.levels))
out.result = std::move(entry);
} else {
2018-08-09 17:08:14 +00:00
QueryFile *file;
if (!FindFileOrFail(db, project, request->id,
params.textDocument.uri.GetPath(), &file))
return;
2018-08-09 17:08:14 +00:00
WorkingFile *wfile = working_files->GetFileByFilename(file->def->path);
2018-05-28 00:50:02 +00:00
for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, params.position))
if (sym.kind == SymbolKind::Func || sym.kind == SymbolKind::Type) {
out.result = BuildInitial(sym, params.derived, params.qualified,
params.levels);
break;
}
}
2018-08-04 06:01:01 +00:00
if (!params.flat) {
pipeline::WriteStdout(kMethodType, out);
return;
}
Out_LocationList out1;
out1.id = request->id;
if (out.result) {
std::queue<Out_CclsInheritanceHierarchy::Entry *> q;
for (auto &entry1 : out.result->children)
q.push(&entry1);
while (q.size()) {
auto *entry = q.front();
q.pop();
if (entry->location.uri.raw_uri.size())
out1.result.push_back({entry->location});
for (auto &entry1 : entry->children)
q.push(&entry1);
}
std::sort(out1.result.begin(), out1.result.end());
out1.result.erase(std::unique(out1.result.begin(), out1.result.end()),
out1.result.end());
2018-08-04 06:01:01 +00:00
}
pipeline::WriteStdout(kMethodType, out1);
2017-12-06 03:32:33 +00:00
}
};
2018-03-31 03:16:33 +00:00
REGISTER_MESSAGE_HANDLER(Handler_CclsInheritanceHierarchy);
2018-08-09 17:08:14 +00:00
} // namespace