2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "query_utils.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
using namespace ccls;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/hover";
|
2017-12-23 16:01:43 +00:00
|
|
|
|
2018-04-16 19:36:02 +00:00
|
|
|
// Returns the hover or detailed name for `sym`, if any.
|
2018-08-19 20:11:47 +00:00
|
|
|
std::pair<std::optional<lsMarkedString>, std::optional<lsMarkedString>>
|
|
|
|
GetHover(DB *db, LanguageId lang, SymbolRef sym, int file_id) {
|
|
|
|
const char *comments = nullptr;
|
|
|
|
std::optional<lsMarkedString> ls_comments, hover;
|
2018-08-09 17:08:14 +00:00
|
|
|
WithEntity(db, sym, [&](const auto &entity) {
|
2018-08-19 20:11:47 +00:00
|
|
|
std::remove_reference_t<decltype(entity.def[0])> *def = nullptr;
|
|
|
|
for (auto &d : entity.def) {
|
|
|
|
if (d.spell) {
|
|
|
|
comments = d.comments[0] ? d.comments : nullptr;
|
|
|
|
def = &d;
|
|
|
|
if (d.spell->file_id == file_id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (def) {
|
2018-04-16 19:36:02 +00:00
|
|
|
lsMarkedString m;
|
2018-05-05 03:40:52 +00:00
|
|
|
m.language = LanguageIdentifier(lang);
|
2018-06-08 04:53:41 +00:00
|
|
|
if (def->hover[0]) {
|
2018-04-16 19:36:02 +00:00
|
|
|
m.value = def->hover;
|
2018-08-19 20:11:47 +00:00
|
|
|
hover = m;
|
2018-06-08 04:53:41 +00:00
|
|
|
} else if (def->detailed_name[0]) {
|
2018-04-16 19:36:02 +00:00
|
|
|
m.value = def->detailed_name;
|
2018-08-19 20:11:47 +00:00
|
|
|
hover = m;
|
2018-02-18 05:52:14 +00:00
|
|
|
}
|
2018-08-19 20:11:47 +00:00
|
|
|
if (comments)
|
|
|
|
ls_comments = lsMarkedString{std::nullopt, comments};
|
2017-12-19 05:20:00 +00:00
|
|
|
}
|
2018-04-16 19:36:02 +00:00
|
|
|
});
|
2018-08-19 20:11:47 +00:00
|
|
|
return {hover, ls_comments};
|
2017-12-19 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_TextDocumentHover : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 04:39:44 +00:00
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentHover, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentHover);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
|
|
|
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
|
|
|
|
struct Result {
|
2017-12-26 05:26:03 +00:00
|
|
|
std::vector<lsMarkedString> contents;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<lsRange> range;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
lsRequestId id;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Result> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range);
|
2018-08-09 17:08:14 +00:00
|
|
|
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_TextDocumentHover, jsonrpc, id,
|
2018-05-08 03:59:08 +00:00
|
|
|
result);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_TextDocumentHover : BaseMessageHandler<In_TextDocumentHover> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2018-08-09 17:08:14 +00:00
|
|
|
void Run(In_TextDocumentHover *request) override {
|
|
|
|
auto ¶ms = request->params;
|
|
|
|
QueryFile *file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2018-04-16 19:36:02 +00:00
|
|
|
params.textDocument.uri.GetPath(), &file))
|
2017-12-06 03:32:33 +00:00
|
|
|
return;
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
WorkingFile *working_file =
|
2017-12-06 03:32:33 +00:00
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
Out_TextDocumentHover out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-02-10 06:51:58 +00:00
|
|
|
for (SymbolRef sym :
|
2018-04-16 19:36:02 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, params.position)) {
|
2017-12-06 03:32:33 +00:00
|
|
|
// Found symbol. Return hover.
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<lsRange> ls_range = GetLsRange(
|
2018-02-09 17:42:10 +00:00
|
|
|
working_files->GetFileByFilename(file->def->path), sym.range);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (!ls_range)
|
|
|
|
continue;
|
|
|
|
|
2018-08-19 20:11:47 +00:00
|
|
|
auto[hover, comments] = GetHover(db, file->def->language, sym, file->id);
|
2018-04-16 19:36:02 +00:00
|
|
|
if (comments || hover) {
|
2017-12-19 07:27:52 +00:00
|
|
|
out.result = Out_TextDocumentHover::Result();
|
|
|
|
out.result->range = *ls_range;
|
2018-04-16 19:36:02 +00:00
|
|
|
if (comments)
|
|
|
|
out.result->contents.push_back(*comments);
|
|
|
|
if (hover)
|
|
|
|
out.result->contents.push_back(*hover);
|
2017-12-19 07:27:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
pipeline::WriteStdout(kMethodType, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentHover);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|