Allow registering handlers for typed LSP requests
This commit is contained in:
parent
a137abe2de
commit
2103eec463
1 changed files with 28 additions and 12 deletions
|
@ -356,40 +356,58 @@ impl LanguageServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_notification<T, F>(&mut self, mut f: F) -> Subscription
|
pub fn on_notification<T, F>(&mut self, f: F) -> Subscription
|
||||||
where
|
where
|
||||||
T: notification::Notification,
|
T: notification::Notification,
|
||||||
F: 'static + Send + Sync + FnMut(T::Params),
|
F: 'static + Send + Sync + FnMut(T::Params),
|
||||||
|
{
|
||||||
|
self.on_custom_notification(T::METHOD, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_request<T, F>(&mut self, f: F) -> Subscription
|
||||||
|
where
|
||||||
|
T: request::Request,
|
||||||
|
F: 'static + Send + Sync + FnMut(T::Params) -> Result<T::Result>,
|
||||||
|
{
|
||||||
|
self.on_custom_request(T::METHOD, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_custom_notification<Params, F>(
|
||||||
|
&mut self,
|
||||||
|
method: &'static str,
|
||||||
|
mut f: F,
|
||||||
|
) -> Subscription
|
||||||
|
where
|
||||||
|
F: 'static + Send + Sync + FnMut(Params),
|
||||||
|
Params: DeserializeOwned,
|
||||||
{
|
{
|
||||||
let prev_handler = self.notification_handlers.write().insert(
|
let prev_handler = self.notification_handlers.write().insert(
|
||||||
T::METHOD,
|
method,
|
||||||
Box::new(move |_, params, _| {
|
Box::new(move |_, params, _| {
|
||||||
let params = serde_json::from_str(params)?;
|
let params = serde_json::from_str(params)?;
|
||||||
f(params);
|
f(params);
|
||||||
Ok(())
|
Ok(())
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
prev_handler.is_none(),
|
prev_handler.is_none(),
|
||||||
"registered multiple handlers for the same notification"
|
"registered multiple handlers for the same LSP method"
|
||||||
);
|
);
|
||||||
|
|
||||||
Subscription {
|
Subscription {
|
||||||
method: T::METHOD,
|
method,
|
||||||
notification_handlers: self.notification_handlers.clone(),
|
notification_handlers: self.notification_handlers.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_custom_request<Params, Resp, F>(
|
pub fn on_custom_request<Params, Res, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
method: &'static str,
|
method: &'static str,
|
||||||
mut f: F,
|
mut f: F,
|
||||||
) -> Subscription
|
) -> Subscription
|
||||||
where
|
where
|
||||||
F: 'static + Send + Sync + FnMut(Params) -> Result<Resp>,
|
F: 'static + Send + Sync + FnMut(Params) -> Result<Res>,
|
||||||
Params: DeserializeOwned,
|
Params: DeserializeOwned,
|
||||||
Resp: Serialize,
|
Res: Serialize,
|
||||||
{
|
{
|
||||||
let prev_handler = self.notification_handlers.write().insert(
|
let prev_handler = self.notification_handlers.write().insert(
|
||||||
method,
|
method,
|
||||||
|
@ -403,12 +421,10 @@ impl LanguageServer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
prev_handler.is_none(),
|
prev_handler.is_none(),
|
||||||
"registered multiple handlers for the same notification"
|
"registered multiple handlers for the same LSP method"
|
||||||
);
|
);
|
||||||
|
|
||||||
Subscription {
|
Subscription {
|
||||||
method,
|
method,
|
||||||
notification_handlers: self.notification_handlers.clone(),
|
notification_handlers: self.notification_handlers.clone(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue