ccls/src/messages/text_document_hover.cc

117 lines
3.5 KiB
C++
Raw Normal View History

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
namespace {
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.
std::optional<lsMarkedString> GetComments(QueryDatabase* db, SymbolRef sym) {
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-04-16 19:36:02 +00:00
});
return ret;
}
// Returns the hover or detailed name for `sym`, if any.
std::optional<lsMarkedString> GetHoverOrName(QueryDatabase* 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-04-16 19:36:02 +00:00
});
return ret;
}
2018-03-22 05:01:21 +00:00
struct In_TextDocumentHover : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
2017-12-06 04:39:44 +00:00
lsTextDocumentPositionParams params;
};
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 {
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);
2017-12-23 16:01:43 +00:00
void Reflect(Writer& visitor, Out_TextDocumentHover& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
if (value.result)
REFLECT_MEMBER(result);
else {
2018-04-16 19:36:02 +00:00
// Empty optional<> is elided by the default serializer, we need to write
// |null| to be compliant with the LSP.
visitor.Key("result");
visitor.Null();
}
REFLECT_MEMBER_END();
}
2017-12-06 04:39:44 +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;
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) {
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);
break;
}
2017-12-06 03:32:33 +00:00
}
QueueManager::WriteStdout(kMethodType, out);
2017-12-06 03:32:33 +00:00
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentHover);
} // namespace