migrate gen-lsp-server to new crossbeam

This commit is contained in:
Aleksey Kladov 2018-12-30 23:02:26 +03:00
parent c0d1b17a4e
commit effc1eae8b
4 changed files with 26 additions and 10 deletions

14
Cargo.lock generated
View file

@ -176,6 +176,17 @@ dependencies = [
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "crossbeam-channel"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"crossbeam-utils 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "crossbeam-deque" name = "crossbeam-deque"
version = "0.2.0" version = "0.2.0"
@ -355,7 +366,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "gen_lsp_server" name = "gen_lsp_server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"languageserver-types 0.53.1 (registry+https://github.com/rust-lang/crates.io-index)", "languageserver-types 0.53.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1544,6 +1555,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7b85741761b7f160bc5e7e0c14986ef685b7f8bf9b7ad081c60c604bb4649827" "checksum crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7b85741761b7f160bc5e7e0c14986ef685b7f8bf9b7ad081c60c604bb4649827"
"checksum crossbeam-channel 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "137bc235f622ffaa0428e3854e24acb53291fc0b3ff6fb2cb75a8be6fb02f06b"
"checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3"
"checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150"
"checksum crossbeam-epoch 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2449aaa4ec7ef96e5fb24db16024b935df718e9ae1cec0a1e68feeca2efca7b8" "checksum crossbeam-epoch 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2449aaa4ec7ef96e5fb24db16024b935df718e9ae1cec0a1e68feeca2efca7b8"

View file

@ -13,4 +13,4 @@ log = "0.4.3"
failure = "0.1.2" failure = "0.1.2"
serde_json = "1.0.24" serde_json = "1.0.24"
serde = { version = "1.0.83", features = ["derive"] } serde = { version = "1.0.83", features = ["derive"] }
crossbeam-channel = "0.2.4" crossbeam-channel = "0.3.5"

View file

@ -95,7 +95,7 @@ pub fn run_server(
server(params, &receiver, &sender)?; server(params, &receiver, &sender)?;
log::info!("lsp server waiting for exit notification"); log::info!("lsp server waiting for exit notification");
match receiver.recv() { match receiver.recv() {
Some(RawMessage::Notification(n)) => n Ok(RawMessage::Notification(n)) => n
.cast::<Exit>() .cast::<Exit>()
.map_err(|n| format_err!("unexpected notification during shutdown: {:?}", n))?, .map_err(|n| format_err!("unexpected notification during shutdown: {:?}", n))?,
m => bail!("unexpected message during shutdown: {:?}", m), m => bail!("unexpected message during shutdown: {:?}", m),
@ -109,7 +109,7 @@ pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<R
match req.cast::<Shutdown>() { match req.cast::<Shutdown>() {
Ok((id, ())) => { Ok((id, ())) => {
let resp = RawResponse::ok::<Shutdown>(id, &()); let resp = RawResponse::ok::<Shutdown>(id, &());
sender.send(RawMessage::Response(resp)); let _ = sender.send(RawMessage::Response(resp));
None None
} }
Err(req) => Some(req), Err(req) => Some(req),
@ -122,16 +122,16 @@ fn initialize(
caps: ServerCapabilities, caps: ServerCapabilities,
) -> Result<InitializeParams> { ) -> Result<InitializeParams> {
let (id, params) = match receiver.recv() { let (id, params) = match receiver.recv() {
Some(RawMessage::Request(req)) => match req.cast::<Initialize>() { Ok(RawMessage::Request(req)) => match req.cast::<Initialize>() {
Err(req) => bail!("expected initialize request, got {:?}", req), Err(req) => bail!("expected initialize request, got {:?}", req),
Ok(req) => req, Ok(req) => req,
}, },
msg => bail!("expected initialize request, got {:?}", msg), msg => bail!("expected initialize request, got {:?}", msg),
}; };
let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps }); let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps });
sender.send(RawMessage::Response(resp)); sender.send(RawMessage::Response(resp)).unwrap();
match receiver.recv() { match receiver.recv() {
Some(RawMessage::Notification(n)) => { Ok(RawMessage::Notification(n)) => {
n.cast::<Initialized>() n.cast::<Initialized>()
.map_err(|_| format_err!("expected initialized notification"))?; .map_err(|_| format_err!("expected initialized notification"))?;
} }

View file

@ -9,11 +9,13 @@ use failure::bail;
use crate::{RawMessage, Result}; use crate::{RawMessage, Result};
pub fn stdio_transport() -> (Receiver<RawMessage>, Sender<RawMessage>, Threads) { pub fn stdio_transport() -> (Receiver<RawMessage>, Sender<RawMessage>, Threads) {
let (writer_sender, mut writer_receiver) = bounded::<RawMessage>(16); let (writer_sender, writer_receiver) = bounded::<RawMessage>(16);
let writer = thread::spawn(move || { let writer = thread::spawn(move || {
let stdout = stdout(); let stdout = stdout();
let mut stdout = stdout.lock(); let mut stdout = stdout.lock();
writer_receiver.try_for_each(|it| it.write(&mut stdout))?; writer_receiver
.into_iter()
.try_for_each(|it| it.write(&mut stdout))?;
Ok(()) Ok(())
}); });
let (reader_sender, reader_receiver) = bounded::<RawMessage>(16); let (reader_sender, reader_receiver) = bounded::<RawMessage>(16);
@ -21,7 +23,9 @@ pub fn stdio_transport() -> (Receiver<RawMessage>, Sender<RawMessage>, Threads)
let stdin = stdin(); let stdin = stdin();
let mut stdin = stdin.lock(); let mut stdin = stdin.lock();
while let Some(msg) = RawMessage::read(&mut stdin)? { while let Some(msg) = RawMessage::read(&mut stdin)? {
reader_sender.send(msg); if let Err(_) = reader_sender.send(msg) {
break;
}
} }
Ok(()) Ok(())
}); });