mirror of
https://github.com/MaskRay/ccls.git
synced 2025-03-30 13:32:13 +00:00
[hover] Return comments and hover as two MarkedString's
Inspired by @romix's https://github.com/jacobdufault/cquery/pull/187
This commit is contained in:
parent
23bbe81b92
commit
5a22a5cc66
@ -3,36 +3,29 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
static std::string getHoverString(const optional<std::string>& hover,
|
std::pair<optional<std::string>, std::string> GetHoverForSymbol(
|
||||||
const optional<std::string>& comments,
|
QueryDatabase* db,
|
||||||
const std::string& detailed_name) {
|
const SymbolIdx& symbol) {
|
||||||
// TODO: Properly format multi-line comments.
|
|
||||||
std::string ret;
|
|
||||||
if (comments) {
|
|
||||||
ret += *comments;
|
|
||||||
ret += '\n';
|
|
||||||
}
|
|
||||||
return ret + hover.value_or(detailed_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GetHoverForSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
|
|
||||||
switch (symbol.kind) {
|
switch (symbol.kind) {
|
||||||
case SymbolKind::Type: {
|
case SymbolKind::Type: {
|
||||||
QueryType& type = db->types[symbol.idx];
|
QueryType& type = db->types[symbol.idx];
|
||||||
if (type.def)
|
if (type.def)
|
||||||
return getHoverString(type.def->hover, type.def->comments, type.def->detailed_name);
|
return {type.def->comments,
|
||||||
|
type.def->hover.value_or(type.def->detailed_name)};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SymbolKind::Func: {
|
case SymbolKind::Func: {
|
||||||
QueryFunc& func = db->funcs[symbol.idx];
|
QueryFunc& func = db->funcs[symbol.idx];
|
||||||
if (func.def)
|
if (func.def)
|
||||||
return getHoverString(func.def->hover, func.def->comments, func.def->detailed_name);
|
return {func.def->comments,
|
||||||
|
func.def->hover.value_or(func.def->detailed_name)};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SymbolKind::Var: {
|
case SymbolKind::Var: {
|
||||||
QueryVar& var = db->vars[symbol.idx];
|
QueryVar& var = db->vars[symbol.idx];
|
||||||
if (var.def)
|
if (var.def)
|
||||||
return getHoverString(var.def->hover, var.def->comments, var.def->detailed_name);
|
return {var.def->comments,
|
||||||
|
var.def->hover.value_or(var.def->detailed_name)};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SymbolKind::File:
|
case SymbolKind::File:
|
||||||
@ -41,7 +34,7 @@ std::string GetHoverForSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return {nullopt, ""};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Ipc_TextDocumentHover : public IpcMessage<Ipc_TextDocumentHover> {
|
struct Ipc_TextDocumentHover : public IpcMessage<Ipc_TextDocumentHover> {
|
||||||
@ -55,7 +48,7 @@ REGISTER_IPC_MESSAGE(Ipc_TextDocumentHover);
|
|||||||
|
|
||||||
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
|
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
|
||||||
struct Result {
|
struct Result {
|
||||||
lsMarkedString contents;
|
std::vector<lsMarkedString> contents;
|
||||||
optional<lsRange> range;
|
optional<lsRange> range;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -100,11 +93,18 @@ struct TextDocumentHoverHandler : BaseMessageHandler<Ipc_TextDocumentHover> {
|
|||||||
if (!ls_range)
|
if (!ls_range)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
std::string hover = GetHoverForSymbol(db, ref.idx);
|
std::pair<optional<std::string>, std::string> comments_hover =
|
||||||
if (!hover.empty()) {
|
GetHoverForSymbol(db, ref.idx);
|
||||||
|
if (comments_hover.first || comments_hover.second.size()) {
|
||||||
out.result = Out_TextDocumentHover::Result();
|
out.result = Out_TextDocumentHover::Result();
|
||||||
out.result->contents.value = hover;
|
if (comments_hover.first) {
|
||||||
out.result->contents.language = file->def->language;
|
out.result->contents.emplace_back(
|
||||||
|
lsMarkedString{file->def->language, *comments_hover.first});
|
||||||
|
}
|
||||||
|
if (comments_hover.second.size()) {
|
||||||
|
out.result->contents.emplace_back(
|
||||||
|
lsMarkedString{file->def->language, comments_hover.second});
|
||||||
|
}
|
||||||
out.result->range = *ls_range;
|
out.result->range = *ls_range;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user