diff options
-rw-r--r-- | crates/gen_lsp_server/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/server/src/main.rs | 8 | ||||
-rw-r--r-- | crates/server/src/main_loop/mod.rs | 60 | ||||
-rw-r--r-- | crates/server/src/req.rs | 20 |
4 files changed, 38 insertions, 54 deletions
diff --git a/crates/gen_lsp_server/src/lib.rs b/crates/gen_lsp_server/src/lib.rs index b47db0df4..476c12cc1 100644 --- a/crates/gen_lsp_server/src/lib.rs +++ b/crates/gen_lsp_server/src/lib.rs | |||
@@ -25,11 +25,9 @@ pub use { | |||
25 | stdio::{stdio_transport, Threads}, | 25 | stdio::{stdio_transport, Threads}, |
26 | }; | 26 | }; |
27 | 27 | ||
28 | pub type LspServer = fn(&mut Receiver<RawMessage>, &mut Sender<RawMessage>) -> Result<()>; | ||
29 | |||
30 | pub fn run_server( | 28 | pub fn run_server( |
31 | caps: ServerCapabilities, | 29 | caps: ServerCapabilities, |
32 | server: LspServer, | 30 | server: impl FnOnce(&mut Receiver<RawMessage>, &mut Sender<RawMessage>) -> Result<()>, |
33 | mut receiver: Receiver<RawMessage>, | 31 | mut receiver: Receiver<RawMessage>, |
34 | mut sender: Sender<RawMessage>, | 32 | mut sender: Sender<RawMessage>, |
35 | ) -> Result<()> { | 33 | ) -> Result<()> { |
diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 3e3bd44a1..ecc1f58f8 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs | |||
@@ -55,7 +55,13 @@ fn main() -> Result<()> { | |||
55 | 55 | ||
56 | fn main_inner() -> Result<()> { | 56 | fn main_inner() -> Result<()> { |
57 | let (receiver, sender, threads) = stdio_transport(); | 57 | let (receiver, sender, threads) = stdio_transport(); |
58 | run_server(caps::server_capabilities(), main_loop::main_loop, receiver, sender)?; | 58 | let root = ::std::env::current_dir()?; |
59 | run_server( | ||
60 | caps::server_capabilities(), | ||
61 | |r, s| main_loop::main_loop(root, r, s), | ||
62 | receiver, | ||
63 | sender, | ||
64 | )?; | ||
59 | info!("shutting down IO..."); | 65 | info!("shutting down IO..."); |
60 | threads.join()?; | 66 | threads.join()?; |
61 | info!("... IO is down"); | 67 | info!("... IO is down"); |
diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index 34d077805..75d65dcbf 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs | |||
@@ -2,6 +2,7 @@ mod handlers; | |||
2 | mod subscriptions; | 2 | mod subscriptions; |
3 | 3 | ||
4 | use std::{ | 4 | use std::{ |
5 | path::PathBuf, | ||
5 | collections::{HashMap}, | 6 | collections::{HashMap}, |
6 | }; | 7 | }; |
7 | 8 | ||
@@ -26,14 +27,13 @@ enum Task { | |||
26 | } | 27 | } |
27 | 28 | ||
28 | pub(super) fn main_loop( | 29 | pub(super) fn main_loop( |
29 | receriver: &mut Receiver<RawMessage>, | 30 | root: PathBuf, |
30 | sender: &mut Sender<RawMessage>, | 31 | msg_receriver: &mut Receiver<RawMessage>, |
32 | msg_sender: &mut Sender<RawMessage>, | ||
31 | ) -> Result<()> { | 33 | ) -> Result<()> { |
32 | let pool = ThreadPool::new(4); | 34 | let pool = ThreadPool::new(4); |
33 | let (task_sender, task_receiver) = bounded::<Task>(16); | 35 | let (task_sender, task_receiver) = bounded::<Task>(16); |
34 | let (fs_events_receiver, watcher) = vfs::watch(vec![ | 36 | let (fs_events_receiver, watcher) = vfs::watch(vec![root]); |
35 | ::std::env::current_dir()?, | ||
36 | ]); | ||
37 | 37 | ||
38 | info!("server initialized, serving requests"); | 38 | info!("server initialized, serving requests"); |
39 | let mut state = ServerWorldState::new(); | 39 | let mut state = ServerWorldState::new(); |
@@ -42,8 +42,8 @@ pub(super) fn main_loop( | |||
42 | let mut subs = Subscriptions::new(); | 42 | let mut subs = Subscriptions::new(); |
43 | main_loop_inner( | 43 | main_loop_inner( |
44 | &pool, | 44 | &pool, |
45 | receriver, | 45 | msg_receriver, |
46 | sender, | 46 | msg_sender, |
47 | task_receiver.clone(), | 47 | task_receiver.clone(), |
48 | task_sender, | 48 | task_sender, |
49 | fs_events_receiver, | 49 | fs_events_receiver, |
@@ -53,7 +53,7 @@ pub(super) fn main_loop( | |||
53 | )?; | 53 | )?; |
54 | 54 | ||
55 | info!("waiting for background jobs to finish..."); | 55 | info!("waiting for background jobs to finish..."); |
56 | task_receiver.for_each(drop); | 56 | task_receiver.for_each(|task| on_task(task, msg_sender, &mut pending_requests)); |
57 | pool.join(); | 57 | pool.join(); |
58 | info!("...background jobs have finished"); | 58 | info!("...background jobs have finished"); |
59 | 59 | ||
@@ -95,22 +95,8 @@ fn main_loop_inner( | |||
95 | }; | 95 | }; |
96 | let mut state_changed = false; | 96 | let mut state_changed = false; |
97 | match event { | 97 | match event { |
98 | Event::FsWatcherDead => { | 98 | Event::FsWatcherDead => fs_receiver = None, |
99 | fs_receiver = None; | 99 | Event::Task(task) => on_task(task, msg_sender, pending_requests), |
100 | } | ||
101 | Event::Task(task) => { | ||
102 | match task { | ||
103 | Task::Respond(response) => { | ||
104 | if let Some(handle) = pending_requests.remove(&response.id) { | ||
105 | assert!(handle.has_completed()); | ||
106 | } | ||
107 | msg_sender.send(RawMessage::Response(response)) | ||
108 | } | ||
109 | Task::Notify(n) => | ||
110 | msg_sender.send(RawMessage::Notification(n)), | ||
111 | } | ||
112 | continue; | ||
113 | } | ||
114 | Event::Fs(events) => { | 100 | Event::Fs(events) => { |
115 | trace!("fs change, {} events", events.len()); | 101 | trace!("fs change, {} events", events.len()); |
116 | state.apply_fs_changes(events); | 102 | state.apply_fs_changes(events); |
@@ -158,6 +144,23 @@ fn main_loop_inner( | |||
158 | } | 144 | } |
159 | } | 145 | } |
160 | 146 | ||
147 | fn on_task( | ||
148 | task: Task, | ||
149 | msg_sender: &mut Sender<RawMessage>, | ||
150 | pending_requests: &mut HashMap<u64, JobHandle>, | ||
151 | ) { | ||
152 | match task { | ||
153 | Task::Respond(response) => { | ||
154 | if let Some(handle) = pending_requests.remove(&response.id) { | ||
155 | assert!(handle.has_completed()); | ||
156 | } | ||
157 | msg_sender.send(RawMessage::Response(response)) | ||
158 | } | ||
159 | Task::Notify(n) => | ||
160 | msg_sender.send(RawMessage::Notification(n)), | ||
161 | } | ||
162 | } | ||
163 | |||
161 | fn on_request( | 164 | fn on_request( |
162 | world: &mut ServerWorldState, | 165 | world: &mut ServerWorldState, |
163 | pending_requests: &mut HashMap<u64, JobHandle>, | 166 | pending_requests: &mut HashMap<u64, JobHandle>, |
@@ -280,15 +283,12 @@ impl<'a> PoolDispatcher<'a> { | |||
280 | None => return Ok(self), | 283 | None => return Ok(self), |
281 | Some(req) => req, | 284 | Some(req) => req, |
282 | }; | 285 | }; |
283 | let world = self.world; | ||
284 | let sender = self.sender; | ||
285 | let pool = self.pool; | ||
286 | match req.cast::<R>() { | 286 | match req.cast::<R>() { |
287 | Ok((id, params)) => { | 287 | Ok((id, params)) => { |
288 | let (handle, token) = JobHandle::new(); | 288 | let (handle, token) = JobHandle::new(); |
289 | let world = world.snapshot(); | 289 | let world = self.world.snapshot(); |
290 | let sender = sender.clone(); | 290 | let sender = self.sender.clone(); |
291 | pool.execute(move || { | 291 | self.pool.execute(move || { |
292 | let resp = match f(world, params, token) { | 292 | let resp = match f(world, params, token) { |
293 | Ok(resp) => RawResponse::ok(id, resp), | 293 | Ok(resp) => RawResponse::ok(id, resp), |
294 | Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()), | 294 | Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()), |
diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index c6d2f2efb..f52127271 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs | |||
@@ -1,6 +1,4 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | |||
3 | use serde::{ser::Serialize, de::DeserializeOwned}; | ||
4 | use languageserver_types::{TextDocumentIdentifier, Range, Url, Position, Location}; | 2 | use languageserver_types::{TextDocumentIdentifier, Range, Url, Position, Location}; |
5 | use url_serde; | 3 | use url_serde; |
6 | 4 | ||
@@ -18,24 +16,6 @@ pub use languageserver_types::{ | |||
18 | TextDocumentEdit, | 16 | TextDocumentEdit, |
19 | }; | 17 | }; |
20 | 18 | ||
21 | |||
22 | pub trait ClientRequest: 'static { | ||
23 | type Params: DeserializeOwned + Send + 'static; | ||
24 | type Result: Serialize + Send + 'static; | ||
25 | const METHOD: &'static str; | ||
26 | } | ||
27 | |||
28 | impl<T> ClientRequest for T | ||
29 | where T: Request + 'static, | ||
30 | T::Params: DeserializeOwned + Send + 'static, | ||
31 | T::Result: Serialize + Send + 'static, | ||
32 | { | ||
33 | type Params = <T as Request>::Params; | ||
34 | type Result = <T as Request>::Result; | ||
35 | const METHOD: &'static str = <T as Request>::METHOD; | ||
36 | } | ||
37 | |||
38 | |||
39 | pub enum SyntaxTree {} | 19 | pub enum SyntaxTree {} |
40 | 20 | ||
41 | impl Request for SyntaxTree { | 21 | impl Request for SyntaxTree { |