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 bool readStandardMessage(std::string &JSONString);
44
45 bool readDelimitedMessage(std::string &JSONString);
46
47 /// \brief Notify the inbound port to close the connection
48 void close() { Close = true; }
49
53
54 /// Read messages specified in LSP standard, and collect standard json string
55 /// into \p JSONString.
56 /// A Language Server Protocol message starts with a set of
57 /// HTTP headers, delimited by \r\n, and terminated by an empty line (\r\n).
58 bool readMessage(std::string &JSONString);
59
60 /// Dispatch messages to on{Notify,Call,Reply} ( \p Handlers)
61 /// Return values should be forwarded from \p Handlers
62 /// i.e. returns true to keep processing messages, or false to shut down.
63 bool dispatch(llvm::json::Value Message, MessageHandler &Handler);
64
66};
67
69private:
70 llvm::raw_ostream &Outs;
71
72 llvm::SmallVector<char, 0> OutputBuffer;
73
74 std::mutex Mutex;
75
76 bool Pretty = false;
77
78public:
79 explicit OutboundPort(bool Pretty = false)
80 : Outs(llvm::outs()), Pretty(Pretty) {}
81 OutboundPort(llvm::raw_ostream &Outs, bool Pretty = false)
82 : Outs(Outs), OutputBuffer(), Pretty(Pretty) {}
83 void notify(llvm::StringRef Method, llvm::json::Value Params);
84 void call(llvm::StringRef Method, llvm::json::Value Params,
85 llvm::json::Value ID);
86 void reply(llvm::json::Value ID, llvm::Expected<llvm::json::Value> Result);
87
88 void sendMessage(llvm::json::Value Message);
89};
90
91} // namespace lspserver
bool dispatch(llvm::json::Value Message, MessageHandler &Handler)
InboundPort(int In=STDIN_FILENO, JSONStreamStyle StreamStyle=JSONStreamStyle::Standard)
Definition Connection.h:50
JSONStreamStyle StreamStyle
Definition Connection.h:41
void close()
Notify the inbound port to close the connection.
Definition Connection.h:48
bool readStandardMessage(std::string &JSONString)
bool readMessage(std::string &JSONString)
bool readDelimitedMessage(std::string &JSONString)
void loop(MessageHandler &Handler)
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:81
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:79
Whether current platform treats paths case insensitively.
Definition Connection.h:11
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)