| 1 |
#pragma once |
| 2 |
#include <map> |
| 3 |
#include <functional> |
| 4 |
#include <memory> |
| 5 |
#include "MessageIssue.h" |
| 6 |
struct LspMessage; |
| 7 |
struct NotificationInMessage; |
| 8 |
struct lsBaseOutMessage; |
| 9 |
struct RequestInMessage; |
| 10 |
|
| 11 |
using GenericResponseHandler = std::function<bool(std::unique_ptr<LspMessage>)>; |
| 12 |
using GenericRequestHandler = std::function<bool(std::unique_ptr<LspMessage>)>; |
| 13 |
using GenericNotificationHandler = std::function<bool(std::unique_ptr<LspMessage>)>; |
| 14 |
|
| 15 |
class Endpoint |
| 16 |
{ |
| 17 |
public: |
| 18 |
virtual ~Endpoint() = default; |
| 19 |
virtual bool onRequest(std::unique_ptr<LspMessage>) = 0; |
| 20 |
virtual bool notify(std::unique_ptr<LspMessage>) = 0; |
| 21 |
|
| 22 |
virtual bool onResponse(std::string const&, std::unique_ptr<LspMessage>) = 0; |
| 23 |
virtual void registerRequestHandler(std::string const&, GenericResponseHandler) = 0; |
| 24 |
virtual void registerNotifyHandler(std::string const&, GenericNotificationHandler) = 0; |
| 25 |
}; |
| 26 |
|
| 27 |
class GenericEndpoint : public Endpoint |
| 28 |
{ |
| 29 |
|
| 30 |
public: |
| 31 |
GenericEndpoint(lsp::Log& l) : log(l) |
| 32 |
{ |
| 33 |
} |
| 34 |
bool notify(std::unique_ptr<LspMessage>) override; |
| 35 |
bool onResponse(std::string const&, std::unique_ptr<LspMessage>) override; |
| 36 |
|
| 37 |
bool onRequest(std::unique_ptr<LspMessage>) override; |
| 38 |
std::map<std::string, GenericRequestHandler> method2request; |
| 39 |
std::map<std::string, GenericResponseHandler> method2response; |
| 40 |
std::map<std::string, GenericNotificationHandler> method2notification; |
| 41 |
|
| 42 |
void registerRequestHandler(std::string const& method, GenericResponseHandler cb) override |
| 43 |
{ |
| 44 |
method2request[method] = cb; |
| 45 |
} |
| 46 |
|
| 47 |
void registerNotifyHandler(std::string const& method, GenericNotificationHandler cb) override |
| 48 |
{ |
| 49 |
method2notification[method] = cb; |
| 50 |
} |
| 51 |
lsp::Log& log; |
| 52 |
}; |