ccls/src/messages/text_document_hover.cc

119 lines
3.6 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 {
2017-12-23 16:01:43 +00:00
std::pair<std::string_view, std::string_view> GetCommentsAndHover(
QueryDatabase* db,
2018-02-09 17:42:10 +00:00
SymbolRef sym) {
switch (sym.kind) {
case SymbolKind::Type: {
2018-02-09 17:42:10 +00:00
QueryType& type = db->types[sym.Idx()];
if (type.def)
return {type.def->comments, type.def->hover.size()
? type.def->hover
: type.def->detailed_name};
break;
}
case SymbolKind::Func: {
2018-02-09 17:42:10 +00:00
QueryFunc& func = db->funcs[sym.Idx()];
if (func.def)
return {func.def->comments, func.def->hover.size()
? func.def->hover
: func.def->detailed_name};
break;
}
case SymbolKind::Var: {
2018-02-09 17:42:10 +00:00
QueryVar& var = db->vars[sym.Idx()];
if (var.def)
return {var.def->comments, var.def->hover.size()
? var.def->hover
: var.def->detailed_name};
break;
}
case SymbolKind::File:
case SymbolKind::Invalid: {
assert(false && "unexpected");
break;
}
}
return {"", ""};
}
struct Ipc_TextDocumentHover : public RequestMessage<Ipc_TextDocumentHover> {
2017-12-06 04:39:44 +00:00
const static IpcId kIpcId = IpcId::TextDocumentHover;
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(Ipc_TextDocumentHover, id, params);
REGISTER_IPC_MESSAGE(Ipc_TextDocumentHover);
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
struct Result {
std::vector<lsMarkedString> contents;
2017-12-06 04:39:44 +00:00
optional<lsRange> range;
};
lsRequestId id;
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 {
// 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
2017-12-06 03:32:33 +00:00
struct TextDocumentHoverHandler : BaseMessageHandler<Ipc_TextDocumentHover> {
void Run(Ipc_TextDocumentHover* request) override {
QueryFile* file;
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_TextDocumentHover out;
out.id = request->id;
2018-02-09 17:42:10 +00:00
for (const SymbolRef& sym :
2017-12-06 03:32:33 +00:00
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return hover.
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;
std::pair<std::string_view, std::string_view> comments_hover =
2018-02-09 17:42:10 +00:00
GetCommentsAndHover(db, sym);
if (comments_hover.first.size() || comments_hover.second.size()) {
out.result = Out_TextDocumentHover::Result();
if (comments_hover.first.size()) {
out.result->contents.emplace_back(comments_hover.first);
}
if (comments_hover.second.size()) {
out.result->contents.emplace_back(lsMarkedString1{
std::string_view(file->def->language), comments_hover.second});
}
out.result->range = *ls_range;
break;
}
2017-12-06 03:32:33 +00:00
}
2017-12-24 00:25:18 +00:00
QueueManager::WriteStdout(IpcId::TextDocumentHover, out);
2017-12-06 03:32:33 +00:00
}
};
REGISTER_MESSAGE_HANDLER(TextDocumentHoverHandler);
} // namespace