forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_document_references.cc
103 lines (91 loc) · 3.49 KB
/
text_document_references.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
#include "message_handler.h"
#include "query_utils.h"
#include "queue_manager.h"
#include <loguru.hpp>
namespace {
MethodType kMethodType = "textDocument/references";
struct In_TextDocumentReferences : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
struct lsReferenceContext {
// Include the declaration of the current symbol.
bool includeDeclaration;
// Include references with these |Role| bits set.
Role role = Role::All;
};
struct Params {
lsTextDocumentIdentifier textDocument;
lsPosition position;
lsReferenceContext context;
};
Params params;
};
MAKE_REFLECT_STRUCT(In_TextDocumentReferences::lsReferenceContext,
includeDeclaration,
role);
MAKE_REFLECT_STRUCT(In_TextDocumentReferences::Params,
textDocument,
position,
context);
MAKE_REFLECT_STRUCT(In_TextDocumentReferences, id, params);
REGISTER_IN_MESSAGE(In_TextDocumentReferences);
struct Out_TextDocumentReferences
: public lsOutMessage<Out_TextDocumentReferences> {
lsRequestId id;
std::vector<lsLocationEx> result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result);
struct Handler_TextDocumentReferences
: BaseMessageHandler<In_TextDocumentReferences> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_TextDocumentReferences* request) override {
QueryFile* file;
if (!FindFileOrFail(db, project, request->id,
request->params.textDocument.uri.GetPath(), &file)) {
return;
}
WorkingFile* working_file =
working_files->GetFileByFilename(file->def->path);
Out_TextDocumentReferences out;
out.id = request->id;
bool container = config->xref.container;
for (const SymbolRef& sym :
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return references.
EachOccurrenceWithParent(
db, sym, request->params.context.includeDeclaration,
[&](Use use, lsSymbolKind parent_kind) {
if (use.role & request->params.context.role)
if (optional<lsLocationEx> ls_loc =
GetLsLocationEx(db, working_files, use, container)) {
if (container)
ls_loc->parentKind = parent_kind;
out.result.push_back(*ls_loc);
}
});
break;
}
if (out.result.empty())
for (const IndexInclude& include : file->def->includes)
if (include.line == request->params.position.line) {
// |include| is the line the cursor is on.
for (QueryFile& file1 : db->files)
if (file1.def)
for (const IndexInclude& include1 : file1.def->includes)
if (include1.resolved_path == include.resolved_path) {
// Another file |file1| has the same include line.
lsLocationEx result;
result.uri = lsDocumentUri::FromPath(file1.def->path);
result.range.start.line = result.range.end.line =
include1.line;
out.result.push_back(std::move(result));
break;
}
break;
}
if ((int)out.result.size() >= config->xref.maxNum)
out.result.resize(config->xref.maxNum);
QueueManager::WriteStdout(kMethodType, out);
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentReferences);
} // namespace