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
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/hover";
|
2017-12-23 16:01:43 +00:00
|
|
|
|
2018-01-31 07:48:34 +00:00
|
|
|
std::pair<std::string_view, std::string_view> GetCommentsAndHover(
|
2017-12-26 05:26:03 +00:00
|
|
|
QueryDatabase* db,
|
2018-02-09 17:42:10 +00:00
|
|
|
SymbolRef sym) {
|
|
|
|
switch (sym.kind) {
|
2017-12-19 05:20:00 +00:00
|
|
|
case SymbolKind::Type: {
|
2018-02-18 05:52:14 +00:00
|
|
|
if (const auto* def = db->GetType(sym).AnyDef()) {
|
|
|
|
return {def->comments, !def->hover.empty()
|
|
|
|
? std::string_view(def->hover)
|
|
|
|
: std::string_view(def->detailed_name)};
|
|
|
|
}
|
2017-12-19 05:20:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Func: {
|
2018-02-18 05:52:14 +00:00
|
|
|
if (const auto* def = db->GetFunc(sym).AnyDef()) {
|
|
|
|
return {def->comments, !def->hover.empty()
|
|
|
|
? std::string_view(def->hover)
|
|
|
|
: std::string_view(def->detailed_name)};
|
|
|
|
}
|
2017-12-19 05:20:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Var: {
|
2018-02-18 05:52:14 +00:00
|
|
|
if (const auto* def = db->GetVar(sym).AnyDef()) {
|
|
|
|
return {def->comments, !def->hover.empty()
|
|
|
|
? std::string_view(def->hover)
|
|
|
|
: std::string_view(def->detailed_name)};
|
|
|
|
}
|
2017-12-19 05:20:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::File:
|
|
|
|
case SymbolKind::Invalid: {
|
|
|
|
assert(false && "unexpected");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 07:48:34 +00:00
|
|
|
return {"", ""};
|
2017-12-19 05:20:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct In_TextDocumentHover : public RequestMessage {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 04:39:44 +00:00
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentHover, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentHover);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
|
|
|
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
|
|
|
|
struct Result {
|
2017-12-26 05:26:03 +00:00
|
|
|
std::vector<lsMarkedString> contents;
|
2017-12-06 04:39:44 +00:00
|
|
|
optional<lsRange> range;
|
|
|
|
};
|
|
|
|
|
|
|
|
lsRequestId id;
|
2017-12-19 07:27:52 +00:00
|
|
|
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) {
|
2018-01-08 04:10:16 +00:00
|
|
|
REFLECT_MEMBER_START();
|
2017-12-19 07:54:00 +00:00
|
|
|
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
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_TextDocumentHover : BaseMessageHandler<In_TextDocumentHover> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
void Run(In_TextDocumentHover* request) override {
|
2017-12-06 03:32:33 +00:00
|
|
|
QueryFile* file;
|
2017-12-31 03:18:33 +00:00
|
|
|
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-10 06:51:58 +00:00
|
|
|
for (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;
|
|
|
|
|
2018-01-31 07:48:34 +00:00
|
|
|
std::pair<std::string_view, std::string_view> comments_hover =
|
2018-02-09 17:42:10 +00:00
|
|
|
GetCommentsAndHover(db, sym);
|
2018-01-31 07:48:34 +00:00
|
|
|
if (comments_hover.first.size() || comments_hover.second.size()) {
|
2017-12-19 07:27:52 +00:00
|
|
|
out.result = Out_TextDocumentHover::Result();
|
2018-01-31 07:48:34 +00:00
|
|
|
if (comments_hover.first.size()) {
|
|
|
|
out.result->contents.emplace_back(comments_hover.first);
|
2017-12-26 05:26:03 +00:00
|
|
|
}
|
|
|
|
if (comments_hover.second.size()) {
|
2018-01-31 07:48:34 +00:00
|
|
|
out.result->contents.emplace_back(lsMarkedString1{
|
|
|
|
std::string_view(file->def->language), comments_hover.second});
|
2017-12-26 05:26:03 +00:00
|
|
|
}
|
2017-12-19 07:27:52 +00:00
|
|
|
out.result->range = *ls_range;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
QueueManager::WriteStdout(kMethodType, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentHover);
|
2017-12-19 07:10:42 +00:00
|
|
|
} // namespace
|