6#include <llvm/ADT/FunctionExtras.h>
7#include <llvm/Support/Compiler.h>
8#include <llvm/Support/Error.h>
9#include <llvm/Support/JSON.h>
18bool LSPServer::onNotify(llvm::StringRef
Method, llvm::json::Value Params) {
24 Handler->second(std::move(Params));
26 log(
"unhandled notification {0}",
Method);
31bool LSPServer::onCall(llvm::StringRef
Method, llvm::json::Value Params,
32 llvm::json::Value ID) {
35 if (Handler !=
Registry.MethodHandlers.end())
36 Handler->second(std::move(Params),
38 this](llvm::Expected<llvm::json::Value> Response)
mutable {
41 Out->reply(std::move(ID), std::move(Response));
43 llvm::Error Err = Response.takeError();
44 log(
"--> reply:{0}({1}) {2:ms}, error: {3}",
Method, ID,
46 Out->reply(std::move(ID), std::move(Err));
52 log(
"--> reply:{0}({1}) method not found",
Method, ID);
53 Out->reply(std::move(ID),
54 llvm::make_error<LSPError>(
"method not found: " +
Method.str(),
60bool LSPServer::onReply(llvm::json::Value ID,
61 llvm::Expected<llvm::json::Value> Result) {
62 log(
"<-- reply({0})", ID);
63 std::optional<Callback<llvm::json::Value>> CB;
65 if (
auto OptI = ID.getAsInteger()) {
66 if (LLVM_UNLIKELY(*OptI > INT_MAX))
67 throw std::logic_error(
"jsonrpc: id is too large (> INT_MAX)");
68 std::lock_guard<std::mutex> Guard(PendingCallsLock);
69 auto I =
static_cast<int>(*OptI);
70 if (PendingCalls.contains(I)) {
71 CB = std::move(PendingCalls[I]);
72 PendingCalls.erase(I);
75 throw std::logic_error(
"jsonrpc: not an integer message ID");
77 if (LLVM_UNLIKELY(!CB)) {
78 elog(
"received a reply with ID {0}, but there was no such call", ID);
84 (*CB)(std::move(Result));
89 std::lock_guard<std::mutex>
_(PendingCallsLock);
91 PendingCalls[Ret] = std::move(CB);
94 if (PendingCalls.size() > MaxPendingCalls) {
95 auto Begin = PendingCalls.begin();
96 auto [ID, OldestCallback] =
97 std::tuple{Begin->first, std::move(Begin->second)};
99 error(
"failed to receive a client reply for request ({0})", ID));
100 elog(
"more than {0} outstanding LSP calls, forgetting about {1}",
101 MaxPendingCalls, ID);
102 PendingCalls.erase(Begin);
Whether current platform treats paths case insensitively.
llvm::unique_function< void(llvm::Expected< T >)> Callback
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
void elog(const char *Fmt, Ts &&...Vals)
void log(const char *Fmt, Ts &&...Vals)
HandlerMap< void(JSON)> NotificationHandlers