From 73e500bc97170095ebaa886749c51a94518c8fa5 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 19 Aug 2018 13:11:47 -0700 Subject: [PATCH] textDocument/hover: prefer definitions in the same file --- src/messages/textDocument_hover.cc | 44 ++++++++++++++---------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/messages/textDocument_hover.cc b/src/messages/textDocument_hover.cc index e6f4a436..13bf156a 100644 --- a/src/messages/textDocument_hover.cc +++ b/src/messages/textDocument_hover.cc @@ -6,38 +6,36 @@ using namespace ccls; namespace { MethodType kMethodType = "textDocument/hover"; -// Find the comments for |sym|, if any. -std::optional GetComments(DB *db, SymbolRef sym) { - std::optional ret; - WithEntity(db, sym, [&](const auto &entity) { - if (const auto *def = entity.AnyDef()) - if (def->comments[0]) { - lsMarkedString m; - m.value = def->comments; - ret = m; - } - }); - return ret; -} - // Returns the hover or detailed name for `sym`, if any. -std::optional GetHoverOrName(DB *db, LanguageId lang, - SymbolRef sym) { - std::optional ret; +std::pair, std::optional> +GetHover(DB *db, LanguageId lang, SymbolRef sym, int file_id) { + const char *comments = nullptr; + std::optional ls_comments, hover; WithEntity(db, sym, [&](const auto &entity) { - if (const auto *def = entity.AnyDef()) { + std::remove_reference_t *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) { lsMarkedString m; m.language = LanguageIdentifier(lang); if (def->hover[0]) { m.value = def->hover; - ret = m; + hover = m; } else if (def->detailed_name[0]) { m.value = def->detailed_name; - ret = m; + hover = m; } + if (comments) + ls_comments = lsMarkedString{std::nullopt, comments}; } }); - return ret; + return {hover, ls_comments}; } struct In_TextDocumentHover : public RequestInMessage { @@ -83,9 +81,7 @@ struct Handler_TextDocumentHover : BaseMessageHandler { if (!ls_range) continue; - std::optional comments = GetComments(db, sym); - std::optional hover = - GetHoverOrName(db, file->def->language, sym); + auto[hover, comments] = GetHover(db, file->def->language, sym, file->id); if (comments || hover) { out.result = Out_TextDocumentHover::Result(); out.result->range = *ls_range;