1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
//! Service abstraction for language servers.
pub use self::client::{Client, ClientSocket, RequestStream, ResponseSink};
pub(crate) use self::pending::Pending;
pub(crate) use self::state::{ServerState, State};
use std::fmt::{self, Debug, Display, Formatter};
use std::sync::Arc;
use std::task::{Context, Poll};
use futures::future::{self, BoxFuture, FutureExt};
use serde_json::Value;
use tower::Service;
use crate::jsonrpc::{
Error, ErrorCode, FromParams, IntoResponse, Method, Request, Response, Router,
};
use crate::LanguageServer;
pub(crate) mod layers;
mod client;
mod pending;
mod state;
/// Error that occurs when attempting to call the language server after it has already exited.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExitedError(());
impl std::error::Error for ExitedError {}
impl Display for ExitedError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("language server has exited")
}
}
/// Service abstraction for the Language Server Protocol.
///
/// This service takes an incoming JSON-RPC message as input and produces an outgoing message as
/// output. If the incoming message is a server notification or a client response, then the
/// corresponding response will be `None`.
///
/// This implements [`tower::Service`] in order to remain independent from the underlying transport
/// and to facilitate further abstraction with middleware.
///
/// Pending requests can be canceled by issuing a [`$/cancelRequest`] notification.
///
/// [`$/cancelRequest`]: https://microsoft.github.io/language-server-protocol/specification#cancelRequest
///
/// The service shuts down and stops serving requests after the [`exit`] notification is received.
///
/// [`exit`]: https://microsoft.github.io/language-server-protocol/specification#exit
#[derive(Debug)]
pub struct LspService<S> {
inner: Router<S, ExitedError>,
state: Arc<ServerState>,
}
impl<S: LanguageServer> LspService<S> {
/// Creates a new `LspService` with the given server backend, also returning a channel for
/// server-to-client communication.
pub fn new<F>(init: F) -> (Self, ClientSocket)
where
F: FnOnce(Client) -> S,
{
LspService::build(init).finish()
}
/// Starts building a new `LspService`.
///
/// Returns an `LspServiceBuilder`, which allows adding custom JSON-RPC methods to the server.
pub fn build<F>(init: F) -> LspServiceBuilder<S>
where
F: FnOnce(Client) -> S,
{
let state = Arc::new(ServerState::new());
let (client, socket) = Client::new(state.clone());
let inner = Router::new(init(client.clone()));
let pending = Arc::new(Pending::new());
LspServiceBuilder {
inner: crate::generated::register_lsp_methods(
inner,
state.clone(),
pending.clone(),
client,
),
state,
pending,
socket,
}
}
/// Returns a reference to the inner server.
pub fn inner(&self) -> &S {
self.inner.inner()
}
}
impl<S: LanguageServer> Service<Request> for LspService<S> {
type Response = Option<Response>;
type Error = ExitedError;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.state.get() {
State::Initializing => Poll::Pending,
State::Exited => Poll::Ready(Err(ExitedError(()))),
_ => self.inner.poll_ready(cx),
}
}
fn call(&mut self, req: Request) -> Self::Future {
if self.state.get() == State::Exited {
return future::err(ExitedError(())).boxed();
}
let fut = self.inner.call(req);
Box::pin(async move {
let response = fut.await?;
match response.as_ref().and_then(|res| res.error()) {
Some(Error {
code: ErrorCode::MethodNotFound,
data: Some(Value::String(m)),
..
}) if m.starts_with("$/") => Ok(None),
_ => Ok(response),
}
})
}
}
/// A builder to customize the properties of an `LspService`.
///
/// To construct an `LspServiceBuilder`, refer to [`LspService::build`].
pub struct LspServiceBuilder<S> {
inner: Router<S, ExitedError>,
state: Arc<ServerState>,
pending: Arc<Pending>,
socket: ClientSocket,
}
impl<S: LanguageServer> LspServiceBuilder<S> {
/// Defines a custom JSON-RPC request or notification with the given method `name` and handler.
///
/// # Handler varieties
///
/// Fundamentally, any inherent `async fn(&self)` method defined directly on the language
/// server backend could be considered a valid method handler.
///
/// Handlers may optionally include a single `params` argument. This argument may be of any
/// type that implements [`Serialize`](serde::Serialize).
///
/// Handlers which return `()` are treated as **notifications**, while those which return
/// [`jsonrpc::Result<T>`](crate::jsonrpc::Result) are treated as **requests**.
///
/// Similar to the `params` argument, the `T` in the `Result<T>` return values may be of any
/// type which implements [`DeserializeOwned`](serde::de::DeserializeOwned). Additionally, this
/// type _must_ be convertible into a [`serde_json::Value`] using [`serde_json::to_value`]. If
/// this latter constraint is not met, the client will receive a JSON-RPC error response with
/// code `-32603` (Internal Error) instead of the expected response.
///
/// # Examples
///
/// ```rust
/// use serde_json::{json, Value};
/// use tower_lsp::jsonrpc::Result;
/// use tower_lsp::lsp_types::*;
/// use tower_lsp::{LanguageServer, LspService};
///
/// struct Mock;
///
/// // Implementation of `LanguageServer` omitted...
/// # #[tower_lsp::async_trait]
/// # impl LanguageServer for Mock {
/// # async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
/// # Ok(InitializeResult::default())
/// # }
/// #
/// # async fn shutdown(&self) -> Result<()> {
/// # Ok(())
/// # }
/// # }
///
/// impl Mock {
/// async fn request(&self) -> Result<i32> {
/// Ok(123)
/// }
///
/// async fn request_params(&self, params: Vec<String>) -> Result<Value> {
/// Ok(json!({"num_elems":params.len()}))
/// }
///
/// async fn notification(&self) {
/// // ...
/// }
///
/// async fn notification_params(&self, params: Value) {
/// // ...
/// # let _ = params;
/// }
/// }
///
/// let (service, socket) = LspService::build(|_| Mock)
/// .custom_method("custom/request", Mock::request)
/// .custom_method("custom/requestParams", Mock::request_params)
/// .custom_method("custom/notification", Mock::notification)
/// .custom_method("custom/notificationParams", Mock::notification_params)
/// .finish();
/// ```
pub fn custom_method<P, R, F>(mut self, name: &'static str, callback: F) -> Self
where
P: FromParams,
R: IntoResponse,
F: for<'a> Method<&'a S, P, R> + Clone + Send + Sync + 'static,
{
let layer = layers::Normal::new(self.state.clone(), self.pending.clone());
self.inner.method(name, callback, layer);
self
}
/// Constructs the `LspService` and returns it, along with a channel for server-to-client
/// communication.
pub fn finish(self) -> (LspService<S>, ClientSocket) {
let LspServiceBuilder {
inner,
state,
socket,
..
} = self;
(LspService { inner, state }, socket)
}
}
impl<S: Debug> Debug for LspServiceBuilder<S> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("LspServiceBuilder")
.field("inner", &self.inner)
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use async_trait::async_trait;
use lsp_types::*;
use serde_json::json;
use tower::ServiceExt;
use super::*;
use crate::jsonrpc::Result;
#[derive(Debug)]
struct Mock;
#[async_trait]
impl LanguageServer for Mock {
async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult::default())
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
// This handler should never resolve...
async fn code_action_resolve(&self, _: CodeAction) -> Result<CodeAction> {
future::pending().await
}
}
impl Mock {
async fn custom_request(&self, params: i32) -> Result<i32> {
Ok(params)
}
}
fn initialize_request(id: i64) -> Request {
Request::build("initialize")
.params(json!({"capabilities":{}}))
.id(id)
.finish()
}
#[tokio::test(flavor = "current_thread")]
async fn initializes_only_once() {
let (mut service, _) = LspService::new(|_| Mock);
let request = initialize_request(1);
let response = service.ready().await.unwrap().call(request.clone()).await;
let ok = Response::from_ok(1.into(), json!({"capabilities":{}}));
assert_eq!(response, Ok(Some(ok)));
let response = service.ready().await.unwrap().call(request).await;
let err = Response::from_error(1.into(), Error::invalid_request());
assert_eq!(response, Ok(Some(err)));
}
#[tokio::test(flavor = "current_thread")]
async fn refuses_requests_after_shutdown() {
let (mut service, _) = LspService::new(|_| Mock);
let initialize = initialize_request(1);
let response = service.ready().await.unwrap().call(initialize).await;
let ok = Response::from_ok(1.into(), json!({"capabilities":{}}));
assert_eq!(response, Ok(Some(ok)));
let shutdown = Request::build("shutdown").id(1).finish();
let response = service.ready().await.unwrap().call(shutdown.clone()).await;
let ok = Response::from_ok(1.into(), json!(null));
assert_eq!(response, Ok(Some(ok)));
let response = service.ready().await.unwrap().call(shutdown).await;
let err = Response::from_error(1.into(), Error::invalid_request());
assert_eq!(response, Ok(Some(err)));
}
#[tokio::test(flavor = "current_thread")]
async fn exit_notification() {
let (mut service, _) = LspService::new(|_| Mock);
let exit = Request::build("exit").finish();
let response = service.ready().await.unwrap().call(exit.clone()).await;
assert_eq!(response, Ok(None));
let ready = future::poll_fn(|cx| service.poll_ready(cx)).await;
assert_eq!(ready, Err(ExitedError(())));
assert_eq!(service.call(exit).await, Err(ExitedError(())));
}
#[tokio::test(flavor = "current_thread")]
async fn cancels_pending_requests() {
let (mut service, _) = LspService::new(|_| Mock);
let initialize = initialize_request(1);
let response = service.ready().await.unwrap().call(initialize).await;
let ok = Response::from_ok(1.into(), json!({"capabilities":{}}));
assert_eq!(response, Ok(Some(ok)));
let pending_request = Request::build("codeAction/resolve")
.params(json!({"title":""}))
.id(1)
.finish();
let cancel_request = Request::build("$/cancelRequest")
.params(json!({"id":1i32}))
.finish();
let pending_fut = service.ready().await.unwrap().call(pending_request);
let cancel_fut = service.ready().await.unwrap().call(cancel_request);
let (pending_response, cancel_response) = futures::join!(pending_fut, cancel_fut);
let canceled = Response::from_error(1.into(), Error::request_cancelled());
assert_eq!(pending_response, Ok(Some(canceled)));
assert_eq!(cancel_response, Ok(None));
}
#[tokio::test(flavor = "current_thread")]
async fn serves_custom_requests() {
let (mut service, _) = LspService::build(|_| Mock)
.custom_method("custom", Mock::custom_request)
.finish();
let initialize = initialize_request(1);
let response = service.ready().await.unwrap().call(initialize).await;
let ok = Response::from_ok(1.into(), json!({"capabilities":{}}));
assert_eq!(response, Ok(Some(ok)));
let custom = Request::build("custom").params(123i32).id(1).finish();
let response = service.ready().await.unwrap().call(custom).await;
let ok = Response::from_ok(1.into(), json!(123i32));
assert_eq!(response, Ok(Some(ok)));
}
#[tokio::test(flavor = "current_thread")]
async fn get_inner() {
let (service, _) = LspService::build(|_| Mock).finish();
service
.inner()
.initialize(InitializeParams::default())
.await
.unwrap();
}
}