Refactor ReplyOnce; error if InitializeParams.rootUri is null

This commit is contained in:
Fangrui Song 2018-11-23 10:08:44 -08:00
parent 8f442c6c35
commit 741d8f2130
8 changed files with 27 additions and 61 deletions

View File

@ -50,15 +50,8 @@ enum class ErrorCode {
}; };
struct ResponseError { struct ResponseError {
// A number indicating the error type that occurred.
ErrorCode code; ErrorCode code;
// A string providing a short description of the error.
std::string message; std::string message;
// A Primitive or Structured value that contains additional
// information about the error. Can be omitted.
// std::optional<D> data;
}; };
constexpr char ccls_xref[] = "ccls.xref"; constexpr char ccls_xref[] = "ccls.xref";

View File

@ -195,21 +195,13 @@ void MessageHandler::Run(InMessage &msg) {
try { try {
it->second(reader, reply); it->second(reader, reply);
} catch (std::invalid_argument &ex) { } catch (std::invalid_argument &ex) {
ResponseError err; reply.Error(ErrorCode::InvalidParams,
err.code = ErrorCode::InvalidParams; "invalid params of " + msg.method + ": " + ex.what());
err.message = "invalid params of " + msg.method + ": " + ex.what();
reply.Error(err);
} catch (...) { } catch (...) {
ResponseError err; reply.Error(ErrorCode::InternalError, "failed to process " + msg.method);
err.code = ErrorCode::InternalError;
err.message = "failed to process " + msg.method;
reply.Error(err);
} }
} else { } else {
ResponseError err; reply.Error(ErrorCode::MethodNotFound, "unknown request " + msg.method);
err.code = ErrorCode::MethodNotFound;
err.message = "unknown request " + msg.method;
reply.Error(err);
} }
} else { } else {
auto it = method2notification.find(msg.method); auto it = method2notification.find(msg.method);
@ -248,14 +240,10 @@ QueryFile *MessageHandler::FindFile(ReplyOnce &reply,
has_entry |= folder.path2entry_index.count(path); has_entry |= folder.path2entry_index.count(path);
} }
ResponseError err; ResponseError err;
if (has_entry) { if (has_entry)
err.code = ErrorCode::ServerNotInitialized; reply.Error(ErrorCode::ServerNotInitialized, path + " is being indexed");
err.message = path + " is being indexed"; else
} else { reply.Error(ErrorCode::InternalError, "unable to find " + path);
err.code = ErrorCode::InternalError;
err.message = "unable to find " + path;
}
reply.Error(err);
} }
return ret; return ret;

View File

@ -182,17 +182,14 @@ MAKE_REFLECT_STRUCT(Diagnostic, range, severity, code, source, message);
MAKE_REFLECT_STRUCT(ShowMessageParam, type, message); MAKE_REFLECT_STRUCT(ShowMessageParam, type, message);
MAKE_REFLECT_TYPE_PROXY(LanguageId); MAKE_REFLECT_TYPE_PROXY(LanguageId);
// TODO llvm 8 llvm::unique_function
template <typename Res>
using Callback = std::function<void(Res*)>;
struct ReplyOnce { struct ReplyOnce {
RequestId id; RequestId id;
template <typename Res> void operator()(Res &result) const { template <typename Res> void operator()(Res &&result) const {
if (id.Valid()) if (id.Valid())
pipeline::Reply(id, [&](Writer &w) { Reflect(w, result); }); pipeline::Reply(id, [&](Writer &w) { Reflect(w, result); });
} }
template <typename Err> void Error(Err &err) const { void Error(ErrorCode code, std::string message) const {
ResponseError err{code, std::move(message)};
if (id.Valid()) if (id.Valid())
pipeline::ReplyError(id, [&](Writer &w) { Reflect(w, err); }); pipeline::ReplyError(id, [&](Writer &w) { Reflect(w, err); });
} }

View File

@ -204,9 +204,7 @@ void MessageHandler::ccls_call(Reader &reader, ReplyOnce &reply) {
if (param.hierarchy) if (param.hierarchy)
reply(result); reply(result);
else { else
auto out = FlattenHierarchy(result); reply(FlattenHierarchy(result));
reply(out);
}
} }
} // namespace ccls } // namespace ccls

View File

@ -149,10 +149,8 @@ void Inheritance(MessageHandler *m, Param &param, ReplyOnce &reply) {
if (param.hierarchy) if (param.hierarchy)
reply(result); reply(result);
else { else
auto out = FlattenHierarchy(result); reply(FlattenHierarchy(result));
reply(out);
}
} }
} // namespace } // namespace

View File

@ -297,9 +297,7 @@ void MessageHandler::ccls_member(Reader &reader, ReplyOnce &reply) {
if (param.hierarchy) if (param.hierarchy)
reply(result); reply(result);
else { else
auto out = FlattenHierarchy(result); reply(FlattenHierarchy(result));
reply(out);
}
} }
} // namespace ccls } // namespace ccls

View File

@ -280,10 +280,7 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {
// Send initialization before starting indexers, so we don't send a // Send initialization before starting indexers, so we don't send a
// status update too early. // status update too early.
{ reply(InitializeResult{});
InitializeResult result;
reply(result);
}
// Set project root. // Set project root.
EnsureEndsInSlash(project_path); EnsureEndsInSlash(project_path);
@ -330,8 +327,10 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {
void MessageHandler::initialize(Reader &reader, ReplyOnce &reply) { void MessageHandler::initialize(Reader &reader, ReplyOnce &reply) {
InitializeParam param; InitializeParam param;
Reflect(reader, param); Reflect(reader, param);
if (!param.rootUri) if (!param.rootUri) {
reply.Error(ErrorCode::InvalidRequest, "expected rootUri");
return; return;
}
Initialize(this, param, reply); Initialize(this, param, reply);
} }
@ -343,8 +342,7 @@ void StandaloneInitialize(MessageHandler &handler, const std::string &root) {
} }
void MessageHandler::shutdown(EmptyParam &, ReplyOnce &reply) { void MessageHandler::shutdown(EmptyParam &, ReplyOnce &reply) {
JsonNull result; reply(JsonNull{});
reply(result);
} }
void MessageHandler::exit(EmptyParam &) { void MessageHandler::exit(EmptyParam &) {

View File

@ -58,15 +58,11 @@ std::vector<TextEdit> ReplacementsToEdits(std::string_view code,
void Format(ReplyOnce &reply, WorkingFile *wfile, tooling::Range range) { void Format(ReplyOnce &reply, WorkingFile *wfile, tooling::Range range) {
std::string_view code = wfile->buffer_content; std::string_view code = wfile->buffer_content;
auto ReplsOrErr = FormatCode(code, wfile->filename, range); auto ReplsOrErr = FormatCode(code, wfile->filename, range);
if (ReplsOrErr) { if (ReplsOrErr)
auto result = ReplacementsToEdits(code, *ReplsOrErr); reply(ReplacementsToEdits(code, *ReplsOrErr));
reply(result); else
} else { reply.Error(ErrorCode::UnknownErrorCode,
ResponseError err; llvm::toString(ReplsOrErr.takeError()));
err.code = ErrorCode::UnknownErrorCode;
err.message = llvm::toString(ReplsOrErr.takeError());
reply.Error(err);
}
} }
} // namespace } // namespace