15 #ifndef MLIR_TOOLS_LSPSERVERSUPPORT_TRANSPORT_H
16 #define MLIR_TOOLS_LSPSERVERSUPPORT_TRANSPORT_H
22 #include "llvm/ADT/FunctionExtras.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/FormatAdapters.h"
26 #include "llvm/Support/JSON.h"
27 #include "llvm/Support/raw_ostream.h"
52 bool prettyOutput =
false)
53 : in(in), out(out), style(style), prettyOutput(prettyOutput) {}
56 void notify(StringRef method, llvm::json::Value params);
57 void call(StringRef method, llvm::json::Value params, llvm::json::Value
id);
65 bool handleMessage(llvm::json::Value msg,
MessageHandler &handler);
67 void sendMessage(llvm::json::Value msg);
72 : readStandardMessage(json);
100 template <
typename T>
109 bool onCall(StringRef
method, llvm::json::Value params, llvm::json::Value
id);
112 template <
typename T>
114 StringRef payloadName, StringRef payloadKind) {
116 llvm::json::Path::Root root;
118 return std::move(result);
122 llvm::raw_string_ostream os(context);
123 root.printErrorContext(raw, os);
126 return llvm::make_error<LSPError>(
127 llvm::formatv(
"failed to decode {0} {1}: {2}", payloadName, payloadKind,
128 fmt_consume(root.getError())),
132 template <
typename Param,
typename Result,
typename ThisT>
136 thisPtr](llvm::json::Value rawParams,
140 return reply(param.takeError());
141 (thisPtr->*handler)(*param, std::move(reply));
145 template <
typename Param,
typename ThisT>
147 void (ThisT::*handler)(
const Param &)) {
149 thisPtr](llvm::json::Value rawParams) {
152 return llvm::consumeError(param.takeError());
153 (thisPtr->*handler)(*param);
158 template <
typename T>
160 return [&,
method](
const T ¶ms) {
161 std::lock_guard<std::mutex> transportLock(transportOutputMutex);
168 template <
typename HandlerT>
169 using HandlerMap = llvm::StringMap<llvm::unique_function<HandlerT>>;
171 HandlerMap<void(llvm::json::Value)> notificationHandlers;
178 std::mutex transportOutputMutex;
A transport class that performs the JSON-RPC communication with the LSP client.
void notify(StringRef method, llvm::json::Value params)
The following methods are used to send a message to the LSP client.
JSONTransport(std::FILE *in, raw_ostream &out, JSONStreamStyle style=JSONStreamStyle::Standard, bool prettyOutput=false)
void call(StringRef method, llvm::json::Value params, llvm::json::Value id)
llvm::Error run(MessageHandler &handler)
Start executing the JSON-RPC transport.
void reply(llvm::json::Value id, llvm::Expected< llvm::json::Value > result)
static void info(const char *fmt, Ts &&...vals)
A handler used to process the incoming transport messages.
void notification(llvm::StringLiteral method, ThisT *thisPtr, void(ThisT::*handler)(const Param &))
bool onCall(StringRef method, llvm::json::Value params, llvm::json::Value id)
void method(llvm::StringLiteral method, ThisT *thisPtr, void(ThisT::*handler)(const Param &, Callback< Result >))
bool onReply(llvm::json::Value id, llvm::Expected< llvm::json::Value > result)
OutgoingNotification< T > outgoingNotification(llvm::StringLiteral method)
Create an OutgoingNotification object used for the given method.
static llvm::Expected< T > parse(const llvm::json::Value &raw, StringRef payloadName, StringRef payloadKind)
MessageHandler(JSONTransport &transport)
bool onNotify(StringRef method, llvm::json::Value value)
llvm::unique_function< void(const T &)> OutgoingNotification
An OutgoingNotification<T> is a function used for outgoing notifications send to the client.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
JSONStreamStyle
The encoding style of the JSON-RPC messages (both input and output).
@ Delimited
Messages are delimited by a '// --—' line. Comment lines start with //.
@ Standard
Encoding per the LSP specification, with mandatory Content-Length header.
bool fromJSON(const llvm::json::Value &value, URIForFile &result, llvm::json::Path path)
This header declares functions that assist transformations in the MemRef dialect.
This class represents an efficient way to signal success or failure.