-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathtextDocument_code.cc
More file actions
299 lines (279 loc) · 9.46 KB
/
textDocument_code.cc
File metadata and controls
299 lines (279 loc) · 9.46 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#include "message_handler.hh"
#include "pipeline.hh"
#include "query.hh"
#include <llvm/Support/FormatVariadic.h>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <unordered_set>
namespace ccls {
namespace {
struct Command {
std::string title;
std::string command;
std::vector<std::string> arguments;
};
struct CodeAction {
std::string title;
std::string kind;
WorkspaceEdit edit;
Command command;
};
struct ReferenceCommand {
TextDocumentIdentifier textDocument;
Position position;
bool callee;
std::string direction;
bool derived;
int kind;
};
REFLECT_STRUCT(Command, title, command, arguments);
REFLECT_STRUCT(CodeAction, title, kind, edit, command);
REFLECT_STRUCT(ReferenceCommand, textDocument, position,
callee, direction, derived, kind);
template <typename T> std::string toString(T &v) {
rapidjson::StringBuffer output;
rapidjson::Writer<rapidjson::StringBuffer> writer(output);
JsonWriter json_writer(&writer);
reflect(json_writer, v);
return output.GetString();
}
} // namespace
template <typename T> bool vec_has(const std::vector<T> &vec, const T &key) {
return std::find(std::begin(vec), std::end(vec), key) != std::end(vec);
}
bool should_send_action(std::vector<std::string> available_kinds,
std::vector<std::string> requested_kinds,
std::string kind) {
if (!requested_kinds.empty() && !vec_has(requested_kinds, kind)) {
return false;
}
if (!available_kinds.empty() && !vec_has(available_kinds, kind)) {
return false;
}
return true;
}
void MessageHandler::textDocument_codeAction(CodeActionParam ¶m,
ReplyOnce &reply) {
WorkingFile *wf = findOrFail(param.textDocument.uri.getPath(), reply).second;
if (!wf)
return;
auto available_kinds = g_config->client.codeActionKind;
std::vector<std::string> requested_kinds = param.context.only;
std::vector<CodeAction> result;
if (should_send_action(available_kinds, requested_kinds, "quickfix")) {
std::vector<Diagnostic> diagnostics;
wfiles->withLock([&]() { diagnostics = wf->diagnostics; });
for (Diagnostic &diag : diagnostics)
if (diag.fixits_.size() &&
(param.range.intersects(diag.range) ||
llvm::any_of(diag.fixits_, [&](const TextEdit &edit) {
return param.range.intersects(edit.range);
}))) {
CodeAction &cmd = result.emplace_back();
cmd.title = "FixIt: " + diag.message;
cmd.kind = "quickfix";
auto &edit = cmd.edit.documentChanges.emplace_back();
edit.textDocument.uri = param.textDocument.uri;
edit.textDocument.version = wf->version;
edit.edits = diag.fixits_;
}
}
if (should_send_action(available_kinds, requested_kinds, "reference")) {
auto add = [&, param = param] (
const char *title, const char *command_name,
const bool callee=false, const char *dir="",
const bool derived=false, int kind=0) {
CodeAction &cmd = result.emplace_back();
ReferenceCommand rcmd;
rcmd.textDocument = param.textDocument;
rcmd.position = param.range.start;
rcmd.callee = callee;
rcmd.direction = dir;
rcmd.derived = derived;
rcmd.kind = kind;
cmd.title = title;
cmd.kind = "reference";
cmd.command.title = title;
cmd.command.command = command_name;
cmd.command.arguments.push_back(toString(rcmd));
};
add("call", "$ccls/call");
add("callee", "$ccls/call", true);
add("navigate-up", "$ccls/navigate", false, "U");
add("navigate-down", "$ccls/navigate", false, "D");
add("navigate-right", "$ccls/navigate", false, "R");
add("navigate-left", "$ccls/navigate", false, "L");
add("inheritance", "$ccls/inheritance");
add("inheritance-derived", "$ccls/inheritance", false, "", true);
add("member-var", "$ccls/member", false, "", false, 4);
add("member-fun", "$ccls/member", false, "", false, 3);
add("member-type", "$ccls/member", false, "", false, 2);
add("vars", "$ccls/vars");
}
reply(result);
}
namespace {
struct Cmd_xref {
Usr usr;
Kind kind;
std::string field;
};
struct CodeLens {
lsRange range;
std::optional<Command> command;
};
REFLECT_STRUCT(Cmd_xref, usr, kind, field);
REFLECT_STRUCT(CodeLens, range, command);
struct CommonCodeLensParams {
std::vector<CodeLens> *result;
DB *db;
WorkingFile *wfile;
};
} // namespace
void MessageHandler::textDocument_codeLens(TextDocumentParam ¶m,
ReplyOnce &reply) {
auto [file, wf] = findOrFail(param.textDocument.uri.getPath(), reply);
if (!wf)
return;
std::vector<CodeLens> result;
auto add = [&, wf = wf](const char *singular, Cmd_xref show, Range range,
int num, bool force_display = false) {
if (!num && !force_display)
return;
std::optional<lsRange> ls_range = getLsRange(wf, range);
if (!ls_range)
return;
CodeLens &code_lens = result.emplace_back();
code_lens.range = *ls_range;
code_lens.command = Command();
code_lens.command->command = std::string(ccls_xref);
bool plural = num > 1 && singular[strlen(singular) - 1] != 'd';
code_lens.command->title =
llvm::formatv("{0} {1}{2}", num, singular, plural ? "s" : "").str();
code_lens.command->arguments.push_back(toString(show));
};
std::unordered_set<Range> seen;
for (auto [sym, refcnt] : file->symbol2refcnt) {
if (refcnt <= 0 || !sym.extent.valid() || !seen.insert(sym.range).second)
continue;
switch (sym.kind) {
case Kind::Func: {
QueryFunc &func = db->getFunc(sym);
const QueryFunc::Def *def = func.anyDef();
if (!def)
continue;
std::vector<Use> base_uses = getUsesForAllBases(db, func);
std::vector<Use> derived_uses = getUsesForAllDerived(db, func);
add("ref", {sym.usr, Kind::Func, "uses"}, sym.range, func.uses.size(),
base_uses.empty());
if (base_uses.size())
add("b.ref", {sym.usr, Kind::Func, "bases uses"}, sym.range,
base_uses.size());
if (derived_uses.size())
add("d.ref", {sym.usr, Kind::Func, "derived uses"}, sym.range,
derived_uses.size());
if (base_uses.empty())
add("base", {sym.usr, Kind::Func, "bases"}, sym.range,
def->bases.size());
add("derived", {sym.usr, Kind::Func, "derived"}, sym.range,
func.derived.size());
break;
}
case Kind::Type: {
QueryType &type = db->getType(sym);
add("ref", {sym.usr, Kind::Type, "uses"}, sym.range, type.uses.size(),
true);
add("derived", {sym.usr, Kind::Type, "derived"}, sym.range,
type.derived.size());
add("var", {sym.usr, Kind::Type, "instances"}, sym.range,
type.instances.size());
break;
}
case Kind::Var: {
QueryVar &var = db->getVar(sym);
const QueryVar::Def *def = var.anyDef();
if (!def || (def->is_local() && !g_config->codeLens.localVariables))
continue;
add("ref", {sym.usr, Kind::Var, "uses"}, sym.range, var.uses.size(),
def->kind != SymbolKind::Macro);
break;
}
case Kind::File:
case Kind::Invalid:
llvm_unreachable("");
};
}
reply(result);
}
void MessageHandler::workspace_executeCommand(JsonReader &reader,
ReplyOnce &reply) {
Command param;
reflect(reader, param);
if (param.arguments.empty()) {
return;
}
rapidjson::Document reader1;
reader1.Parse(param.arguments[0].c_str());
JsonReader json_reader{&reader1};
if (param.command == ccls_xref) {
Cmd_xref cmd;
reflect(json_reader, cmd);
std::vector<Location> result;
auto map = [&](auto &&uses) {
for (auto &use : uses)
if (auto loc = getLsLocation(db, wfiles, use))
result.push_back(std::move(*loc));
};
switch (cmd.kind) {
case Kind::Func: {
QueryFunc &func = db->getFunc(cmd.usr);
if (cmd.field == "bases") {
if (auto *def = func.anyDef())
map(getFuncDeclarations(db, def->bases));
} else if (cmd.field == "bases uses") {
map(getUsesForAllBases(db, func));
} else if (cmd.field == "derived") {
map(getFuncDeclarations(db, func.derived));
} else if (cmd.field == "derived uses") {
map(getUsesForAllDerived(db, func));
} else if (cmd.field == "uses") {
map(func.uses);
}
break;
}
case Kind::Type: {
QueryType &type = db->getType(cmd.usr);
if (cmd.field == "derived") {
map(getTypeDeclarations(db, type.derived));
} else if (cmd.field == "instances") {
map(getVarDeclarations(db, type.instances, 7));
} else if (cmd.field == "uses") {
map(type.uses);
}
break;
}
case Kind::Var: {
QueryVar &var = db->getVar(cmd.usr);
if (cmd.field == "uses")
map(var.uses);
break;
}
default:
break;
}
reply(result);
} else if (param.command == "$ccls/call") {
ccls_call(json_reader, reply);
} else if (param.command == "$ccls/navigate") {
ccls_navigate(json_reader, reply);
} else if (param.command == "$ccls/inheritance") {
ccls_inheritance(json_reader, reply);
} else if (param.command == "$ccls/member") {
ccls_member(json_reader, reply);
} else if (param.command == "$ccls/vars") {
ccls_vars(json_reader, reply);
}
}
} // namespace ccls