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 {
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Ipc_TextDocumentReferences
|
2018-01-19 08:47:52 +00:00
|
|
|
: public RequestMessage<Ipc_TextDocumentReferences> {
|
2018-01-20 05:11:03 +00:00
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentReferences;
|
2017-12-06 04:39:44 +00:00
|
|
|
struct lsReferenceContext {
|
|
|
|
// Include the declaration of the current symbol.
|
|
|
|
bool includeDeclaration;
|
|
|
|
};
|
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
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences::lsReferenceContext,
|
|
|
|
includeDeclaration);
|
2018-01-20 05:11:03 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences::Params,
|
2017-12-06 04:39:44 +00:00
|
|
|
textDocument,
|
|
|
|
position,
|
|
|
|
context);
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentReferences, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentReferences);
|
|
|
|
|
|
|
|
struct Out_TextDocumentReferences
|
|
|
|
: public lsOutMessage<Out_TextDocumentReferences> {
|
|
|
|
lsRequestId id;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsLocation> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentReferences, jsonrpc, id, result);
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct TextDocumentReferencesHandler
|
|
|
|
: BaseMessageHandler<Ipc_TextDocumentReferences> {
|
|
|
|
void Run(Ipc_TextDocumentReferences* request) override {
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (const SymbolRef& ref :
|
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.position)) {
|
|
|
|
optional<QueryLocation> excluded_declaration;
|
|
|
|
if (!request->params.context.includeDeclaration) {
|
|
|
|
LOG_S(INFO) << "Excluding declaration in references";
|
|
|
|
excluded_declaration = GetDefinitionSpellingOfSymbol(db, ref.idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found symbol. Return references.
|
|
|
|
std::vector<QueryLocation> uses = GetUsesOfSymbol(db, ref.idx);
|
|
|
|
out.result.reserve(uses.size());
|
|
|
|
for (const QueryLocation& use : uses) {
|
|
|
|
if (excluded_declaration.has_value() && use == *excluded_declaration)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
optional<lsLocation> ls_location =
|
|
|
|
GetLsLocation(db, working_files, use);
|
|
|
|
if (ls_location)
|
|
|
|
out.result.push_back(*ls_location);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
lsLocation 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;
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentReferences, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentReferencesHandler);
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|