2017-12-06 03:32:33 +00:00
|
|
|
#include "lex_utils.h"
|
|
|
|
#include "message_handler.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
|
|
|
#include "working_files.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_TextDocumentDocumentLink
|
|
|
|
: public IpcMessage<Ipc_TextDocumentDocumentLink> {
|
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentDocumentLink;
|
|
|
|
|
|
|
|
struct DocumentLinkParams {
|
|
|
|
// The document to provide document links for.
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
};
|
|
|
|
|
|
|
|
lsRequestId id;
|
|
|
|
DocumentLinkParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentLink::DocumentLinkParams,
|
|
|
|
textDocument);
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentDocumentLink, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentDocumentLink);
|
|
|
|
|
|
|
|
// A document link is a range in a text document that links to an internal or
|
|
|
|
// external resource, like another text document or a web site.
|
|
|
|
struct lsDocumentLink {
|
|
|
|
// The range this link applies to.
|
|
|
|
lsRange range;
|
|
|
|
// The uri this link points to. If missing a resolve request is sent later.
|
|
|
|
optional<lsDocumentUri> target;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(lsDocumentLink, range, target);
|
|
|
|
|
|
|
|
struct Out_TextDocumentDocumentLink
|
|
|
|
: public lsOutMessage<Out_TextDocumentDocumentLink> {
|
|
|
|
lsRequestId id;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsDocumentLink> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentDocumentLink, jsonrpc, id, result);
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct TextDocumentDocumentLinkHandler
|
|
|
|
: BaseMessageHandler<Ipc_TextDocumentDocumentLink> {
|
|
|
|
void Run(Ipc_TextDocumentDocumentLink* request) override {
|
|
|
|
Out_TextDocumentDocumentLink out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
|
|
|
if (config->showDocumentLinksOnIncludes) {
|
|
|
|
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(
|
|
|
|
request->params.textDocument.uri.GetPath());
|
|
|
|
if (!working_file) {
|
|
|
|
LOG_S(WARNING) << "Unable to find working file "
|
|
|
|
<< request->params.textDocument.uri.GetPath();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const IndexInclude& include : file->def->includes) {
|
2018-01-14 18:50:54 +00:00
|
|
|
optional<int> buffer_line =
|
2018-01-14 22:24:47 +00:00
|
|
|
working_file->GetBufferPosFromIndexPos(include.line, nullptr);
|
2018-01-14 18:50:54 +00:00
|
|
|
if (!buffer_line)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Subtract 1 from line because querydb stores 1-based lines but
|
|
|
|
// vscode expects 0-based lines.
|
2018-01-14 18:50:54 +00:00
|
|
|
optional<lsRange> between_quotes = ExtractQuotedRange(
|
2018-01-14 19:39:29 +00:00
|
|
|
*buffer_line, working_file->buffer_lines[*buffer_line]);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (!between_quotes)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lsDocumentLink link;
|
|
|
|
link.target = lsDocumentUri::FromPath(include.resolved_path);
|
|
|
|
link.range = *between_quotes;
|
|
|
|
out.result.push_back(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentDocumentLink, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentDocumentLinkHandler);
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|