forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cquery_inheritance_hierarchy.cc
182 lines (166 loc) · 5.85 KB
/
cquery_inheritance_hierarchy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "message_handler.h"
#include "query_utils.h"
#include "queue_manager.h"
namespace {
MethodType kMethodType = "$cquery/inheritanceHierarchy";
struct In_CqueryInheritanceHierarchy : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
struct Params {
// 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;
Maybe<Id<void>> id;
SymbolKind kind = SymbolKind::Invalid;
// true: derived classes/functions; false: base classes/functions
bool derived = false;
bool detailedName = false;
int levels = 1;
};
Params params;
};
MAKE_REFLECT_STRUCT(In_CqueryInheritanceHierarchy::Params,
textDocument,
position,
id,
kind,
derived,
detailedName,
levels);
MAKE_REFLECT_STRUCT(In_CqueryInheritanceHierarchy, id, params);
REGISTER_IN_MESSAGE(In_CqueryInheritanceHierarchy);
struct Out_CqueryInheritanceHierarchy
: public lsOutMessage<Out_CqueryInheritanceHierarchy> {
struct Entry {
Id<void> 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;
};
lsRequestId id;
optional<Entry> result;
};
MAKE_REFLECT_STRUCT(Out_CqueryInheritanceHierarchy::Entry,
id,
kind,
name,
location,
numChildren,
children);
MAKE_REFLECT_STRUCT(Out_CqueryInheritanceHierarchy, jsonrpc, id, result);
bool Expand(MessageHandler* m,
Out_CqueryInheritanceHierarchy::Entry* entry,
bool derived,
bool detailed_name,
int levels);
template <typename Q>
bool ExpandHelper(MessageHandler* m,
Out_CqueryInheritanceHierarchy::Entry* entry,
bool derived,
bool detailed_name,
int levels,
Q& entity) {
const auto* def = entity.AnyDef();
if (!def) {
entry->numChildren = 0;
return false;
}
if (detailed_name)
entry->name = def->DetailedName(false);
else
entry->name = def->ShortName();
if (def->spell) {
if (optional<lsLocation> loc =
GetLsLocation(m->db, m->working_files, *def->spell))
entry->location = *loc;
}
if (derived) {
if (levels > 0) {
for (auto id : entity.derived) {
Out_CqueryInheritanceHierarchy::Entry entry1;
entry1.id = id;
entry1.kind = entry->kind;
if (Expand(m, &entry1, derived, detailed_name, levels - 1))
entry->children.push_back(std::move(entry1));
}
entry->numChildren = int(entry->children.size());
} else
entry->numChildren = int(entity.derived.size());
} else {
if (levels > 0) {
for (auto id : def->bases) {
Out_CqueryInheritanceHierarchy::Entry entry1;
entry1.id = id;
entry1.kind = entry->kind;
if (Expand(m, &entry1, derived, detailed_name, levels - 1))
entry->children.push_back(std::move(entry1));
}
entry->numChildren = int(entry->children.size());
} else
entry->numChildren = int(def->bases.size());
}
return true;
}
bool Expand(MessageHandler* m,
Out_CqueryInheritanceHierarchy::Entry* entry,
bool derived,
bool detailed_name,
int levels) {
if (entry->kind == SymbolKind::Func)
return ExpandHelper(m, entry, derived, detailed_name, levels,
m->db->funcs[entry->id.id]);
else
return ExpandHelper(m, entry, derived, detailed_name, levels,
m->db->types[entry->id.id]);
}
struct Handler_CqueryInheritanceHierarchy
: BaseMessageHandler<In_CqueryInheritanceHierarchy> {
MethodType GetMethodType() const override { return kMethodType; }
optional<Out_CqueryInheritanceHierarchy::Entry>
BuildInitial(SymbolRef sym, bool derived, bool detailed_name, int levels) {
Out_CqueryInheritanceHierarchy::Entry entry;
entry.id = sym.id;
entry.kind = sym.kind;
Expand(this, &entry, derived, detailed_name, levels);
return entry;
}
void Run(In_CqueryInheritanceHierarchy* request) override {
const auto& params = request->params;
Out_CqueryInheritanceHierarchy out;
out.id = request->id;
if (params.id) {
Out_CqueryInheritanceHierarchy::Entry entry;
entry.id = *params.id;
entry.kind = params.kind;
if (((entry.kind == SymbolKind::Func && entry.id.id < db->funcs.size()) ||
(entry.kind == SymbolKind::Type &&
entry.id.id < db->types.size())) &&
Expand(this, &entry, params.derived, params.detailedName,
params.levels))
out.result = std::move(entry);
} else {
QueryFile* file;
if (!FindFileOrFail(db, project, request->id,
params.textDocument.uri.GetPath(), &file))
return;
WorkingFile* working_file =
working_files->GetFileByFilename(file->def->path);
for (SymbolRef sym : FindSymbolsAtLocation(working_file, file,
request->params.position)) {
if (sym.kind == SymbolKind::Func || sym.kind == SymbolKind::Type) {
out.result = BuildInitial(sym, params.derived, params.detailedName,
params.levels);
break;
}
}
}
QueueManager::WriteStdout(kMethodType, out);
}
};
REGISTER_MESSAGE_HANDLER(Handler_CqueryInheritanceHierarchy);
} // namespace