diff --git a/README.md b/README.md index 36f1f36b..f2469109 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ struct Backend; #[tower_lsp::async_trait] impl LanguageServer for Backend { - fn initialize(&self, _: &Client, _: InitializeParams) -> Result { + async fn initialize(&self, _: &Client, _: InitializeParams) -> Result { Ok(InitializeResult::default()) } diff --git a/examples/custom_notification.rs b/examples/custom_notification.rs index 62fee980..b225f48e 100644 --- a/examples/custom_notification.rs +++ b/examples/custom_notification.rs @@ -33,7 +33,7 @@ struct Backend; #[tower_lsp::async_trait] impl LanguageServer for Backend { - fn initialize(&self, _: &Client, _: InitializeParams) -> Result { + async fn initialize(&self, _: &Client, _: InitializeParams) -> Result { Ok(InitializeResult { server_info: None, capabilities: ServerCapabilities { diff --git a/examples/server.rs b/examples/server.rs index 5d4e34e9..e1c83d1f 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -8,7 +8,7 @@ struct Backend; #[tower_lsp::async_trait] impl LanguageServer for Backend { - fn initialize(&self, _: &Client, _: InitializeParams) -> Result { + async fn initialize(&self, _: &Client, _: InitializeParams) -> Result { Ok(InitializeResult { server_info: None, capabilities: ServerCapabilities { diff --git a/src/delegate.rs b/src/delegate.rs index 41d89706..8dfb7c88 100644 --- a/src/delegate.rs +++ b/src/delegate.rs @@ -13,7 +13,7 @@ use futures::compat::Compat; use futures::future::{self, FutureExt, TryFutureExt}; use futures::Stream; use jsonrpc_core::types::{ErrorCode, Output, Params}; -use jsonrpc_core::{Error, Result as RpcResult}; +use jsonrpc_core::Error; use jsonrpc_derive::rpc; use log::{error, info}; use lsp_types::notification::{Notification, *}; @@ -51,7 +51,7 @@ pub trait LanguageServerCore { // Initialization #[rpc(name = "initialize", raw_params)] - fn initialize(&self, params: Params) -> RpcResult; + fn initialize(&self, params: Params) -> BoxFuture; #[rpc(name = "initialized", raw_params)] fn initialized(&self, params: Params); @@ -247,15 +247,22 @@ macro_rules! delegate_request { } impl LanguageServerCore for Delegate { - fn initialize(&self, params: Params) -> RpcResult { + fn initialize(&self, params: Params) -> BoxFuture { if !self.initialized.load(Ordering::SeqCst) { - let params: InitializeParams = params.parse()?; - let response = self.server.initialize(&self.client, params)?; - info!("language server initialized"); - self.initialized.store(true, Ordering::SeqCst); - Ok(response) + let server = self.server.clone(); + let initialized = self.initialized.clone(); + let client = self.client.clone(); + let fut = async move { + let params = params.parse()?; + let response = server.initialize(&client, params).await?; + info!("language server initialized"); + initialized.store(true, Ordering::SeqCst); + Ok(response) + }; + + fut.boxed().compat() } else { - Err(Error::invalid_request()) + future::err(Error::invalid_request()).boxed().compat() } } diff --git a/src/lib.rs b/src/lib.rs index 6e136fe4..c8615383 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ //! //! #[tower_lsp::async_trait] //! impl LanguageServer for Backend { -//! fn initialize(&self, _: &Client, _: InitializeParams) -> Result { +//! async fn initialize(&self, _: &Client, _: InitializeParams) -> Result { //! Ok(InitializeResult::default()) //! } //! @@ -100,7 +100,11 @@ pub trait LanguageServer: Send + Sync + 'static { /// /// This method is guaranteed to only execute once. If the client sends this request to the /// server again, the server will respond with JSON-RPC error code `-32600` (invalid request). - fn initialize(&self, client: &Client, params: InitializeParams) -> Result; + async fn initialize( + &self, + client: &Client, + params: InitializeParams, + ) -> Result; /// The [`initialized`] notification is sent from the client to the server after the client /// received the result of the initialize request but before the client sends anything else. @@ -735,8 +739,12 @@ pub trait LanguageServer: Send + Sync + 'static { #[async_trait] impl LanguageServer for Box { - fn initialize(&self, client: &Client, params: InitializeParams) -> Result { - (**self).initialize(client, params) + async fn initialize( + &self, + client: &Client, + params: InitializeParams, + ) -> Result { + (**self).initialize(client, params).await } async fn initialized(&self, client: &Client, params: InitializedParams) { diff --git a/src/service.rs b/src/service.rs index 2aee0e24..efdeffe4 100644 --- a/src/service.rs +++ b/src/service.rs @@ -143,7 +143,7 @@ mod tests { #[async_trait] impl LanguageServer for Mock { - fn initialize(&self, _: &Client, _: InitializeParams) -> Result { + async fn initialize(&self, _: &Client, _: InitializeParams) -> Result { Ok(InitializeResult::default()) }