1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use lsp_server::{Connection, Request, RequestId};
use lsp_types::{
notification::{self, Notification},
request, InitializeParams,
};
use std::{cell::RefCell, rc::Rc};
use crate::rpc_channel::RpcChannel;
use crate::vhdl_server::VHDLServer;
pub fn start() {
let (connection, io_threads) = Connection::stdio();
let connection_rpc = ConnectionRpcChannel::new(connection);
let mut server = VHDLServer::new(connection_rpc.clone());
connection_rpc.handle_initialization(&mut server);
connection_rpc.main_event_loop(server);
io_threads.join().unwrap();
}
#[derive(Clone)]
struct ConnectionRpcChannel {
connection: Rc<Connection>,
next_outgoing_request_id: Rc<RefCell<u64>>,
}
impl RpcChannel for ConnectionRpcChannel {
fn send_notification(
&self,
method: impl Into<String>,
notification: impl serde::ser::Serialize,
) {
let notification = lsp_server::Notification {
method: method.into(),
params: serde_json::to_value(notification).unwrap(),
};
trace!("Sending notification: {:?}", notification);
self.connection.sender.send(notification.into()).unwrap();
}
fn send_request(&self, method: impl Into<String>, params: impl serde::ser::Serialize) {
let request_id = self.next_outgoing_request_id.replace_with(|&mut id| id + 1);
let request = Request::new(
RequestId::from(request_id),
method.into(),
serde_json::to_value(params).unwrap(),
);
self.connection.sender.send(request.into()).unwrap();
}
}
impl ConnectionRpcChannel {
fn new(connection: Connection) -> Self {
Self {
connection: Rc::new(connection),
next_outgoing_request_id: Rc::new(RefCell::new(0)),
}
}
fn handle_initialization<T: RpcChannel + Clone>(&self, server: &mut VHDLServer<T>) {
let (initialize_id, initialize_params) = self.connection.initialize_start().unwrap();
let initialize_params =
serde_json::from_value::<InitializeParams>(initialize_params).unwrap();
let initialize_result = server.initialize_request(initialize_params);
self.connection
.initialize_finish(
initialize_id,
serde_json::to_value(initialize_result).unwrap(),
)
.unwrap();
server.initialized_notification();
}
fn main_event_loop<T: RpcChannel + Clone>(&self, mut server: VHDLServer<T>) {
info!("Language server initialized, waiting for messages ...");
while let Ok(message) = self.connection.receiver.recv() {
trace!("Received message: {:?}", message);
if let lsp_server::Message::Notification(notification) = &message {
if notification.method == notification::Exit::METHOD {
return;
}
}
match message {
lsp_server::Message::Request(request) => self.handle_request(&mut server, request),
lsp_server::Message::Notification(notification) => {
self.handle_notification(&mut server, notification);
}
lsp_server::Message::Response(response) => {
self.handle_response(&mut server, response)
}
};
}
}
fn send_response(&self, response: lsp_server::Response) {
trace!("Sending response: {:?}", response);
self.connection.sender.send(response.into()).unwrap();
}
fn handle_request<T: RpcChannel + Clone>(
&self,
server: &mut VHDLServer<T>,
request: lsp_server::Request,
) {
fn extract<R>(
request: lsp_server::Request,
) -> Result<(lsp_server::RequestId, R::Params), lsp_server::Request>
where
R: request::Request,
R::Params: serde::de::DeserializeOwned,
{
request.extract(R::METHOD)
}
trace!("Handling request: {:?}", request);
let request = match extract::<request::GotoDeclaration>(request) {
Ok((id, params)) => {
let result = server.text_document_declaration(¶ms);
self.send_response(lsp_server::Response::new_ok(id, result));
return;
}
Err(request) => request,
};
let request = match extract::<request::GotoDefinition>(request) {
Ok((id, params)) => {
let result = server.text_document_definition(¶ms);
self.send_response(lsp_server::Response::new_ok(id, result));
return;
}
Err(request) => request,
};
let request = match extract::<request::HoverRequest>(request) {
Ok((id, params)) => {
let result = server.text_document_hover(¶ms);
self.send_response(lsp_server::Response::new_ok(id, result));
return;
}
Err(request) => request,
};
let request = match extract::<request::References>(request) {
Ok((id, params)) => {
let result = server.text_document_references(¶ms);
self.send_response(lsp_server::Response::new_ok(id, result));
return;
}
Err(request) => request,
};
let request = match extract::<request::Shutdown>(request) {
Ok((id, _params)) => {
server.shutdown_server();
self.send_response(lsp_server::Response::new_ok(id, ()));
return;
}
Err(request) => request,
};
debug!("Unhandled request: {:?}", request);
self.send_response(lsp_server::Response::new_err(
request.id,
lsp_server::ErrorCode::MethodNotFound as i32,
"Unknown request".to_string(),
));
}
fn handle_notification<T: RpcChannel + Clone>(
&self,
server: &mut VHDLServer<T>,
notification: lsp_server::Notification,
) {
fn extract<N>(
notification: lsp_server::Notification,
) -> Result<N::Params, lsp_server::Notification>
where
N: notification::Notification,
N::Params: serde::de::DeserializeOwned,
{
notification.extract(N::METHOD)
}
trace!("Handling notification: {:?}", notification);
let notification = match extract::<notification::DidChangeTextDocument>(notification) {
Ok(params) => return server.text_document_did_change_notification(¶ms),
Err(notification) => notification,
};
let notification = match extract::<notification::DidOpenTextDocument>(notification) {
Ok(params) => return server.text_document_did_open_notification(¶ms),
Err(notification) => notification,
};
let notification = match extract::<notification::DidChangeWatchedFiles>(notification) {
Ok(params) => return server.workspace_did_change_watched_files(¶ms),
Err(notification) => notification,
};
let notification = match extract::<notification::Exit>(notification) {
Ok(_params) => return server.exit_notification(),
Err(notification) => notification,
};
if !notification.method.starts_with("$/") {
debug!("Unhandled notification: {:?}", notification);
}
}
fn handle_response<T: RpcChannel + Clone>(
&self,
_server: &mut VHDLServer<T>,
response: lsp_server::Response,
) {
trace!("Handling response: {:?}", response);
}
}