1 //! Utilities for LSP-related boilerplate code.
3 use crossbeam_channel::Sender;
4 use lsp_server::{Message, Notification, Request, RequestId};
6 use serde::{de::DeserializeOwned, Serialize};
10 typ: lsp_types::MessageType,
11 message: impl Into<String>,
12 sender: &Sender<Message>,
14 let message = message.into();
15 let params = lsp_types::ShowMessageParams { typ, message };
16 let not = notification_new::<lsp_types::notification::ShowMessage>(params);
17 sender.send(not.into()).unwrap();
20 pub(crate) fn is_canceled(e: &(dyn Error + 'static)) -> bool {
21 e.downcast_ref::<Canceled>().is_some()
24 pub(crate) fn notification_is<N: lsp_types::notification::Notification>(
25 notification: &Notification,
27 notification.method == N::METHOD
30 pub(crate) fn notification_cast<N>(notification: Notification) -> Result<N::Params, Notification>
32 N: lsp_types::notification::Notification,
33 N::Params: DeserializeOwned,
35 notification.extract(N::METHOD)
38 pub(crate) fn notification_new<N>(params: N::Params) -> Notification
40 N: lsp_types::notification::Notification,
43 Notification::new(N::METHOD.to_string(), params)
46 pub(crate) fn request_new<R>(id: RequestId, params: R::Params) -> Request
48 R: lsp_types::request::Request,
51 Request::new(id, R::METHOD.to_string(), params)