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
|
|
|
// Find the comments for |sym|, if any.
|
2018-05-30 06:56:14 +00:00
|
|
|
std::optional<lsMarkedString> GetComments(DB* db, SymbolRef sym) {
|
2018-04-16 19:36:02 +00:00
|
|
|
std::optional<lsMarkedString> ret;
|
|
|
|
WithEntity(db, sym, [&](const auto& entity) {
|
|
|
|
if (const auto* def = entity.AnyDef())
|
|
|
|
if (!def->comments.empty()) {
|
|
|
|
lsMarkedString m;
|
|
|
|
m.value = def->comments;
|
|
|
|
ret = m;
|
2018-02-18 05:52:14 +00:00
|
|
|
}
|
2018-04-16 19:36:02 +00:00
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the hover or detailed name for `sym`, if any.
|
2018-05-30 06:56:14 +00:00
|
|
|
std::optional<lsMarkedString> GetHoverOrName(DB* db,
|
2018-05-05 03:40:52 +00:00
|
|
|
LanguageId lang,
|
|
|
|
SymbolRef sym) {
|
2018-04-16 19:36:02 +00:00
|
|
|
std::optional<lsMarkedString> ret;
|
|
|
|
WithEntity(db, sym, [&](const auto& entity) {
|
|
|
|
if (const auto* def = entity.AnyDef()) {
|
|
|
|
lsMarkedString m;
|
2018-05-05 03:40:52 +00:00
|
|
|
m.language = LanguageIdentifier(lang);
|
2018-04-16 19:36:02 +00:00
|
|
|
if (!def->hover.empty()) {
|
|
|
|
m.value = def->hover;
|
|
|
|
ret = m;
|
|
|
|
} else if (!def->detailed_name.empty()) {
|
|
|
|
m.value = def->detailed_name;
|
|
|
|
ret = m;
|
2018-02-18 05:52:14 +00:00
|
|
|
}
|
2017-12-19 05:20:00 +00:00
|
|
|
}
|
2018-04-16 19:36:02 +00:00
|
|
|
});
|
|
|
|
return ret;
|
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-05-08 03:59:08 +00:00
|
|
|
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_TextDocumentHover,
|
|
|
|
jsonrpc,
|
|
|
|
id,
|
|
|
|
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; }
|
|
|
|
void Run(In_TextDocumentHover* request) override {
|
2018-04-16 19:36:02 +00:00
|
|
|
auto& params = request->params;
|
2017-12-06 03:32:33 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
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-04-16 19:36:02 +00:00
|
|
|
std::optional<lsMarkedString> comments = GetComments(db, sym);
|
|
|
|
std::optional<lsMarkedString> hover =
|
|
|
|
GetHoverOrName(db, file->def->language, sym);
|
|
|
|
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);
|
2017-12-19 07:10:42 +00:00
|
|
|
} // namespace
|