2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2017-12-23 16:01:43 +00:00
|
|
|
|
2017-12-24 02:22:22 +00:00
|
|
|
static std::string getHoverString(const optional<std::string>& hover,
|
|
|
|
const optional<std::string>& comments,
|
|
|
|
const std::string& detailed_name) {
|
|
|
|
// TODO: Properly format multi-line comments.
|
|
|
|
std::string ret;
|
|
|
|
if (comments) {
|
|
|
|
ret += *comments;
|
|
|
|
ret += '\n';
|
|
|
|
}
|
|
|
|
return ret + hover.value_or(detailed_name);
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:20:00 +00:00
|
|
|
std::string GetHoverForSymbol(QueryDatabase* db, const SymbolIdx& symbol) {
|
|
|
|
switch (symbol.kind) {
|
|
|
|
case SymbolKind::Type: {
|
|
|
|
QueryType& type = db->types[symbol.idx];
|
2017-12-24 02:22:22 +00:00
|
|
|
if (type.def)
|
|
|
|
return getHoverString(type.def->hover, type.def->comments, type.def->detailed_name);
|
2017-12-19 05:20:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Func: {
|
|
|
|
QueryFunc& func = db->funcs[symbol.idx];
|
2017-12-24 02:22:22 +00:00
|
|
|
if (func.def)
|
|
|
|
return getHoverString(func.def->hover, func.def->comments, func.def->detailed_name);
|
2017-12-19 05:20:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Var: {
|
|
|
|
QueryVar& var = db->vars[symbol.idx];
|
2017-12-24 02:22:22 +00:00
|
|
|
if (var.def)
|
|
|
|
return getHoverString(var.def->hover, var.def->comments, var.def->detailed_name);
|
2017-12-19 05:20:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::File:
|
|
|
|
case SymbolKind::Invalid: {
|
|
|
|
assert(false && "unexpected");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Ipc_TextDocumentHover : public IpcMessage<Ipc_TextDocumentHover> {
|
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentHover;
|
|
|
|
|
|
|
|
lsRequestId id;
|
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentHover, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentHover);
|
|
|
|
|
|
|
|
struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
|
|
|
|
struct Result {
|
|
|
|
lsMarkedString contents;
|
|
|
|
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) {
|
2017-12-19 07:54:00 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct TextDocumentHoverHandler : BaseMessageHandler<Ipc_TextDocumentHover> {
|
|
|
|
void Run(Ipc_TextDocumentHover* request) override {
|
|
|
|
QueryFile* file;
|
|
|
|
if (!FindFileOrFail(db, request->id,
|
|
|
|
request->params.textDocument.uri.GetPath(), &file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
Out_TextDocumentHover out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
|
|
|
for (const SymbolRef& ref :
|
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.position)) {
|
|
|
|
// Found symbol. Return hover.
|
|
|
|
optional<lsRange> ls_range = GetLsRange(
|
|
|
|
working_files->GetFileByFilename(file->def->path), ref.loc.range);
|
|
|
|
if (!ls_range)
|
|
|
|
continue;
|
|
|
|
|
2017-12-19 07:27:52 +00:00
|
|
|
std::string hover = GetHoverForSymbol(db, ref.idx);
|
|
|
|
if (!hover.empty()) {
|
|
|
|
out.result = Out_TextDocumentHover::Result();
|
|
|
|
out.result->contents.value = hover;
|
|
|
|
out.result->contents.language = file->def->language;
|
|
|
|
out.result->range = *ls_range;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentHover, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentHoverHandler);
|
2017-12-19 07:10:42 +00:00
|
|
|
} // namespace
|