Skip to content

Commit

Permalink
Refactor ReplyOnce; error if InitializeParams.rootUri is null
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Nov 10, 2019
1 parent 8f442c6 commit 741d8f2
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 61 deletions.
7 changes: 0 additions & 7 deletions src/lsp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ enum class ErrorCode {
};

struct ResponseError {
// A number indicating the error type that occurred.
ErrorCode code;

// A string providing a short description of the error.
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";
Expand Down
28 changes: 8 additions & 20 deletions src/message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,13 @@ void MessageHandler::Run(InMessage &msg) {
try {
it->second(reader, reply);
} catch (std::invalid_argument &ex) {
ResponseError err;
err.code = ErrorCode::InvalidParams;
err.message = "invalid params of " + msg.method + ": " + ex.what();
reply.Error(err);
reply.Error(ErrorCode::InvalidParams,
"invalid params of " + msg.method + ": " + ex.what());
} catch (...) {
ResponseError err;
err.code = ErrorCode::InternalError;
err.message = "failed to process " + msg.method;
reply.Error(err);
reply.Error(ErrorCode::InternalError, "failed to process " + msg.method);
}
} else {
ResponseError err;
err.code = ErrorCode::MethodNotFound;
err.message = "unknown request " + msg.method;
reply.Error(err);
reply.Error(ErrorCode::MethodNotFound, "unknown request " + msg.method);
}
} else {
auto it = method2notification.find(msg.method);
Expand Down Expand Up @@ -248,14 +240,10 @@ QueryFile *MessageHandler::FindFile(ReplyOnce &reply,
has_entry |= folder.path2entry_index.count(path);
}
ResponseError err;
if (has_entry) {
err.code = ErrorCode::ServerNotInitialized;
err.message = path + " is being indexed";
} else {
err.code = ErrorCode::InternalError;
err.message = "unable to find " + path;
}
reply.Error(err);
if (has_entry)
reply.Error(ErrorCode::ServerNotInitialized, path + " is being indexed");
else
reply.Error(ErrorCode::InternalError, "unable to find " + path);
}

return ret;
Expand Down
9 changes: 3 additions & 6 deletions src/message_handler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,14 @@ MAKE_REFLECT_STRUCT(Diagnostic, range, severity, code, source, message);
MAKE_REFLECT_STRUCT(ShowMessageParam, type, message);
MAKE_REFLECT_TYPE_PROXY(LanguageId);

// TODO llvm 8 llvm::unique_function
template <typename Res>
using Callback = std::function<void(Res*)>;

struct ReplyOnce {
RequestId id;
template <typename Res> void operator()(Res &result) const {
template <typename Res> void operator()(Res &&result) const {
if (id.Valid())
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())
pipeline::ReplyError(id, [&](Writer &w) { Reflect(w, err); });
}
Expand Down
6 changes: 2 additions & 4 deletions src/messages/ccls_call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ void MessageHandler::ccls_call(Reader &reader, ReplyOnce &reply) {

if (param.hierarchy)
reply(result);
else {
auto out = FlattenHierarchy(result);
reply(out);
}
else
reply(FlattenHierarchy(result));
}
} // namespace ccls
6 changes: 2 additions & 4 deletions src/messages/ccls_inheritance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,8 @@ void Inheritance(MessageHandler *m, Param &param, ReplyOnce &reply) {

if (param.hierarchy)
reply(result);
else {
auto out = FlattenHierarchy(result);
reply(out);
}
else
reply(FlattenHierarchy(result));
}
} // namespace

Expand Down
6 changes: 2 additions & 4 deletions src/messages/ccls_member.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ void MessageHandler::ccls_member(Reader &reader, ReplyOnce &reply) {

if (param.hierarchy)
reply(result);
else {
auto out = FlattenHierarchy(result);
reply(out);
}
else
reply(FlattenHierarchy(result));
}
} // namespace ccls
12 changes: 5 additions & 7 deletions src/messages/initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,7 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {

// Send initialization before starting indexers, so we don't send a
// status update too early.
{
InitializeResult result;
reply(result);
}
reply(InitializeResult{});

// Set project root.
EnsureEndsInSlash(project_path);
Expand Down Expand Up @@ -330,8 +327,10 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {
void MessageHandler::initialize(Reader &reader, ReplyOnce &reply) {
InitializeParam param;
Reflect(reader, param);
if (!param.rootUri)
if (!param.rootUri) {
reply.Error(ErrorCode::InvalidRequest, "expected rootUri");
return;
}
Initialize(this, param, reply);
}

Expand All @@ -343,8 +342,7 @@ void StandaloneInitialize(MessageHandler &handler, const std::string &root) {
}

void MessageHandler::shutdown(EmptyParam &, ReplyOnce &reply) {
JsonNull result;
reply(result);
reply(JsonNull{});
}

void MessageHandler::exit(EmptyParam &) {
Expand Down
14 changes: 5 additions & 9 deletions src/messages/textDocument_formatting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ std::vector<TextEdit> ReplacementsToEdits(std::string_view code,
void Format(ReplyOnce &reply, WorkingFile *wfile, tooling::Range range) {
std::string_view code = wfile->buffer_content;
auto ReplsOrErr = FormatCode(code, wfile->filename, range);
if (ReplsOrErr) {
auto result = ReplacementsToEdits(code, *ReplsOrErr);
reply(result);
} else {
ResponseError err;
err.code = ErrorCode::UnknownErrorCode;
err.message = llvm::toString(ReplsOrErr.takeError());
reply.Error(err);
}
if (ReplsOrErr)
reply(ReplacementsToEdits(code, *ReplsOrErr));
else
reply.Error(ErrorCode::UnknownErrorCode,
llvm::toString(ReplsOrErr.takeError()));
}
} // namespace

Expand Down

0 comments on commit 741d8f2

Please sign in to comment.