9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_LSPBINDER_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_LSPBINDER_H
15#include "llvm/ADT/FunctionExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/Support/JSON.h"
36 using JSON = llvm::json::Value;
39 template <
typename HandlerT>
40 using HandlerMap = llvm::StringMap<llvm::unique_function<HandlerT>>;
60 template <
typename Param,
typename Result,
typename ThisT>
68 template <
typename Param,
typename ThisT>
70 void (ThisT::*Handler)(
const Param &));
76 template <
typename Param,
typename Result,
typename ThisT>
77 void command(llvm::StringLiteral Command, ThisT *This,
80 template <
typename P,
typename R>
100 template <
typename T>
101 static llvm::Expected<T> parse(
const llvm::json::Value &Raw,
102 llvm::StringRef PayloadName,
103 llvm::StringRef PayloadKind);
110llvm::Expected<T> LSPBinder::parse(
const llvm::json::Value &Raw,
111 llvm::StringRef PayloadName,
112 llvm::StringRef PayloadKind) {
114 llvm::json::Path::Root
Root;
116 elog(
"Failed to decode {0} {1}: {2}", PayloadName, PayloadKind,
121 Root.printErrorContext(Raw,
OS);
124 return llvm::make_error<LSPError>(
125 llvm::formatv(
"failed to decode {0} {1}: {2}", PayloadName, PayloadKind,
126 fmt_consume(
Root.getError())),
129 return std::move(Result);
132template <
typename Param,
typename Result,
typename ThisT>
134 void (ThisT::*Handler)(
const Param &,
138 auto P = LSPBinder::parse<Param>(RawParams,
Method,
"request");
140 return Reply(P.takeError());
141 (This->*Handler)(*P, std::move(Reply));
145template <
typename Param,
typename ThisT>
147 void (ThisT::*Handler)(
const Param &)) {
148 Raw.NotificationHandlers[
Method] = [
Method, Handler, This](
JSON RawParams) {
149 llvm::Expected<Param> P =
150 LSPBinder::parse<Param>(RawParams,
Method,
"request");
152 return llvm::consumeError(P.takeError());
153 (This->*Handler)(*P);
157template <
typename Param,
typename Result,
typename ThisT>
159 void (ThisT::*Handler)(
const Param &,
163 auto P = LSPBinder::parse<Param>(RawParams,
Method,
"command");
165 return Reply(P.takeError());
166 (This->*Handler)(*P, std::move(Reply));
171 llvm::StringLiteral
Method;
191 llvm::StringLiteral
Method;
198 template <
typename Request,
typename Response>
205 Method](llvm::Expected<JSON> RawRsp)
mutable {
207 return Reply(RawRsp.takeError());
208 Reply(LSPBinder::parse<Response>(std::move(*RawRsp),
Method,
CompiledFragmentImpl & Out
llvm::raw_string_ostream OS
A context is an immutable container for per-request data that must be propagated through layers that ...
static const Context & current()
Returns the context for the current thread, creating it if needed.
virtual void callMethod(llvm::StringRef Method, JSON Params, Callback< JSON > Reply)=0
virtual ~RawOutgoing()=default
virtual void notify(llvm::StringRef Method, JSON Params)=0
LSPBinder collects a table of functions that handle LSP calls.
llvm::unique_function< void(const P &)> OutgoingNotification
LSPBinder(RawHandlers &Raw, RawOutgoing &Out)
UntypedOutgoingMethod outgoingMethod(llvm::StringLiteral Method)
Bind a function object to be used for outgoing method calls.
void method(llvm::StringLiteral Method, ThisT *This, void(ThisT::*Handler)(const Param &, Callback< Result >))
Bind a handler for an LSP method.
void notification(llvm::StringLiteral Method, ThisT *This, void(ThisT::*Handler)(const Param &))
Bind a handler for an LSP notification.
UntypedOutgoingNotification outgoingNotification(llvm::StringLiteral Method)
Bind a function object to be used for outgoing notifications.
llvm::unique_function< void(const P &, Callback< R >)> OutgoingMethod
void command(llvm::StringLiteral Command, ThisT *This, void(ThisT::*Handler)(const Param &, Callback< Result >))
Bind a handler for an LSP command.
void vlog(const char *Fmt, Ts &&... Vals)
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
void elog(const char *Fmt, Ts &&... Vals)
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request, llvm::json::Path P)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
HandlerMap< void(JSON)> NotificationHandlers
llvm::StringMap< llvm::unique_function< HandlerT > > HandlerMap
HandlerMap< void(JSON, Callback< JSON >)> CommandHandlers
HandlerMap< void(JSON, Callback< JSON >)> MethodHandlers