2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/references";
|
|
|
|
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_TextDocumentReferences : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 04:39:44 +00:00
|
|
|
struct lsReferenceContext {
|
|
|
|
// Include the declaration of the current symbol.
|
|
|
|
bool includeDeclaration;
|
2018-02-22 21:50:46 +00:00
|
|
|
// Include references with these |Role| bits set.
|
|
|
|
Role role = Role::All;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-01-20 05:11:03 +00:00
|
|
|
struct Params {
|
2017-12-06 04:39:44 +00:00
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
|
|
|
lsReferenceContext context;
|
|
|
|
};
|
|
|
|
|
2018-01-20 05:11:03 +00:00
|
|
|
Params params;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentReferences::lsReferenceContext,
|
2018-02-22 21:50:46 +00:00
|
|
|
includeDeclaration,
|
|
|
|
role);
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentReferences::Params,
|
2017-12-06 04:39:44 +00:00
|
|
|
textDocument,
|
|
|
|
position,
|
|
|
|
context);
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentReferences, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentReferences);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
|
|
|
struct Out_TextDocumentReferences
|
|
|
|
: public lsOutMessage<Out_TextDocumentReferences> {
|
|
|
|
lsRequestId id;
|
2018-02-11 01:50:44 +00:00
|
|
|
std::vector<lsLocationEx> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result);
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_TextDocumentReferences
|
|
|
|
: BaseMessageHandler<In_TextDocumentReferences> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
|
|
|
void Run(In_TextDocumentReferences* request) override {
|
2017-12-06 03:32:33 +00:00
|
|
|
QueryFile* file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2017-12-06 03:32:33 +00:00
|
|
|
request->params.textDocument.uri.GetPath(), &file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
Out_TextDocumentReferences out;
|
|
|
|
out.id = request->id;
|
2018-04-04 06:05:41 +00:00
|
|
|
bool container = g_config->xref.container;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-02-09 17:42:10 +00:00
|
|
|
for (const SymbolRef& sym :
|
2017-12-06 03:32:33 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.position)) {
|
|
|
|
// Found symbol. Return references.
|
2018-03-07 03:07:54 +00:00
|
|
|
EachOccurrenceWithParent(
|
|
|
|
db, sym, request->params.context.includeDeclaration,
|
|
|
|
[&](Use use, lsSymbolKind parent_kind) {
|
2018-02-23 23:27:21 +00:00
|
|
|
if (use.role & request->params.context.role)
|
2018-03-31 03:16:33 +00:00
|
|
|
if (std::optional<lsLocationEx> ls_loc =
|
2018-03-07 03:07:54 +00:00
|
|
|
GetLsLocationEx(db, working_files, use, container)) {
|
|
|
|
if (container)
|
|
|
|
ls_loc->parentKind = parent_kind;
|
2018-02-23 23:27:21 +00:00
|
|
|
out.result.push_back(*ls_loc);
|
2018-03-07 03:07:54 +00:00
|
|
|
}
|
2018-02-23 23:27:21 +00:00
|
|
|
});
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-01-20 05:11:03 +00:00
|
|
|
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.
|
2018-02-11 01:50:44 +00:00
|
|
|
lsLocationEx result;
|
2018-01-20 05:11:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-04 06:05:41 +00:00
|
|
|
if ((int)out.result.size() >= g_config->xref.maxNum)
|
|
|
|
out.result.resize(g_config->xref.maxNum);
|
2018-03-22 04:05:25 +00:00
|
|
|
QueueManager::WriteStdout(kMethodType, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentReferences);
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|