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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-09-13 07:18:37 +00:00
|
|
|
#include "hierarchy.hh"
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "message_handler.hh"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2018-11-27 06:29:28 +00:00
|
|
|
#include "query.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-05-26 05:48:58 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-10-28 17:49:31 +00:00
|
|
|
struct Param : TextDocumentPositionParam {
|
|
|
|
// If id+kind are specified, expand a node; otherwise textDocument+position
|
|
|
|
// should be specified for building the root and |levels| of nodes below.
|
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-11-03 20:52:43 +00:00
|
|
|
Kind kind = Kind::Invalid;
|
2018-10-28 17:49:31 +00:00
|
|
|
|
|
|
|
// true: derived classes/functions; false: base classes/functions
|
|
|
|
bool derived = false;
|
|
|
|
bool qualified = true;
|
|
|
|
int levels = 1;
|
|
|
|
bool hierarchy = false;
|
2018-02-26 02:45:46 +00:00
|
|
|
};
|
|
|
|
|
2018-12-02 23:53:33 +00:00
|
|
|
REFLECT_STRUCT(Param, textDocument, position, id, kind, derived, qualified,
|
|
|
|
levels, hierarchy);
|
2019-01-09 07:19:17 +00:00
|
|
|
|
|
|
|
struct Out_cclsInheritance {
|
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-11-03 20:52:43 +00:00
|
|
|
Kind kind;
|
2019-01-09 07:19:17 +00:00
|
|
|
std::string_view name;
|
2018-11-03 20:52:43 +00:00
|
|
|
Location location;
|
2019-01-09 07:19:17 +00:00
|
|
|
// 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<Out_cclsInheritance> children;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-12-02 23:53:33 +00:00
|
|
|
REFLECT_STRUCT(Out_cclsInheritance, id, kind, name, location, numChildren,
|
|
|
|
children);
|
2018-08-09 17:08:14 +00:00
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
bool Expand(MessageHandler *m, Out_cclsInheritance *entry, bool derived,
|
|
|
|
bool qualified, int levels);
|
2018-02-26 02:45:46 +00:00
|
|
|
|
|
|
|
template <typename Q>
|
2019-01-09 07:19:17 +00:00
|
|
|
bool ExpandHelper(MessageHandler *m, Out_cclsInheritance *entry, bool derived,
|
|
|
|
bool qualified, int levels, Q &entity) {
|
2018-08-09 17:08:14 +00:00
|
|
|
const auto *def = entity.AnyDef();
|
2018-05-26 05:48:58 +00:00
|
|
|
if (def) {
|
|
|
|
entry->name = def->Name(qualified);
|
|
|
|
if (def->spell) {
|
2018-10-29 04:21:21 +00:00
|
|
|
if (auto loc = GetLsLocation(m->db, m->wfiles, *def->spell))
|
2018-05-26 05:48:58 +00:00
|
|
|
entry->location = *loc;
|
2018-09-12 20:46:20 +00:00
|
|
|
} else if (entity.declarations.size()) {
|
2018-10-29 04:21:21 +00:00
|
|
|
if (auto loc = GetLsLocation(m->db, m->wfiles, entity.declarations[0]))
|
2018-09-12 20:46:20 +00:00
|
|
|
entry->location = *loc;
|
2018-05-26 05:48:58 +00:00
|
|
|
}
|
|
|
|
} else if (!derived) {
|
2018-02-26 02:45:46 +00:00
|
|
|
entry->numChildren = 0;
|
|
|
|
return false;
|
2017-12-19 06:15:46 +00:00
|
|
|
}
|
2018-05-26 05:48:58 +00:00
|
|
|
std::unordered_set<Usr> seen;
|
2018-02-26 02:45:46 +00:00
|
|
|
if (derived) {
|
|
|
|
if (levels > 0) {
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto usr : entity.derived) {
|
2018-05-28 00:50:02 +00:00
|
|
|
if (!seen.insert(usr).second)
|
2018-05-26 05:48:58 +00:00
|
|
|
continue;
|
2019-01-09 07:19:17 +00:00
|
|
|
Out_cclsInheritance 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-05-28 00:50:02 +00:00
|
|
|
if (!seen.insert(usr).second)
|
2018-05-26 05:48:58 +00:00
|
|
|
continue;
|
2019-01-09 07:19:17 +00:00
|
|
|
Out_cclsInheritance 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
|
|
|
}
|
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
bool Expand(MessageHandler *m, Out_cclsInheritance *entry, bool derived,
|
|
|
|
bool qualified, int levels) {
|
2018-11-03 20:52:43 +00:00
|
|
|
if (entry->kind == Kind::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-10-28 17:49:31 +00:00
|
|
|
std::optional<Out_cclsInheritance> BuildInitial(MessageHandler *m, SymbolRef sym, bool derived,
|
|
|
|
bool qualified, int levels) {
|
|
|
|
Out_cclsInheritance entry;
|
|
|
|
entry.id = std::to_string(sym.usr);
|
|
|
|
entry.usr = sym.usr;
|
|
|
|
entry.kind = sym.kind;
|
|
|
|
Expand(m, &entry, derived, qualified, levels);
|
|
|
|
return entry;
|
|
|
|
}
|
2018-02-26 02:45:46 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
void Inheritance(MessageHandler *m, Param ¶m, ReplyOnce &reply) {
|
|
|
|
std::optional<Out_cclsInheritance> result;
|
|
|
|
if (param.id.size()) {
|
|
|
|
try {
|
|
|
|
param.usr = std::stoull(param.id);
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
2018-08-04 06:01:01 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
result.emplace();
|
|
|
|
result->id = std::to_string(param.usr);
|
|
|
|
result->usr = param.usr;
|
|
|
|
result->kind = param.kind;
|
2018-11-03 20:52:43 +00:00
|
|
|
if (!(((param.kind == Kind::Func && m->db->HasFunc(param.usr)) ||
|
|
|
|
(param.kind == Kind::Type && m->db->HasType(param.usr))) &&
|
|
|
|
Expand(m, &*result, param.derived, param.qualified, param.levels)))
|
2018-10-28 17:49:31 +00:00
|
|
|
result.reset();
|
|
|
|
} else {
|
2018-12-02 00:55:51 +00:00
|
|
|
QueryFile *file = m->FindFile(param.textDocument.uri.GetPath());
|
2018-10-28 17:49:31 +00:00
|
|
|
if (!file)
|
|
|
|
return;
|
2018-12-01 06:44:52 +00:00
|
|
|
WorkingFile *wf = m->wfiles->GetFile(file->def->path);
|
|
|
|
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position))
|
2018-11-03 20:52:43 +00:00
|
|
|
if (sym.kind == Kind::Func || sym.kind == Kind::Type) {
|
2018-10-28 17:49:31 +00:00
|
|
|
result = BuildInitial(m, sym, param.derived, param.qualified,
|
|
|
|
param.levels);
|
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
if (param.hierarchy)
|
|
|
|
reply(result);
|
2018-11-23 18:08:44 +00:00
|
|
|
else
|
|
|
|
reply(FlattenHierarchy(result));
|
2018-10-28 17:49:31 +00:00
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|
2018-10-28 17:49:31 +00:00
|
|
|
|
2018-12-02 23:53:33 +00:00
|
|
|
void MessageHandler::ccls_inheritance(JsonReader &reader, ReplyOnce &reply) {
|
2018-10-28 17:49:31 +00:00
|
|
|
Param param;
|
|
|
|
Reflect(reader, param);
|
|
|
|
Inheritance(this, param, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHandler::textDocument_implementation(
|
|
|
|
TextDocumentPositionParam ¶m, ReplyOnce &reply) {
|
|
|
|
Param param1;
|
|
|
|
param1.textDocument = param.textDocument;
|
|
|
|
param1.position = param.position;
|
|
|
|
param1.derived = true;
|
|
|
|
Inheritance(this, param1, reply);
|
|
|
|
}
|
|
|
|
} // namespace ccls
|