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
|
use std::sync::{
atomic::{AtomicI32, Ordering},
Arc,
};
use anyhow::{bail, Result};
use crossbeam_channel::Sender;
use dashmap::DashMap;
use lsp_server::{ErrorCode, Message, Request, RequestId, Response};
use lsp_types::{notification::ShowMessage, MessageType, ShowMessageParams};
use serde::{de::DeserializeOwned, Serialize};
use crate::Options;
#[derive(Debug)]
struct RawClient {
sender: Sender<Message>,
next_id: AtomicI32,
pending: DashMap<RequestId, Sender<Response>>,
}
#[derive(Debug, Clone)]
pub struct LspClient {
raw: Arc<RawClient>,
}
impl LspClient {
pub fn new(sender: Sender<Message>) -> Self {
let raw = Arc::new(RawClient {
sender,
next_id: AtomicI32::new(1),
pending: DashMap::default(),
});
Self { raw }
}
pub fn send_notification<N>(&self, params: N::Params) -> Result<()>
where
N: lsp_types::notification::Notification,
N::Params: Serialize,
{
self.raw
.sender
.send(lsp_server::Notification::new(N::METHOD.to_string(), params).into())?;
Ok(())
}
pub fn send_request<R>(&self, params: R::Params) -> Result<R::Result>
where
R: lsp_types::request::Request,
R::Params: Serialize,
R::Result: DeserializeOwned,
{
let id = RequestId::from(self.raw.next_id.fetch_add(1, Ordering::SeqCst));
let (tx, rx) = crossbeam_channel::bounded(1);
self.raw.pending.insert(id.clone(), tx);
self.raw
.sender
.send(Request::new(id, R::METHOD.to_string(), params).into())?;
let response = rx.recv()?;
let result = match response.error {
Some(error) => bail!(error.message),
None => response.result.unwrap_or_default(),
};
Ok(serde_json::from_value(result)?)
}
pub fn send_response(&self, response: lsp_server::Response) -> Result<()> {
self.raw.sender.send(response.into())?;
Ok(())
}
pub fn send_error(&self, id: RequestId, code: ErrorCode, message: String) -> Result<()> {
self.send_response(lsp_server::Response::new_err(id, code as i32, message))?;
Ok(())
}
pub fn recv_response(&self, response: lsp_server::Response) -> Result<()> {
let (_, tx) = self
.raw
.pending
.remove(&response.id)
.expect("response with known request id received");
tx.send(response)?;
Ok(())
}
pub fn parse_options(&self, value: serde_json::Value) -> Result<Options> {
let options = match serde_json::from_value(value) {
Ok(new_options) => new_options,
Err(why) => {
let message = format!(
"The texlab configuration is invalid; using the default settings instead.\nDetails: {why}"
);
let typ = MessageType::WARNING;
self.send_notification::<ShowMessage>(ShowMessageParams { message, typ })?;
None
}
};
Ok(options.unwrap_or_default())
}
}
|