Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LanguageServer::initialize() asynchronous #182

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Backend;

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
async fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult::default())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Backend;

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
async fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
Expand Down
2 changes: 1 addition & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Backend;

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
async fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
Expand Down
25 changes: 16 additions & 9 deletions src/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, *};
Expand Down Expand Up @@ -51,7 +51,7 @@ pub trait LanguageServerCore {
// Initialization

#[rpc(name = "initialize", raw_params)]
fn initialize(&self, params: Params) -> RpcResult<InitializeResult>;
fn initialize(&self, params: Params) -> BoxFuture<InitializeResult>;

#[rpc(name = "initialized", raw_params)]
fn initialized(&self, params: Params);
Expand Down Expand Up @@ -247,15 +247,22 @@ macro_rules! delegate_request {
}

impl<T: LanguageServer> LanguageServerCore for Delegate<T> {
fn initialize(&self, params: Params) -> RpcResult<InitializeResult> {
fn initialize(&self, params: Params) -> BoxFuture<InitializeResult> {
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()
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//!
//! #[tower_lsp::async_trait]
//! impl LanguageServer for Backend {
//! fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
//! async fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
//! Ok(InitializeResult::default())
//! }
//!
Expand Down Expand Up @@ -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<InitializeResult>;
async fn initialize(
&self,
client: &Client,
params: InitializeParams,
) -> Result<InitializeResult>;

/// 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.
Expand Down Expand Up @@ -735,8 +739,12 @@ pub trait LanguageServer: Send + Sync + 'static {

#[async_trait]
impl<S: ?Sized + LanguageServer> LanguageServer for Box<S> {
fn initialize(&self, client: &Client, params: InitializeParams) -> Result<InitializeResult> {
(**self).initialize(client, params)
async fn initialize(
&self,
client: &Client,
params: InitializeParams,
) -> Result<InitializeResult> {
(**self).initialize(client, params).await
}

async fn initialized(&self, client: &Client, params: InitializedParams) {
Expand Down
2 changes: 1 addition & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod tests {

#[async_trait]
impl LanguageServer for Mock {
fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
async fn initialize(&self, _: &Client, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult::default())
}

Expand Down