Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 78175cf

Browse files
authored
Merge pull request #519 from philip-alldredge/ack_responses
ack_responses
2 parents 7ce6aca + fd1a936 commit 78175cf

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/server/mod.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub fn run_server(analysis: Arc<AnalysisHost>, vfs: Arc<Vfs>) {
4545
#[derive(Debug, Serialize)]
4646
pub struct Ack;
4747

48+
#[derive(Debug)]
49+
pub struct NoResponse;
50+
4851
#[derive(Debug, Serialize, PartialEq)]
4952
pub struct NoParams;
5053

@@ -56,6 +59,21 @@ impl<'de> Deserialize<'de> for NoParams {
5659
}
5760
}
5861

62+
pub trait Response {
63+
fn send<O: Output>(&self, id: usize, out: O);
64+
}
65+
66+
impl Response for NoResponse {
67+
fn send<O: Output>(&self, _id: usize, _out: O) {
68+
}
69+
}
70+
71+
impl<R: ::serde::Serialize + fmt::Debug> Response for R {
72+
fn send<O: Output>(&self, id: usize, out: O) {
73+
out.success(id, &self);
74+
}
75+
}
76+
5977
pub trait Action<'a> {
6078
type Params: serde::Serialize + for<'de> ::serde::Deserialize<'de>;
6179
const METHOD: &'static str;
@@ -68,7 +86,7 @@ pub trait NotificationAction<'a>: Action<'a> {
6886
}
6987

7088
pub trait RequestAction<'a>: Action<'a> {
71-
type Response: ::serde::Serialize + fmt::Debug;
89+
type Response: Response + fmt::Debug;
7290

7391
fn handle<O: Output>(&mut self, id: usize, params: Self::Params, ctx: &mut ActionContext, out: O) -> Result<Self::Response, ()>;
7492
}
@@ -90,9 +108,7 @@ impl<'a, A: RequestAction<'a>> Request<'a, A> {
90108
fn dispatch<O: Output>(self, state: &'a mut LsState, ctx: &mut ActionContext, out: O) -> Result<A::Response, ()> {
91109
let mut action = A::new(state);
92110
let result = action.handle(self.id, self.params, ctx, out.clone())?;
93-
if ::std::mem::size_of::<A::Response>() > 0 {
94-
out.success(self.id, &result);
95-
}
111+
result.send(self.id, out);
96112
Ok(result)
97113
}
98114
}
@@ -196,8 +212,8 @@ impl<'a> Action<'a> for InitializeRequest {
196212
}
197213

198214
impl<'a> RequestAction<'a> for InitializeRequest {
199-
type Response = ();
200-
fn handle<O: Output>(&mut self, id: usize, params: Self::Params, ctx: &mut ActionContext, out: O) -> Result<(), ()> {
215+
type Response = NoResponse;
216+
fn handle<O: Output>(&mut self, id: usize, params: Self::Params, ctx: &mut ActionContext, out: O) -> Result<NoResponse, ()> {
201217
let init_options: InitializationOptions = params
202218
.initialization_options
203219
.as_ref()
@@ -240,7 +256,7 @@ impl<'a> RequestAction<'a> for InitializeRequest {
240256
let root_path = params.root_path.as_ref().map(PathBuf::from).expect("No root path");
241257
ctx.init(root_path, &init_options, out);
242258

243-
Ok(())
259+
Ok(NoResponse)
244260
}
245261
}
246262

src/test/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod harness;
1616
use analysis;
1717
use actions::requests;
1818
use config::{Config, Inferrable};
19-
use server::{self as ls_server, Request};
19+
use server::{self as ls_server, Request, ShutdownRequest, NoParams};
2020
use jsonrpc_core;
2121
use vfs;
2222

@@ -65,6 +65,31 @@ pub fn request<'a, T: ls_server::RequestAction<'a>>(id: usize, params: T::Params
6565
}
6666
}
6767

68+
#[test]
69+
fn test_shutdown() {
70+
let mut env = Environment::new("common");
71+
72+
let root_path = env.cache.abs_path(Path::new("."));
73+
74+
let messages = vec![
75+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
76+
request::<ShutdownRequest>(1, NoParams).to_string(),
77+
];
78+
79+
let (mut server, results) = env.mock_server(messages);
80+
// Initialise and build.
81+
assert_eq!(ls_server::LsService::handle_message(&mut server),
82+
ls_server::ServerStateChange::Continue);
83+
expect_messages(results.clone(), &[ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
84+
ExpectedMessage::new(None).expect_contains("beginBuild"),
85+
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),
86+
ExpectedMessage::new(None).expect_contains("diagnosticsEnd")]);
87+
88+
assert_eq!(ls_server::LsService::handle_message(&mut server),
89+
ls_server::ServerStateChange::Continue);
90+
expect_messages(results.clone(), &[&ExpectedMessage::new(Some(1))]);
91+
}
92+
6893
#[test]
6994
fn test_goto_def() {
7095
let mut env = Environment::new("common");

0 commit comments

Comments
 (0)