nixd
Loading...
Searching...
No Matches
Connection.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdio>
4
5#include <atomic>
6#include <llvm/Support/JSON.h>
7#include <llvm/Support/raw_ostream.h>
8#include <mutex>
9#include <unistd.h>
10
11namespace lspserver {
12
13enum class JSONStreamStyle {
14 // LSP standard, for real lsp server
16 // For testing.
18};
19
20/// Parsed & classfied messages are dispatched to this handler class
21/// LSP Servers should inherit from this handler and dispatch
22/// notify/call/reply to implementations.
24public:
25 virtual ~MessageHandler() = default;
26 // Handler returns true to keep processing messages, or false to shut down.
27 virtual bool onNotify(llvm::StringRef Method, llvm::json::Value) = 0;
28 virtual bool onCall(llvm::StringRef Method, llvm::json::Value Params,
29 llvm::json::Value ID) = 0;
30 virtual bool onReply(llvm::json::Value ID,
31 llvm::Expected<llvm::json::Value> Result) = 0;
32};
33
35private:
36 std::atomic<bool> Close;
37
38public:
39 int In;
40
42
43 /// Read one message as specified in the LSP standard.
44 /// A Language Server Protocol message starts with a set of
45 /// HTTP headers, delimited by \r\n, and terminated by an empty line (\r\n).
46 llvm::Expected<llvm::json::Value> readStandardMessage(std::string &Buffer);
47
48 /// Read one message, expecting the input to be one of our Markdown lit-tests.
49 llvm::Expected<llvm::json::Value> readLitTestMessage(std::string &Buffer);
50
51 /// \brief Notify the inbound port to close the connection
52 void close() { Close = true; }
53
54 InboundPort(int In = STDIN_FILENO,
56 : Close(false), In(In), StreamStyle(StreamStyle) {};
57
58 /// Read one LSP message depending on the configured StreamStyle.
59 /// The parsed value is returned. The given \p Buffer is only used internally.
60 llvm::Expected<llvm::json::Value> readMessage(std::string &Buffer);
61
62 /// Dispatch messages to on{Notify,Call,Reply} ( \p Handlers)
63 /// Return values should be forwarded from \p Handlers
64 /// i.e. returns true to keep processing messages, or false to shut down.
65 bool dispatch(llvm::json::Value Message, MessageHandler &Handler);
66
67 void loop(MessageHandler &Handler);
68};
69
71private:
72 llvm::raw_ostream &Outs;
73
74 llvm::SmallVector<char, 0> OutputBuffer;
75
76 std::mutex Mutex;
77
78 bool Pretty = false;
79
80public:
81 explicit OutboundPort(bool Pretty = false)
82 : Outs(llvm::outs()), Pretty(Pretty) {}
83 OutboundPort(llvm::raw_ostream &Outs, bool Pretty = false)
84 : Outs(Outs), OutputBuffer(), Pretty(Pretty) {}
85 void notify(llvm::StringRef Method, llvm::json::Value Params);
86 void call(llvm::StringRef Method, llvm::json::Value Params,
87 llvm::json::Value ID);
88 void reply(llvm::json::Value ID, llvm::Expected<llvm::json::Value> Result);
89
90 void sendMessage(llvm::json::Value Message);
91};
92
93} // namespace lspserver
llvm::Expected< llvm::json::Value > readMessage(std::string &Buffer)
bool dispatch(llvm::json::Value Message, MessageHandler &Handler)
InboundPort(int In=STDIN_FILENO, JSONStreamStyle StreamStyle=JSONStreamStyle::Standard)
Definition Connection.h:54
JSONStreamStyle StreamStyle
Definition Connection.h:41
void close()
Notify the inbound port to close the connection.
Definition Connection.h:52
llvm::Expected< llvm::json::Value > readStandardMessage(std::string &Buffer)
void loop(MessageHandler &Handler)
llvm::Expected< llvm::json::Value > readLitTestMessage(std::string &Buffer)
Read one message, expecting the input to be one of our Markdown lit-tests.
virtual bool onReply(llvm::json::Value ID, llvm::Expected< llvm::json::Value > Result)=0
virtual bool onNotify(llvm::StringRef Method, llvm::json::Value)=0
virtual bool onCall(llvm::StringRef Method, llvm::json::Value Params, llvm::json::Value ID)=0
virtual ~MessageHandler()=default
OutboundPort(llvm::raw_ostream &Outs, bool Pretty=false)
Definition Connection.h:83
void sendMessage(llvm::json::Value Message)
void notify(llvm::StringRef Method, llvm::json::Value Params)
void reply(llvm::json::Value ID, llvm::Expected< llvm::json::Value > Result)
void call(llvm::StringRef Method, llvm::json::Value Params, llvm::json::Value ID)
OutboundPort(bool Pretty=false)
Definition Connection.h:81
Whether current platform treats paths case insensitively.
Definition Connection.h:11