#include "message_handler.h" #include "query_utils.h" #include "queue_manager.h" namespace { MethodType kMethodType = "textDocument/hover"; // Find the comments for |sym|, if any. std::optional GetComments(QueryDatabase* db, SymbolRef sym) { std::optional 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; } }); return ret; } // Returns the hover or detailed name for `sym`, if any. std::optional GetHoverOrName(QueryDatabase* db, const std::string& language, SymbolRef sym) { std::optional ret; WithEntity(db, sym, [&](const auto& entity) { if (const auto* def = entity.AnyDef()) { lsMarkedString m; m.language = language; if (!def->hover.empty()) { m.value = def->hover; ret = m; } else if (!def->detailed_name.empty()) { m.value = def->detailed_name; ret = m; } } }); return ret; } struct In_TextDocumentHover : public RequestInMessage { MethodType GetMethodType() const override { return kMethodType; } lsTextDocumentPositionParams params; }; MAKE_REFLECT_STRUCT(In_TextDocumentHover, id, params); REGISTER_IN_MESSAGE(In_TextDocumentHover); struct Out_TextDocumentHover : public lsOutMessage { struct Result { std::vector contents; std::optional range; }; lsRequestId id; std::optional result; }; MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range); 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(); } struct Handler_TextDocumentHover : BaseMessageHandler { MethodType GetMethodType() const override { return kMethodType; } void Run(In_TextDocumentHover* request) override { auto& params = request->params; QueryFile* file; if (!FindFileOrFail(db, project, request->id, params.textDocument.uri.GetPath(), &file)) return; WorkingFile* working_file = working_files->GetFileByFilename(file->def->path); Out_TextDocumentHover out; out.id = request->id; for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, params.position)) { // Found symbol. Return hover. std::optional ls_range = GetLsRange( working_files->GetFileByFilename(file->def->path), sym.range); if (!ls_range) continue; std::optional comments = GetComments(db, sym); std::optional hover = GetHoverOrName(db, file->def->language, sym); if (comments || hover) { out.result = Out_TextDocumentHover::Result(); out.result->range = *ls_range; if (comments) out.result->contents.push_back(*comments); if (hover) out.result->contents.push_back(*hover); break; } } QueueManager::WriteStdout(kMethodType, out); } }; REGISTER_MESSAGE_HANDLER(Handler_TextDocumentHover); } // namespace