10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H 11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H 16 #include "clang/Basic/LLVM.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/StringMap.h" 19 #include "llvm/Support/JSON.h" 32 JSONOutput(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs,
33 Logger::Level MinLevel, llvm::raw_ostream *InputMirror =
nullptr,
35 :
Pretty(
Pretty), MinLevel(MinLevel), Outs(Outs), Logs(Logs),
36 InputMirror(InputMirror) {}
54 llvm::raw_ostream &Outs;
55 llvm::raw_ostream &Logs;
56 llvm::raw_ostream *InputMirror;
58 std::mutex StreamMutex;
63 void reply(llvm::json::Value &&Result);
69 void call(llvm::StringRef
Method, llvm::json::Value &&Params);
76 using Handler = std::function<void(const llvm::json::Value &)>;
81 : UnknownHandler(std::move(UnknownHandler)) {}
90 llvm::StringMap<Handler> Handlers;
JSONStreamStyle
Controls the way JSON-RPC messages are encoded (both input and output).
Encapsulates output and logs streams and provides thread-safe access to them.
void log(Level, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
JSONRPCDispatcher(Handler UnknownHandler)
Create a new JSONRPCDispatcher.
static const StringRef Message
JSONOutput(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs, Logger::Level MinLevel, llvm::raw_ostream *InputMirror=nullptr, bool Pretty=false)
void replyError(ErrorCode Code, const llvm::StringRef &Message)
Sends an error response to the client, and logs it.
Interface to allow custom logging in clangd.
void mirrorInput(const Twine &Message)
Mirror Message into InputMirror stream.
Messages are delimited by a '—' line. Comment lines start with #.
Main JSONRPC entry point.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::function< void(const llvm::json::Value &)> Handler
Encoding per the LSP specification, with mandatory Content-Length header.
void call(llvm::StringRef Method, llvm::json::Value &&Params)
Sends a request to the client.
void writeMessage(const llvm::json::Value &Result)
Emit a JSONRPC message.
void reply(llvm::json::Value &&Result)
Sends a successful reply.
void runLanguageServerLoop(std::FILE *In, JSONOutput &Out, JSONStreamStyle InputStyle, JSONRPCDispatcher &Dispatcher, bool &IsDone)
Parses input queries from LSP client (coming from In) and runs call method of Dispatcher for each que...
static llvm::cl::opt< JSONStreamStyle > InputStyle("input-style", llvm::cl::desc("Input JSON stream encoding"), llvm::cl::values(clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"), clEnumValN(JSONStreamStyle::Delimited, "delimited", "messages delimited by --- lines, with # comment support")), llvm::cl::init(JSONStreamStyle::Standard))