rust-analyzer/crates/gen_lsp_server/src/lib.rs

137 lines
4.9 KiB
Rust
Raw Normal View History

2018-11-11 18:28:55 +00:00
//! A language server scaffold, exposing a synchronous crossbeam-channel based API.
2018-10-09 09:55:23 +00:00
//! This crate handles protocol handshaking and parsing messages, while you
//! control the message dispatch loop yourself.
//!
//! Run with `RUST_LOG=gen_lsp_server=debug` to see all the messages.
2018-10-09 09:55:23 +00:00
//!
//! ```no_run
2019-06-15 07:53:37 +00:00
//! use std::error::Error;
2018-10-09 09:55:23 +00:00
//! use crossbeam_channel::{Sender, Receiver};
2019-01-14 10:55:56 +00:00
//! use lsp_types::{ServerCapabilities, InitializeParams, request::{GotoDefinition, GotoDefinitionResponse}};
2018-10-09 09:55:23 +00:00
//! use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse};
//!
2019-06-15 07:53:37 +00:00
//! fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
2018-10-09 09:55:23 +00:00
//! let (receiver, sender, io_threads) = stdio_transport();
//! run_server(
2018-10-09 09:55:23 +00:00
//! ServerCapabilities::default(),
//! receiver,
//! sender,
//! main_loop,
//! )?;
//! io_threads.join()?;
//! Ok(())
//! }
//!
//! fn main_loop(
//! _params: InitializeParams,
//! receiver: &Receiver<RawMessage>,
//! sender: &Sender<RawMessage>,
2019-06-15 07:53:37 +00:00
//! ) -> Result<(), Box<dyn Error + Send + Sync>> {
2018-10-09 09:55:23 +00:00
//! for msg in receiver {
//! match msg {
//! RawMessage::Request(req) => {
//! let req = match handle_shutdown(req, sender) {
//! None => return Ok(()),
//! Some(req) => req,
//! };
//! match req.cast::<GotoDefinition>() {
2018-10-09 09:55:23 +00:00
//! Ok((id, _params)) => {
//! let resp = RawResponse::ok::<GotoDefinition>(
//! id,
//! &Some(GotoDefinitionResponse::Array(Vec::new())),
//! );
//! sender.send(RawMessage::Response(resp))?;
2018-10-09 09:55:23 +00:00
//! continue;
//! },
//! Err(req) => req,
//! };
//! // ...
//! }
//! RawMessage::Response(_resp) => (),
//! RawMessage::Notification(_not) => (),
//! }
//! }
//! Ok(())
//! }
//! ```
2019-06-14 19:03:17 +00:00
use std::error::Error;
2018-09-01 13:18:02 +00:00
mod msg;
mod stdio;
use crossbeam_channel::{Receiver, Sender};
2019-01-14 10:55:56 +00:00
use lsp_types::{
notification::{Exit, Initialized},
2018-09-01 17:21:11 +00:00
request::{Initialize, Shutdown},
InitializeParams, InitializeResult, ServerCapabilities,
2018-09-01 13:18:02 +00:00
};
2019-06-15 07:24:02 +00:00
pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
2018-12-06 18:16:37 +00:00
pub use crate::{
msg::{ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, RawResponseError},
2018-09-01 13:18:02 +00:00
stdio::{stdio_transport, Threads},
};
2018-10-09 09:55:23 +00:00
/// Main entry point: runs the server from initialization to shutdown.
/// To attach server to standard input/output streams, use the `stdio_transport`
2018-10-09 09:55:23 +00:00
/// function to create corresponding `sender` and `receiver` pair.
///
/// `server` should use the `handle_shutdown` function to handle the `Shutdown`
2018-10-09 09:55:23 +00:00
/// request.
2018-09-01 13:18:02 +00:00
pub fn run_server(
caps: ServerCapabilities,
2018-10-09 09:55:23 +00:00
receiver: Receiver<RawMessage>,
sender: Sender<RawMessage>,
server: impl FnOnce(InitializeParams, &Receiver<RawMessage>, &Sender<RawMessage>) -> Result<()>,
2018-09-01 13:18:02 +00:00
) -> Result<()> {
2018-12-06 18:16:37 +00:00
log::info!("lsp server initializes");
2018-10-09 09:55:23 +00:00
let params = initialize(&receiver, &sender, caps)?;
2018-12-06 18:16:37 +00:00
log::info!("lsp server initialized, serving requests");
2018-10-09 09:55:23 +00:00
server(params, &receiver, &sender)?;
2018-12-06 18:16:37 +00:00
log::info!("lsp server waiting for exit notification");
2018-09-01 13:18:02 +00:00
match receiver.recv() {
Ok(RawMessage::Notification(n)) => n
.cast::<Exit>()
2019-06-14 19:03:17 +00:00
.map_err(|n| format!("unexpected notification during shutdown: {:?}", n))?,
m => Err(format!("unexpected message during shutdown: {:?}", m))?,
2018-09-01 13:18:02 +00:00
}
2018-12-06 18:16:37 +00:00
log::info!("lsp server shutdown complete");
2018-09-01 13:18:02 +00:00
Ok(())
}
/// If `req` is `Shutdown`, respond to it and return `None`, otherwise return `Some(req)`
2018-09-01 17:21:11 +00:00
pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> {
match req.cast::<Shutdown>() {
Ok((id, ())) => {
2018-09-02 12:18:43 +00:00
let resp = RawResponse::ok::<Shutdown>(id, &());
let _ = sender.send(RawMessage::Response(resp));
2018-09-01 17:21:11 +00:00
None
}
Err(req) => Some(req),
}
}
2018-09-01 13:18:02 +00:00
fn initialize(
2018-10-09 09:55:23 +00:00
receiver: &Receiver<RawMessage>,
sender: &Sender<RawMessage>,
2018-09-01 13:18:02 +00:00
caps: ServerCapabilities,
2018-09-05 18:38:43 +00:00
) -> Result<InitializeParams> {
let (id, params) = match receiver.recv() {
Ok(RawMessage::Request(req)) => match req.cast::<Initialize>() {
2019-06-14 19:03:17 +00:00
Err(req) => Err(format!("expected initialize request, got {:?}", req))?,
2018-09-05 18:38:43 +00:00
Ok(req) => req,
},
2019-06-14 19:03:17 +00:00
msg => Err(format!("expected initialize request, got {:?}", msg))?,
2018-09-01 13:18:02 +00:00
};
2018-09-02 12:18:43 +00:00
let resp = RawResponse::ok::<Initialize>(id, &InitializeResult { capabilities: caps });
sender.send(RawMessage::Response(resp)).unwrap();
2018-09-01 13:18:02 +00:00
match receiver.recv() {
Ok(RawMessage::Notification(n)) => {
2019-06-15 07:37:15 +00:00
n.cast::<Initialized>().map_err(|_| "expected initialized notification")?;
2018-09-01 13:18:02 +00:00
}
2019-07-04 17:26:44 +00:00
_ => Err("expected initialized notification".to_string())?,
2018-09-01 13:18:02 +00:00
}
2018-09-05 18:38:43 +00:00
Ok(params)
2018-09-01 13:18:02 +00:00
}