nixd
Loading...
Searching...
No Matches
LSPServer.cpp
Go to the documentation of this file.
5
6#include <llvm/ADT/FunctionExtras.h>
7#include <llvm/Support/Compiler.h>
8#include <llvm/Support/Error.h>
9#include <llvm/Support/JSON.h>
10
11#include <mutex>
12#include <stdexcept>
13
14namespace lspserver {
15
16void LSPServer::run() { In->loop(*this); }
17
18bool LSPServer::onNotify(llvm::StringRef Method, llvm::json::Value Params) {
19 log("<-- {0}", Method);
20 if (Method == "exit")
21 return false;
22 auto Handler = Registry.NotificationHandlers.find(Method);
23 if (Handler != Registry.NotificationHandlers.end()) {
24 Handler->second(std::move(Params));
25 } else {
26 log("unhandled notification {0}", Method);
27 }
28 return true;
29}
30
31bool LSPServer::onCall(llvm::StringRef Method, llvm::json::Value Params,
32 llvm::json::Value ID) {
33 log("<-- {0}({1})", Method, ID);
34 auto Handler = Registry.MethodHandlers.find(Method);
35 if (Handler != Registry.MethodHandlers.end())
36 Handler->second(std::move(Params),
37 [=, Method = std::string(Method),
38 this](llvm::Expected<llvm::json::Value> Response) mutable {
39 if (Response) {
40 log("--> reply:{0}({1})", Method, ID);
41 Out->reply(std::move(ID), std::move(Response));
42 } else {
43 llvm::Error Err = Response.takeError();
44 log("--> reply:{0}({1}) {2:ms}, error: {3}", Method, ID,
45 Err);
46 Out->reply(std::move(ID), std::move(Err));
47 }
48 });
49 else {
50 // An unknown request must be answered, not end the session: clients probe
51 // optional methods such as `textDocument/implementation`.
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(),
56 }
57 return true;
58}
59
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;
64
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);
73 }
74 } else {
75 throw std::logic_error("jsonrpc: not an integer message ID");
76 }
77 if (LLVM_UNLIKELY(!CB)) {
78 elog("received a reply with ID {0}, but there was no such call", ID);
79 // Ignore this error
80 return true;
81 }
82 // Invoke the callback outside of the critical zone, because we just do not
83 // need to lock PendingCalls.
84 (*CB)(std::move(Result));
85 return true;
86}
87
88int LSPServer::bindReply(Callback<llvm::json::Value> CB) {
89 std::lock_guard<std::mutex> _(PendingCallsLock);
90 int Ret = TopID++;
91 PendingCalls[Ret] = std::move(CB);
92
93 // Check the limit
94 if (PendingCalls.size() > MaxPendingCalls) {
95 auto Begin = PendingCalls.begin();
96 auto [ID, OldestCallback] =
97 std::tuple{Begin->first, std::move(Begin->second)};
98 OldestCallback(
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);
103 }
104 return Ret;
105}
106
107} // namespace lspserver
HandlerRegistry Registry
Definition LSPServer.h:55
Whether current platform treats paths case insensitively.
Definition Connection.h:11
llvm::unique_function< void(llvm::Expected< T >)> Callback
Definition Function.h:14
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
Definition Logger.h:70
void elog(const char *Fmt, Ts &&...Vals)
Definition Logger.h:52
void log(const char *Fmt, Ts &&...Vals)
Definition Logger.h:58
HandlerMap< void(JSON)> NotificationHandlers
Definition LSPBinder.h:47