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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
use std::sync::Once;
use anyhow::{bail, Result};
use crossbeam_channel::{Receiver, Sender};
use lsp_server::{Connection, Message, Notification, Request, Response};
use lsp_types::{
notification::{Exit, Initialized},
request::{Initialize, Shutdown},
ClientCapabilities, ClientInfo, DidOpenTextDocumentParams, InitializeParams, InitializeResult,
InitializedParams, Url, WorkspaceFolder,
};
use tempfile::{tempdir, TempDir};
use crate::Server;
static INIT_LOGGER: Once = Once::new();
pub struct IncomingHandler {
_handle: jod_thread::JoinHandle<Result<()>>,
pub requests: Receiver<Request>,
pub notifications: Receiver<Notification>,
pub responses: Receiver<Response>,
}
impl IncomingHandler {
pub fn spawn(receiver: Receiver<Message>) -> Self {
let (req_sender, req_receiver) = crossbeam_channel::unbounded();
let (not_sender, not_receiver) = crossbeam_channel::unbounded();
let (res_sender, res_receiver) = crossbeam_channel::unbounded();
let _handle = jod_thread::spawn(move || {
for message in &receiver {
match message {
Message::Request(req) => req_sender.send(req)?,
Message::Response(res) => res_sender.send(res)?,
Message::Notification(not) => not_sender.send(not)?,
};
}
Ok(())
});
Self {
_handle,
requests: req_receiver,
notifications: not_receiver,
responses: res_receiver,
}
}
}
pub struct ClientResult {
pub directory: TempDir,
pub incoming: IncomingHandler,
}
pub struct Client {
outgoing: Sender<Message>,
incoming: IncomingHandler,
directory: TempDir,
request_id: i32,
_handle: jod_thread::JoinHandle,
}
impl Client {
pub fn spawn() -> Self {
INIT_LOGGER.call_once(|| env_logger::init());
let directory = tempdir().unwrap();
let (client, server) = Connection::memory();
let incoming = IncomingHandler::spawn(client.receiver);
let outgoing = client.sender;
let server = Server::new(server);
let _handle = jod_thread::spawn(move || {
server.run().expect("server failed to run");
});
Self {
outgoing,
incoming,
directory,
request_id: 0,
_handle,
}
}
pub fn initialize(
&mut self,
client_capabilities: ClientCapabilities,
client_info: Option<ClientInfo>,
) -> InitializeResult {
let result = self
.request::<Initialize>(InitializeParams {
initialization_options: Some(serde_json::json!({ "skipDistro": true })),
capabilities: client_capabilities,
workspace_folders: Some(vec![WorkspaceFolder {
name: "Test".into(),
uri: Url::from_directory_path(self.directory.path()).unwrap(),
}]),
client_info,
..InitializeParams::default()
})
.unwrap();
self.notify::<Initialized>(InitializedParams {});
result
}
pub fn request<R: lsp_types::request::Request>(
&mut self,
params: R::Params,
) -> Result<R::Result> {
self.request_id += 1;
self.outgoing
.send(Request::new(self.request_id.into(), R::METHOD.into(), params).into())
.unwrap();
let response = self.incoming.responses.recv().unwrap();
assert_eq!(response.id, self.request_id.into());
let result = match response.result {
Some(result) => result,
None => bail!("request failed: {:?}", response.error),
};
Ok(serde_json::from_value(result)?)
}
pub fn notify<N: lsp_types::notification::Notification>(&mut self, params: N::Params) {
self.outgoing
.send(Notification::new(N::METHOD.into(), serde_json::to_value(params).unwrap()).into())
.unwrap();
}
pub fn open(&mut self, name: &str, language_id: &str, text: String) {
self.notify::<lsp_types::notification::DidOpenTextDocument>(DidOpenTextDocumentParams {
text_document: lsp_types::TextDocumentItem {
uri: self.uri(name),
language_id: language_id.to_string(),
version: 0,
text,
},
});
}
pub fn shutdown(mut self) -> ClientResult {
self.request::<Shutdown>(()).unwrap();
self.notify::<Exit>(());
ClientResult {
directory: self.directory,
incoming: self.incoming,
}
}
pub fn uri(&self, name: &str) -> Url {
Url::from_file_path(self.directory.path().join(name)).unwrap()
}
}
|