2018-08-21 05:27:52 +00:00
|
|
|
// Copyright 2017-2018 ccls Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "message_handler.hh"
|
2018-11-27 06:29:28 +00:00
|
|
|
#include "query.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-11-03 20:52:43 +00:00
|
|
|
struct MarkedString {
|
2018-10-28 17:49:31 +00:00
|
|
|
std::optional<std::string> language;
|
|
|
|
std::string value;
|
|
|
|
};
|
|
|
|
struct Hover {
|
2018-11-03 20:52:43 +00:00
|
|
|
std::vector<MarkedString> contents;
|
2018-10-28 17:49:31 +00:00
|
|
|
std::optional<lsRange> range;
|
|
|
|
};
|
|
|
|
|
2018-12-02 23:53:33 +00:00
|
|
|
void Reflect(JsonWriter &vis, MarkedString &v) {
|
2018-10-28 17:49:31 +00:00
|
|
|
// If there is a language, emit a `{language:string, value:string}` object. If
|
|
|
|
// not, emit a string.
|
2018-11-23 19:53:21 +00:00
|
|
|
if (v.language) {
|
2018-12-02 23:53:33 +00:00
|
|
|
vis.StartObject();
|
2018-10-28 17:49:31 +00:00
|
|
|
REFLECT_MEMBER(language);
|
|
|
|
REFLECT_MEMBER(value);
|
2018-12-02 23:53:33 +00:00
|
|
|
vis.EndObject();
|
2018-10-28 17:49:31 +00:00
|
|
|
} else {
|
2018-11-23 19:53:21 +00:00
|
|
|
Reflect(vis, v.value);
|
2018-10-28 17:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-02 23:53:33 +00:00
|
|
|
REFLECT_STRUCT(Hover, contents, range);
|
2017-12-23 16:01:43 +00:00
|
|
|
|
2018-10-14 06:22:29 +00:00
|
|
|
const char *LanguageIdentifier(LanguageId lang) {
|
|
|
|
switch (lang) {
|
|
|
|
// clang-format off
|
|
|
|
case LanguageId::C: return "c";
|
|
|
|
case LanguageId::Cpp: return "cpp";
|
|
|
|
case LanguageId::ObjC: return "objective-c";
|
|
|
|
case LanguageId::ObjCpp: return "objective-cpp";
|
|
|
|
default: return "";
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 19:36:02 +00:00
|
|
|
// Returns the hover or detailed name for `sym`, if any.
|
2018-11-03 20:52:43 +00:00
|
|
|
std::pair<std::optional<MarkedString>, std::optional<MarkedString>>
|
2018-08-19 20:11:47 +00:00
|
|
|
GetHover(DB *db, LanguageId lang, SymbolRef sym, int file_id) {
|
|
|
|
const char *comments = nullptr;
|
2018-11-03 20:52:43 +00:00
|
|
|
std::optional<MarkedString> ls_comments, hover;
|
2018-08-09 17:08:14 +00:00
|
|
|
WithEntity(db, sym, [&](const auto &entity) {
|
2018-08-19 20:11:47 +00:00
|
|
|
std::remove_reference_t<decltype(entity.def[0])> *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;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 07:22:40 +00:00
|
|
|
if (!def && entity.def.size()) {
|
|
|
|
def = &entity.def[0];
|
|
|
|
if (def->comments[0])
|
|
|
|
comments = def->comments;
|
|
|
|
}
|
2018-08-19 20:11:47 +00:00
|
|
|
if (def) {
|
2018-11-03 20:52:43 +00:00
|
|
|
MarkedString m;
|
2018-05-05 03:40:52 +00:00
|
|
|
m.language = LanguageIdentifier(lang);
|
2018-06-08 04:53:41 +00:00
|
|
|
if (def->hover[0]) {
|
2018-04-16 19:36:02 +00:00
|
|
|
m.value = def->hover;
|
2018-08-19 20:11:47 +00:00
|
|
|
hover = m;
|
2018-06-08 04:53:41 +00:00
|
|
|
} else if (def->detailed_name[0]) {
|
2018-04-16 19:36:02 +00:00
|
|
|
m.value = def->detailed_name;
|
2018-08-19 20:11:47 +00:00
|
|
|
hover = m;
|
2018-02-18 05:52:14 +00:00
|
|
|
}
|
2018-08-19 20:11:47 +00:00
|
|
|
if (comments)
|
2018-11-03 20:52:43 +00:00
|
|
|
ls_comments = MarkedString{std::nullopt, comments};
|
2017-12-19 05:20:00 +00:00
|
|
|
}
|
2018-04-16 19:36:02 +00:00
|
|
|
});
|
2018-08-19 20:11:47 +00:00
|
|
|
return {hover, ls_comments};
|
2017-12-19 05:20:00 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
} // namespace
|
2017-12-19 05:20:00 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
void MessageHandler::textDocument_hover(TextDocumentPositionParam ¶m,
|
|
|
|
ReplyOnce &reply) {
|
2018-12-02 00:55:51 +00:00
|
|
|
QueryFile *file = FindFile(param.textDocument.uri.GetPath());
|
2018-12-01 06:44:52 +00:00
|
|
|
WorkingFile *wf = file ? wfiles->GetFile(file->def->path) : nullptr;
|
2018-12-02 00:55:51 +00:00
|
|
|
if (!wf) {
|
|
|
|
reply.NotReady(file);
|
2018-10-28 17:49:31 +00:00
|
|
|
return;
|
2018-12-02 00:55:51 +00:00
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-12-02 00:55:51 +00:00
|
|
|
Hover result;
|
2018-12-01 06:44:52 +00:00
|
|
|
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position)) {
|
2018-10-29 04:39:17 +00:00
|
|
|
std::optional<lsRange> ls_range =
|
2018-12-01 06:44:52 +00:00
|
|
|
GetLsRange(wfiles->GetFile(file->def->path), sym.range);
|
2018-10-28 17:49:31 +00:00
|
|
|
if (!ls_range)
|
|
|
|
continue;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
auto [hover, comments] = GetHover(db, file->def->language, sym, file->id);
|
|
|
|
if (comments || hover) {
|
|
|
|
result.range = *ls_range;
|
|
|
|
if (comments)
|
|
|
|
result.contents.push_back(*comments);
|
|
|
|
if (hover)
|
|
|
|
result.contents.push_back(*hover);
|
|
|
|
break;
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
|
|
|
|
reply(result);
|
|
|
|
}
|
|
|
|
} // namespace ccls
|