Skip to content

Commit b4ff5de

Browse files
committed
Simplify vhdl server
1 parent 38e6eb6 commit b4ff5de

File tree

3 files changed

+184
-286
lines changed

3 files changed

+184
-286
lines changed

vhdl_ls/src/rpc_channel.rs

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,49 @@
66

77
//! Contains the RpcChannel Traid and associated convenience functions
88
9-
use lsp_types::*;
10-
use vhdl_lang::{Message, MessageHandler};
9+
use serde_json::Value;
10+
use std::rc::Rc;
1111

1212
pub trait RpcChannel {
1313
/// Send notification to the client.
14-
fn send_notification(
15-
&self,
16-
method: impl Into<String>,
17-
notification: impl serde::ser::Serialize,
18-
);
14+
fn send_notification(&self, method: String, notification: Value);
1915

2016
/// Send request to the client.
21-
fn send_request(&self, method: impl Into<String>, params: impl serde::ser::Serialize);
22-
23-
fn window_show_message(&self, typ: MessageType, message: impl Into<String>) {
24-
self.send_notification(
25-
"window/showMessage",
26-
ShowMessageParams {
27-
typ,
28-
message: message.into(),
29-
},
30-
);
31-
}
32-
33-
fn window_log_message(&self, typ: MessageType, message: impl Into<String>) {
34-
self.send_notification(
35-
"window/logMessage",
36-
LogMessageParams {
37-
typ,
38-
message: message.into(),
39-
},
40-
);
41-
}
42-
43-
fn push_msg(&self, msg: Message) {
44-
if matches!(
45-
msg.message_type,
46-
vhdl_lang::MessageType::Warning | vhdl_lang::MessageType::Error
47-
) {
48-
self.window_show_message(to_lsp_message_type(&msg.message_type), msg.message.clone());
49-
}
50-
self.window_log_message(to_lsp_message_type(&msg.message_type), msg.message);
51-
}
17+
fn send_request(&self, method: String, params: Value);
5218
}
5319

54-
pub struct MessageChannel<'a, T: RpcChannel> {
55-
channel: &'a T,
20+
#[derive(Clone)]
21+
pub struct SharedRpcChannel {
22+
chan: Rc<dyn RpcChannel>,
5623
}
5724

58-
impl<'a, T: RpcChannel> MessageChannel<'a, T> {
59-
pub fn new(channel: &'a T) -> MessageChannel<'a, T> {
60-
MessageChannel { channel }
25+
impl SharedRpcChannel {
26+
pub fn new(chan: Rc<dyn RpcChannel>) -> Self {
27+
Self { chan }
6128
}
62-
}
6329

64-
impl<'a, T: RpcChannel> MessageHandler for MessageChannel<'a, T> {
65-
fn push(&mut self, message: Message) {
66-
self.channel.push_msg(message);
30+
/// Send notification to the client.
31+
pub fn send_notification(
32+
&self,
33+
method: impl Into<String>,
34+
notification: impl serde::ser::Serialize,
35+
) {
36+
self.chan
37+
.send_notification(method.into(), serde_json::to_value(&notification).unwrap())
6738
}
68-
}
6939

70-
fn to_lsp_message_type(message_type: &vhdl_lang::MessageType) -> MessageType {
71-
match message_type {
72-
vhdl_lang::MessageType::Error => MessageType::ERROR,
73-
vhdl_lang::MessageType::Warning => MessageType::WARNING,
74-
vhdl_lang::MessageType::Info => MessageType::INFO,
75-
vhdl_lang::MessageType::Log => MessageType::LOG,
40+
/// Send request to the client.
41+
pub fn send_request(&self, method: impl Into<String>, params: impl serde::ser::Serialize) {
42+
self.chan
43+
.send_request(method.into(), serde_json::to_value(&params).unwrap())
7644
}
7745
}
7846

7947
#[cfg(test)]
8048
pub mod test_support {
8149

8250
use pretty_assertions::assert_eq;
51+
use serde_json::Value;
8352
use std::cell::RefCell;
8453
use std::collections::VecDeque;
8554
use std::rc::Rc;
@@ -191,13 +160,7 @@ pub mod test_support {
191160
}
192161

193162
impl super::RpcChannel for RpcMock {
194-
fn send_notification(
195-
&self,
196-
method: impl Into<String>,
197-
notification: impl serde::ser::Serialize,
198-
) {
199-
let method = method.into();
200-
let notification = serde_json::to_value(notification).unwrap();
163+
fn send_notification(&self, method: String, notification: Value) {
201164
let expected = self
202165
.expected
203166
.borrow_mut()
@@ -240,9 +203,7 @@ pub mod test_support {
240203
}
241204
}
242205

243-
fn send_request(&self, method: impl Into<String>, params: impl serde::ser::Serialize) {
244-
let method = method.into();
245-
let params = serde_json::to_value(params).unwrap();
206+
fn send_request(&self, method: String, params: Value) {
246207
let expected = self
247208
.expected
248209
.borrow_mut()

vhdl_ls/src/stdio_server.rs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
1111
use lsp_server::{Connection, ExtractError, Request, RequestId};
1212
use lsp_types::{notification, request, InitializeParams};
13+
use serde_json::Value;
14+
1315
use std::{cell::RefCell, rc::Rc};
1416

15-
use crate::rpc_channel::RpcChannel;
17+
use crate::rpc_channel::{RpcChannel, SharedRpcChannel};
1618
use crate::vhdl_server::VHDLServer;
1719
use crate::vhdl_server::VHDLServerSettings;
1820

1921
/// Set up the IO channel for `stdio` and start the VHDL language server.
2022
pub fn start(settings: VHDLServerSettings) {
2123
let (connection, io_threads) = Connection::stdio();
22-
let connection_rpc = ConnectionRpcChannel::new(connection);
23-
let mut server = VHDLServer::new_settings(connection_rpc.clone(), settings);
24-
24+
let connection_rpc = Rc::new(ConnectionRpcChannel::new(connection));
25+
let rpc = SharedRpcChannel::new(connection_rpc.clone());
26+
let mut server = VHDLServer::new_settings(rpc, settings);
2527
connection_rpc.handle_initialization(&mut server);
2628
connection_rpc.main_event_loop(server);
2729

@@ -38,29 +40,18 @@ struct ConnectionRpcChannel {
3840

3941
impl RpcChannel for ConnectionRpcChannel {
4042
/// Send notification to the client.
41-
fn send_notification(
42-
&self,
43-
method: impl Into<String>,
44-
notification: impl serde::ser::Serialize,
45-
) {
46-
let notification = lsp_server::Notification {
47-
method: method.into(),
48-
params: serde_json::to_value(notification).unwrap(),
49-
};
43+
fn send_notification(&self, method: String, params: Value) {
44+
let notification = lsp_server::Notification { method, params };
5045

5146
trace!("Sending notification: {:?}", notification);
5247
self.connection.sender.send(notification.into()).unwrap();
5348
}
5449

5550
/// Send request to the client.
56-
fn send_request(&self, method: impl Into<String>, params: impl serde::ser::Serialize) {
51+
fn send_request(&self, method: String, params: Value) {
5752
let request_id = self.next_outgoing_request_id.replace_with(|&mut id| id + 1);
5853

59-
let request = Request::new(
60-
RequestId::from(request_id),
61-
method.into(),
62-
serde_json::to_value(params).unwrap(),
63-
);
54+
let request = Request::new(RequestId::from(request_id), method, params);
6455
self.connection.sender.send(request.into()).unwrap();
6556
}
6657
}
@@ -74,7 +65,7 @@ impl ConnectionRpcChannel {
7465
}
7566

7667
/// Wait for initialize request from the client and let the server respond to it.
77-
fn handle_initialization<T: RpcChannel + Clone>(&self, server: &mut VHDLServer<T>) {
68+
fn handle_initialization(&self, server: &mut VHDLServer) {
7869
let (initialize_id, initialize_params) = self.connection.initialize_start().unwrap();
7970
let initialize_params =
8071
serde_json::from_value::<InitializeParams>(initialize_params).unwrap();
@@ -90,7 +81,7 @@ impl ConnectionRpcChannel {
9081
}
9182

9283
/// Main event loop handling incoming messages from the client.
93-
fn main_event_loop<T: RpcChannel + Clone>(&self, mut server: VHDLServer<T>) {
84+
fn main_event_loop(&self, mut server: VHDLServer) {
9485
info!("Language server initialized, waiting for messages ...");
9586
while let Ok(message) = self.connection.receiver.recv() {
9687
trace!("Received message: {:?}", message);
@@ -113,11 +104,7 @@ impl ConnectionRpcChannel {
113104
}
114105

115106
/// Handle incoming requests from the client.
116-
fn handle_request<T: RpcChannel + Clone>(
117-
&self,
118-
server: &mut VHDLServer<T>,
119-
request: lsp_server::Request,
120-
) {
107+
fn handle_request(&self, server: &mut VHDLServer, request: lsp_server::Request) {
121108
fn extract<R>(
122109
request: lsp_server::Request,
123110
) -> Result<(lsp_server::RequestId, R::Params), lsp_server::Request>
@@ -185,11 +172,7 @@ impl ConnectionRpcChannel {
185172
}
186173

187174
/// Handle incoming notifications from the client.
188-
fn handle_notification<T: RpcChannel + Clone>(
189-
&self,
190-
server: &mut VHDLServer<T>,
191-
notification: lsp_server::Notification,
192-
) {
175+
fn handle_notification(&self, server: &mut VHDLServer, notification: lsp_server::Notification) {
193176
fn extract<N>(
194177
notification: lsp_server::Notification,
195178
) -> Result<N::Params, lsp_server::Notification>
@@ -233,11 +216,7 @@ impl ConnectionRpcChannel {
233216
}
234217

235218
/// Handle incoming responses (to requests sent by us) from the client.
236-
fn handle_response<T: RpcChannel + Clone>(
237-
&self,
238-
_server: &mut VHDLServer<T>,
239-
response: lsp_server::Response,
240-
) {
219+
fn handle_response(&self, _server: &mut VHDLServer, response: lsp_server::Response) {
241220
trace!("Handling response: {:?}", response);
242221
// We currently can ignore incoming responses as the implemented
243222
// outgoing requests do not require confirmation by the client.

0 commit comments

Comments
 (0)