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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
use deno_core::futures::future;
use deno_core::serde_json;
use deno_core::serde_json::json;
use lspower::lsp;
use crate::lsp::config::SETTINGS_SECTION;
use crate::lsp::repl::get_repl_workspace_settings;
use super::lsp_custom;
#[derive(Clone)]
pub struct Client(Arc<dyn ClientTrait>);
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Client").finish()
}
}
impl Client {
pub fn from_lspower(client: lspower::Client) -> Self {
Self(Arc::new(LspowerClient(client)))
}
pub fn new_for_repl() -> Self {
Self(Arc::new(ReplClient))
}
pub async fn publish_diagnostics(
&self,
uri: lsp::Url,
diags: Vec<lsp::Diagnostic>,
version: Option<i32>,
) {
self.0.publish_diagnostics(uri, diags, version).await;
}
pub async fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
) {
self.0.send_registry_state_notification(params).await;
}
pub async fn configuration(
&self,
items: Vec<lsp::ConfigurationItem>,
) -> Result<Vec<serde_json::Value>, AnyError> {
self.0.configuration(items).await
}
pub async fn show_message(
&self,
message_type: lsp::MessageType,
message: impl std::fmt::Display,
) {
self
.0
.show_message(message_type, format!("{}", message))
.await
}
pub async fn register_capability(
&self,
registrations: Vec<lsp::Registration>,
) -> Result<(), AnyError> {
self.0.register_capability(registrations).await
}
}
type AsyncReturn<T> = Pin<Box<dyn Future<Output = T> + 'static + Send>>;
trait ClientTrait: Send + Sync {
fn publish_diagnostics(
&self,
uri: lsp::Url,
diagnostics: Vec<lsp::Diagnostic>,
version: Option<i32>,
) -> AsyncReturn<()>;
fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
) -> AsyncReturn<()>;
fn configuration(
&self,
items: Vec<lsp::ConfigurationItem>,
) -> AsyncReturn<Result<Vec<serde_json::Value>, AnyError>>;
fn show_message(
&self,
message_type: lsp::MessageType,
text: String,
) -> AsyncReturn<()>;
fn register_capability(
&self,
registrations: Vec<lsp::Registration>,
) -> AsyncReturn<Result<(), AnyError>>;
}
#[derive(Clone)]
struct LspowerClient(lspower::Client);
impl ClientTrait for LspowerClient {
fn publish_diagnostics(
&self,
uri: lsp::Url,
diagnostics: Vec<lsp::Diagnostic>,
version: Option<i32>,
) -> AsyncReturn<()> {
let client = self.0.clone();
Box::pin(async move {
client.publish_diagnostics(uri, diagnostics, version).await
})
}
fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
) -> AsyncReturn<()> {
let client = self.0.clone();
Box::pin(async move {
client
.send_custom_notification::<lsp_custom::RegistryStateNotification>(
params,
)
.await
})
}
fn configuration(
&self,
items: Vec<lsp::ConfigurationItem>,
) -> AsyncReturn<Result<Vec<serde_json::Value>, AnyError>> {
let client = self.0.clone();
Box::pin(async move {
client
.configuration(items)
.await
.map_err(|err| anyhow!("{}", err))
})
}
fn show_message(
&self,
message_type: lsp::MessageType,
message: String,
) -> AsyncReturn<()> {
let client = self.0.clone();
Box::pin(async move { client.show_message(message_type, message).await })
}
fn register_capability(
&self,
registrations: Vec<lsp::Registration>,
) -> AsyncReturn<Result<(), AnyError>> {
let client = self.0.clone();
Box::pin(async move {
client
.register_capability(registrations)
.await
.map_err(|err| anyhow!("{}", err))
})
}
}
#[derive(Clone)]
struct ReplClient;
impl ClientTrait for ReplClient {
fn publish_diagnostics(
&self,
_uri: lsp::Url,
_diagnostics: Vec<lsp::Diagnostic>,
_version: Option<i32>,
) -> AsyncReturn<()> {
Box::pin(future::ready(()))
}
fn send_registry_state_notification(
&self,
_params: lsp_custom::RegistryStateNotificationParams,
) -> AsyncReturn<()> {
Box::pin(future::ready(()))
}
fn configuration(
&self,
items: Vec<lsp::ConfigurationItem>,
) -> AsyncReturn<Result<Vec<serde_json::Value>, AnyError>> {
let is_global_config_request = items.len() == 1
&& items[0].scope_uri.is_none()
&& items[0].section.as_deref() == Some(SETTINGS_SECTION);
let response = if is_global_config_request {
vec![serde_json::to_value(get_repl_workspace_settings()).unwrap()]
} else {
// all specifiers are enabled for the REPL
items
.into_iter()
.map(|_| {
json!({
"enable": true,
})
})
.collect()
};
Box::pin(future::ready(Ok(response)))
}
fn show_message(
&self,
_message_type: lsp::MessageType,
_message: String,
) -> AsyncReturn<()> {
Box::pin(future::ready(()))
}
fn register_capability(
&self,
_registrations: Vec<lsp::Registration>,
) -> AsyncReturn<Result<(), AnyError>> {
Box::pin(future::ready(Ok(())))
}
}
|