Struct tower_lsp::LspServiceBuilder
source · pub struct LspServiceBuilder<S> { /* private fields */ }
Expand description
A builder to customize the properties of an LspService
.
To construct an LspServiceBuilder
, refer to LspService::build
.
Implementations§
source§impl<S: LanguageServer> LspServiceBuilder<S>
impl<S: LanguageServer> LspServiceBuilder<S>
sourcepub fn custom_method<P, R, F>(self, name: &'static str, callback: F) -> Selfwhere
P: FromParams,
R: IntoResponse,
F: for<'a> Method<&'a S, P, R> + Clone + Send + Sync + 'static,
pub fn custom_method<P, R, F>(self, name: &'static str, callback: F) -> Selfwhere
P: FromParams,
R: IntoResponse,
F: for<'a> Method<&'a S, P, R> + Clone + Send + Sync + 'static,
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
.
Handlers which return ()
are treated as notifications, while those which return
jsonrpc::Result<T>
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
. 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
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...
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 (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();
sourcepub fn finish(self) -> (LspService<S>, ClientSocket)
pub fn finish(self) -> (LspService<S>, ClientSocket)
Constructs the LspService
and returns it, along with a channel for server-to-client
communication.