LLVM: include/llvm/Support/LSP/Transport.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef LLVM_SUPPORT_LSP_TRANSPORT_H
16#define LLVM_SUPPORT_LSP_TRANSPORT_H
17
27#include
28
29namespace llvm {
30
32 std::string InstrStr;
34 Os << Op;
35 return Os.str();
36}
37namespace lsp {
38class MessageHandler;
39
40
41
42
43
44
51
52
54public:
58
61
62
69
70private:
71
73};
74
75
77public:
81
84
87
88private:
89 std::FILE *In;
90};
91
92
93
95public:
97 bool PrettyOutput = false)
98 : In(std::move(In)), Out(Out), PrettyOutput(PrettyOutput) {}
99
102 bool PrettyOutput = false)
104 PrettyOutput(PrettyOutput) {}
105
106
112
113
115
116private:
117
119
121
122private:
123
124 std::unique_ptr In;
126
128
129 bool PrettyOutput;
130};
131
132
133
134
135
136
137
138template
140
141
142
143template
145
146
147
148template
151
152
153
154
155template
158
159
161public:
163
167
168 template
171 T Result;
173 if (fromJSON(Raw, Result, Root))
174 return std::move(Result);
175
176
177 std::string Context;
180
181
183 llvm::formatv("failed to decode {0} {1}: {2}", PayloadName, PayloadKind,
186 }
187
188 template <typename Param, typename Result, typename ThisT>
197 return Reply(Parameter.takeError());
198 (ThisPtr->*Handler)(*Parameter, std::move(Reply));
199 };
200 }
201
202 template <typename Param, typename ThisT>
204 void (ThisT::*Handler)(const Param &)) {
205 NotificationHandlers[Method] = [Method, Handler,
212 Logger::error("JSON parsing error: {0}",
213 LspError.message.c_str());
214 }));
215 }
217 };
218 }
219
220
221 template
223 return [&, Method](const T &Params) {
224 std::lock_guardstd::mutex TransportLock(TransportOutputMutex);
227 };
228 }
229
230
231
232
233
234 template <typename Param, typename Result>
243 return Callback(std::move(Id), Value.takeError());
244
250
252 };
253
254 {
255 std::lock_guardstd::mutex Lock(ResponseHandlersMutex);
256 ResponseHandlers.insert(
257 {debugString(Id), std::make_pair(Method.str(), CallbackWrapper)});
258 }
259
260 std::lock_guardstd::mutex TransportLock(TransportOutputMutex);
263 };
264 }
265
266private:
267 template
269
272 MethodHandlers;
273
274
275
276 using ResponseHandlerTy =
277 std::pair<std::string, OutgoingRequestCallbackllvm::json::Value>;
278
280
281 std::mutex ResponseHandlersMutex;
282
284
285
286 std::mutex TransportOutputMutex;
287};
288
289}
290}
291
292#endif
This file defines the StringMap class.
#define LLVM_ABI_FOR_TEST
This file supports working with JSON data.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
This class represents success/failure for parsing-like operations that find it important to chain tog...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringRef - Represent a constant reference to a string, i.e.
The root is the trivial Path to the root value.
LLVM_ABI void printErrorContext(const Value &, llvm::raw_ostream &) const
Print the root value with the error shown inline as a comment.
LLVM_ABI Error getError() const
Returns the last error reported, or else a generic error.
A Value is an JSON value of unknown type.
Concrete implementation of the JSONTransportInput that reads from a file.
Definition Transport.h:76
LLVM_ABI_FOR_TEST LogicalResult readStandardMessage(std::string &Json) final
bool hasError() const final
Definition Transport.h:82
JSONTransportInputOverFile(std::FILE *In, JSONStreamStyle Style=JSONStreamStyle::Standard)
Definition Transport.h:78
bool isEndOfInput() const final
Definition Transport.h:83
LLVM_ABI_FOR_TEST LogicalResult readDelimitedMessage(std::string &Json) final
For lit tests we support a simplified syntax:
virtual bool hasError() const =0
virtual bool isEndOfInput() const =0
virtual LogicalResult readDelimitedMessage(std::string &Json)=0
LogicalResult readMessage(std::string &Json)
Read in a message from the input stream.
Definition Transport.h:63
virtual ~JSONTransportInput()=default
virtual LogicalResult readStandardMessage(std::string &Json)=0
JSONTransportInput(JSONStreamStyle Style=JSONStreamStyle::Standard)
Definition Transport.h:55
A transport class that performs the JSON-RPC communication with the LSP client.
Definition Transport.h:94
LLVM_ABI_FOR_TEST void reply(llvm::json::Value Id, llvm::Expected< llvm::json::Value > Result)
LLVM_ABI_FOR_TEST llvm::Error run(MessageHandler &Handler)
Start executing the JSON-RPC transport.
LLVM_ABI_FOR_TEST void notify(StringRef Method, llvm::json::Value Params)
The following methods are used to send a message to the LSP client.
JSONTransport(std::unique_ptr< JSONTransportInput > In, raw_ostream &Out, bool PrettyOutput=false)
Definition Transport.h:96
LLVM_ABI_FOR_TEST void call(StringRef Method, llvm::json::Value Params, llvm::json::Value Id)
JSONTransport(std::FILE *In, raw_ostream &Out, JSONStreamStyle Style=JSONStreamStyle::Standard, bool PrettyOutput=false)
Definition Transport.h:100
This class models an LSP error as an llvm::Error.
static void info(const char *Fmt, Ts &&...Vals)
A handler used to process the incoming transport messages.
Definition Transport.h:160
OutgoingRequest< Param > outgoingRequest(llvm::StringLiteral Method, OutgoingRequestCallback< Result > Callback)
Create an OutgoingRequest function that, when called, sends a request with the given method via the t...
Definition Transport.h:236
void notification(llvm::StringLiteral Method, ThisT *ThisPtr, void(ThisT::*Handler)(const Param &))
Definition Transport.h:203
OutgoingNotification< T > outgoingNotification(llvm::StringLiteral Method)
Create an OutgoingNotification object used for the given method.
Definition Transport.h:222
bool onNotify(StringRef Method, llvm::json::Value Value)
void method(llvm::StringLiteral Method, ThisT *ThisPtr, void(ThisT::*Handler)(const Param &, Callback< Result >))
Definition Transport.h:189
MessageHandler(JSONTransport &Transport)
Definition Transport.h:162
static llvm::Expected< T > parse(const llvm::json::Value &Raw, StringRef PayloadName, StringRef PayloadKind)
Definition Transport.h:169
bool onCall(StringRef Method, llvm::json::Value Params, llvm::json::Value Id)
bool onReply(llvm::json::Value Id, llvm::Expected< llvm::json::Value > Result)
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
unique_function is a type-erasing functor similar to std::function.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback is a void function that accepts Expected.
Definition Transport.h:139
llvm::unique_function< void(const T &, llvm::json::Value Id)> OutgoingRequest
An OutgoingRequest is a function used for outgoing requests to send to the client.
Definition Transport.h:149
@ Parameter
An inlay hint that is for a parameter.
std::function< void(llvm::json::Value, llvm::Expected< T >)> OutgoingRequestCallback
An OutgoingRequestCallback is invoked when an outgoing request to the client receives a response in t...
Definition Transport.h:156
LLVM_ABI_FOR_TEST bool fromJSON(const llvm::json::Value &value, URIForFile &result, llvm::json::Path path)
JSONStreamStyle
The encoding style of the JSON-RPC messages (both input and output).
Definition Transport.h:45
@ Standard
Encoding per the LSP specification, with mandatory Content-Length header.
Definition Transport.h:47
@ Delimited
Messages are delimited by a '// --—' line. Comment lines start with //.
Definition Transport.h:49
llvm::unique_function< void(const T &)> OutgoingNotification
An OutgoingNotification is a function used for outgoing notifications send to the client.
Definition Transport.h:144
This is an optimization pass for GlobalISel generic memory operations.
support::detail::ErrorAdapter fmt_consume(Error &&Item)
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
static std::string debugString(T &&Op)
Definition Transport.h:31
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void consumeError(Error Err)
Consume a Error without doing anything.
Implement std::hash so that hash_code can be used in STL containers.
This class represents an efficient way to signal success or failure.