nixd
Toggle main menu visibility
Loading...
Searching...
No Matches
nixd
lspserver
include
lspserver
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
11
namespace
lspserver
{
12
13
enum class
JSONStreamStyle
{
14
// LSP standard, for real lsp server
15
Standard
,
16
// For testing.
17
LitTest
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.
23
class
MessageHandler
{
24
public
:
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
34
class
InboundPort
{
35
private
:
36
std::atomic<bool> Close;
37
38
public
:
39
int
In
;
40
41
JSONStreamStyle
StreamStyle
=
JSONStreamStyle::Standard
;
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,
55
JSONStreamStyle
StreamStyle
=
JSONStreamStyle::Standard
)
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
70
class
OutboundPort
{
71
private
:
72
llvm::raw_ostream &Outs;
73
74
llvm::SmallVector<char, 0> OutputBuffer;
75
76
std::mutex Mutex;
77
78
bool
Pretty =
false
;
79
80
public
:
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
lspserver::InboundPort::readMessage
llvm::Expected< llvm::json::Value > readMessage(std::string &Buffer)
Definition
Connection.cpp:306
lspserver::InboundPort::dispatch
bool dispatch(llvm::json::Value Message, MessageHandler &Handler)
Definition
Connection.cpp:126
lspserver::InboundPort::InboundPort
InboundPort(int In=STDIN_FILENO, JSONStreamStyle StreamStyle=JSONStreamStyle::Standard)
Definition
Connection.h:54
lspserver::InboundPort::StreamStyle
JSONStreamStyle StreamStyle
Definition
Connection.h:41
lspserver::InboundPort::close
void close()
Notify the inbound port to close the connection.
Definition
Connection.h:52
lspserver::InboundPort::readStandardMessage
llvm::Expected< llvm::json::Value > readStandardMessage(std::string &Buffer)
Definition
Connection.cpp:210
lspserver::InboundPort::loop
void loop(MessageHandler &Handler)
Definition
Connection.cpp:318
lspserver::InboundPort::readLitTestMessage
llvm::Expected< llvm::json::Value > readLitTestMessage(std::string &Buffer)
Read one message, expecting the input to be one of our Markdown lit-tests.
Definition
Connection.cpp:246
lspserver::InboundPort::In
int In
Definition
Connection.h:39
lspserver::MessageHandler
Definition
Connection.h:23
lspserver::MessageHandler::onReply
virtual bool onReply(llvm::json::Value ID, llvm::Expected< llvm::json::Value > Result)=0
lspserver::MessageHandler::onNotify
virtual bool onNotify(llvm::StringRef Method, llvm::json::Value)=0
lspserver::MessageHandler::onCall
virtual bool onCall(llvm::StringRef Method, llvm::json::Value Params, llvm::json::Value ID)=0
lspserver::MessageHandler::~MessageHandler
virtual ~MessageHandler()=default
lspserver::OutboundPort::OutboundPort
OutboundPort(llvm::raw_ostream &Outs, bool Pretty=false)
Definition
Connection.h:83
lspserver::OutboundPort::sendMessage
void sendMessage(llvm::json::Value Message)
Definition
Connection.cpp:113
lspserver::OutboundPort::notify
void notify(llvm::StringRef Method, llvm::json::Value Params)
Definition
Connection.cpp:80
lspserver::OutboundPort::reply
void reply(llvm::json::Value ID, llvm::Expected< llvm::json::Value > Result)
Definition
Connection.cpp:96
lspserver::OutboundPort::call
void call(llvm::StringRef Method, llvm::json::Value Params, llvm::json::Value ID)
Definition
Connection.cpp:87
lspserver::OutboundPort::OutboundPort
OutboundPort(bool Pretty=false)
Definition
Connection.h:81
llvm
Definition
lspserver/include/lspserver/Protocol.h:2034
lspserver
Whether current platform treats paths case insensitively.
Definition
Connection.h:11
lspserver::JSONStreamStyle
JSONStreamStyle
Definition
Connection.h:13
lspserver::JSONStreamStyle::LitTest
@ LitTest
Definition
Connection.h:17
lspserver::JSONStreamStyle::Standard
@ Standard
Definition
Connection.h:15
lspserver::CompletionItemKind::Method
@ Method
Definition
lspserver/include/lspserver/Protocol.h:398
Generated by
1.17.0