diff options
author | Aleksey Kladov <[email protected]> | 2020-06-25 06:56:47 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-25 06:56:47 +0100 |
commit | 76a530242a12f75e2a8456f952cef07e2d564f67 (patch) | |
tree | c172661c9be87bea10b796bffd32f4c32c65ab7a /crates/rust-analyzer/src/lsp_utils.rs | |
parent | 78e94e4570f09c8cbe1f8c6802df9b112ca37f08 (diff) | |
parent | 6e81c9a921b975be7f2efb927dab4f3cfd505ebc (diff) |
Merge branch 'Veetaha-feat/sync-branch'
Diffstat (limited to 'crates/rust-analyzer/src/lsp_utils.rs')
-rw-r--r-- | crates/rust-analyzer/src/lsp_utils.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/crates/rust-analyzer/src/lsp_utils.rs b/crates/rust-analyzer/src/lsp_utils.rs new file mode 100644 index 000000000..c79022797 --- /dev/null +++ b/crates/rust-analyzer/src/lsp_utils.rs | |||
@@ -0,0 +1,52 @@ | |||
1 | //! Utilities for LSP-related boilerplate code. | ||
2 | |||
3 | use crossbeam_channel::Sender; | ||
4 | use lsp_server::{Message, Notification, Request, RequestId}; | ||
5 | use ra_db::Canceled; | ||
6 | use serde::{de::DeserializeOwned, Serialize}; | ||
7 | use std::error::Error; | ||
8 | |||
9 | pub fn show_message( | ||
10 | typ: lsp_types::MessageType, | ||
11 | message: impl Into<String>, | ||
12 | sender: &Sender<Message>, | ||
13 | ) { | ||
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(); | ||
18 | } | ||
19 | |||
20 | pub(crate) fn is_canceled(e: &(dyn Error + 'static)) -> bool { | ||
21 | e.downcast_ref::<Canceled>().is_some() | ||
22 | } | ||
23 | |||
24 | pub(crate) fn notification_is<N: lsp_types::notification::Notification>( | ||
25 | notification: &Notification, | ||
26 | ) -> bool { | ||
27 | notification.method == N::METHOD | ||
28 | } | ||
29 | |||
30 | pub(crate) fn notification_cast<N>(notification: Notification) -> Result<N::Params, Notification> | ||
31 | where | ||
32 | N: lsp_types::notification::Notification, | ||
33 | N::Params: DeserializeOwned, | ||
34 | { | ||
35 | notification.extract(N::METHOD) | ||
36 | } | ||
37 | |||
38 | pub(crate) fn notification_new<N>(params: N::Params) -> Notification | ||
39 | where | ||
40 | N: lsp_types::notification::Notification, | ||
41 | N::Params: Serialize, | ||
42 | { | ||
43 | Notification::new(N::METHOD.to_string(), params) | ||
44 | } | ||
45 | |||
46 | pub(crate) fn request_new<R>(id: RequestId, params: R::Params) -> Request | ||
47 | where | ||
48 | R: lsp_types::request::Request, | ||
49 | R::Params: Serialize, | ||
50 | { | ||
51 | Request::new(id, R::METHOD.to_string(), params) | ||
52 | } | ||