forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rs
404 lines (363 loc) · 10.4 KB
/
client.rs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::sync::Arc;
use async_trait::async_trait;
use deno_core::anyhow::anyhow;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::serde_json::json;
use deno_core::unsync::spawn;
use tower_lsp::lsp_types as lsp;
use tower_lsp::lsp_types::ConfigurationItem;
use crate::lsp::repl::get_repl_workspace_settings;
use super::config::WorkspaceSettings;
use super::config::SETTINGS_SECTION;
use super::lsp_custom;
use super::testing::lsp_custom as testing_lsp_custom;
use super::urls::LspClientUrl;
#[derive(Debug)]
pub enum TestingNotification {
Module(testing_lsp_custom::TestModuleNotificationParams),
DeleteModule(testing_lsp_custom::TestModuleDeleteNotificationParams),
Progress(testing_lsp_custom::TestRunProgressParams),
}
#[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_tower(client: tower_lsp::Client) -> Self {
Self(Arc::new(TowerClient(client)))
}
pub fn new_for_repl() -> Self {
Self(Arc::new(ReplClient))
}
/// Gets additional methods that should only be called outside
/// the LSP's lock to prevent deadlocking scenarios.
pub fn when_outside_lsp_lock(&self) -> OutsideLockClient {
OutsideLockClient(self.0.clone())
}
pub fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
) {
// do on a task in case the caller currently is in the lsp lock
let client = self.0.clone();
spawn(async move {
client.send_registry_state_notification(params).await;
});
}
/// This notification is sent to the client during internal testing
/// purposes only in order to let the test client know when the latest
/// diagnostics have been published.
pub fn send_diagnostic_batch_notification(
&self,
params: lsp_custom::DiagnosticBatchNotificationParams,
) {
// do on a task in case the caller currently is in the lsp lock
let client = self.0.clone();
spawn(async move {
client.send_diagnostic_batch_notification(params).await;
});
}
pub fn send_test_notification(&self, params: TestingNotification) {
// do on a task in case the caller currently is in the lsp lock
let client = self.0.clone();
spawn(async move {
client.send_test_notification(params).await;
});
}
pub fn send_did_change_deno_configuration_notification(
&self,
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
) {
// do on a task in case the caller currently is in the lsp lock
let client = self.0.clone();
spawn(async move {
client
.send_did_change_deno_configuration_notification(params)
.await;
});
}
pub fn send_did_upgrade_check_notification(
&self,
params: lsp_custom::DidUpgradeCheckNotificationParams,
) {
// do on a task in case the caller currently is in the lsp lock
let client = self.0.clone();
spawn(async move {
client.send_did_upgrade_check_notification(params).await;
});
}
pub fn show_message(
&self,
message_type: lsp::MessageType,
message: impl std::fmt::Display,
) {
// do on a task in case the caller currently is in the lsp lock
let client = self.0.clone();
let message = message.to_string();
spawn(async move {
client.show_message(message_type, message).await;
});
}
}
/// DANGER: The methods on this client should only be called outside
/// the LSP's lock. The reason is you never want to call into the client
/// while holding the lock because the client might call back into the
/// server and cause a deadlock.
pub struct OutsideLockClient(Arc<dyn ClientTrait>);
impl OutsideLockClient {
pub async fn register_capability(
&self,
registrations: Vec<lsp::Registration>,
) -> Result<(), AnyError> {
self.0.register_capability(registrations).await
}
pub async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
) -> Result<Vec<WorkspaceSettings>, AnyError> {
self.0.workspace_configuration(scopes).await
}
pub async fn publish_diagnostics(
&self,
uri: LspClientUrl,
diags: Vec<lsp::Diagnostic>,
version: Option<i32>,
) {
self
.0
.publish_diagnostics(uri.into_url(), diags, version)
.await;
}
}
#[async_trait]
trait ClientTrait: Send + Sync {
async fn publish_diagnostics(
&self,
uri: lsp::Url,
diagnostics: Vec<lsp::Diagnostic>,
version: Option<i32>,
);
async fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
);
async fn send_diagnostic_batch_notification(
&self,
params: lsp_custom::DiagnosticBatchNotificationParams,
);
async fn send_test_notification(&self, params: TestingNotification);
async fn send_did_change_deno_configuration_notification(
&self,
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
);
async fn send_did_upgrade_check_notification(
&self,
params: lsp_custom::DidUpgradeCheckNotificationParams,
);
async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
) -> Result<Vec<WorkspaceSettings>, AnyError>;
async fn show_message(&self, message_type: lsp::MessageType, text: String);
async fn register_capability(
&self,
registrations: Vec<lsp::Registration>,
) -> Result<(), AnyError>;
}
#[derive(Clone)]
struct TowerClient(tower_lsp::Client);
#[async_trait]
impl ClientTrait for TowerClient {
async fn publish_diagnostics(
&self,
uri: lsp::Url,
diagnostics: Vec<lsp::Diagnostic>,
version: Option<i32>,
) {
self.0.publish_diagnostics(uri, diagnostics, version).await
}
async fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
) {
self
.0
.send_notification::<lsp_custom::RegistryStateNotification>(params)
.await
}
async fn send_diagnostic_batch_notification(
&self,
params: lsp_custom::DiagnosticBatchNotificationParams,
) {
self
.0
.send_notification::<lsp_custom::DiagnosticBatchNotification>(params)
.await
}
async fn send_test_notification(&self, notification: TestingNotification) {
match notification {
TestingNotification::Module(params) => {
self
.0
.send_notification::<testing_lsp_custom::TestModuleNotification>(
params,
)
.await
}
TestingNotification::DeleteModule(params) => self
.0
.send_notification::<testing_lsp_custom::TestModuleDeleteNotification>(
params,
)
.await,
TestingNotification::Progress(params) => {
self
.0
.send_notification::<testing_lsp_custom::TestRunProgressNotification>(
params,
)
.await
}
}
}
async fn send_did_change_deno_configuration_notification(
&self,
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
) {
self
.0
.send_notification::<lsp_custom::DidChangeDenoConfigurationNotification>(
params,
)
.await
}
async fn send_did_upgrade_check_notification(
&self,
params: lsp_custom::DidUpgradeCheckNotificationParams,
) {
self
.0
.send_notification::<lsp_custom::DidUpgradeCheckNotification>(params)
.await
}
async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
) -> Result<Vec<WorkspaceSettings>, AnyError> {
let config_response = self
.0
.configuration(
scopes
.iter()
.flat_map(|scope_uri| {
vec![
ConfigurationItem {
scope_uri: scope_uri.clone(),
section: Some(SETTINGS_SECTION.to_string()),
},
ConfigurationItem {
scope_uri: scope_uri.clone(),
section: Some("javascript".to_string()),
},
ConfigurationItem {
scope_uri: scope_uri.clone(),
section: Some("typescript".to_string()),
},
]
})
.collect(),
)
.await;
match config_response {
Ok(configs) => {
let mut configs = configs.into_iter();
let mut result = Vec::with_capacity(scopes.len());
for _ in 0..scopes.len() {
let deno = json!(configs.next());
let javascript = json!(configs.next());
let typescript = json!(configs.next());
result.push(WorkspaceSettings::from_raw_settings(
deno, javascript, typescript,
));
}
Ok(result)
}
Err(err) => {
bail!("Error getting workspace configurations: {}", err)
}
}
}
async fn show_message(
&self,
message_type: lsp::MessageType,
message: String,
) {
self.0.show_message(message_type, message).await
}
async fn register_capability(
&self,
registrations: Vec<lsp::Registration>,
) -> Result<(), AnyError> {
self
.0
.register_capability(registrations)
.await
.map_err(|err| anyhow!("{}", err))
}
}
#[derive(Clone)]
struct ReplClient;
#[async_trait]
impl ClientTrait for ReplClient {
async fn publish_diagnostics(
&self,
_uri: lsp::Url,
_diagnostics: Vec<lsp::Diagnostic>,
_version: Option<i32>,
) {
}
async fn send_registry_state_notification(
&self,
_params: lsp_custom::RegistryStateNotificationParams,
) {
}
async fn send_diagnostic_batch_notification(
&self,
_params: lsp_custom::DiagnosticBatchNotificationParams,
) {
}
async fn send_test_notification(&self, _params: TestingNotification) {}
async fn send_did_change_deno_configuration_notification(
&self,
_params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
) {
}
async fn send_did_upgrade_check_notification(
&self,
_params: lsp_custom::DidUpgradeCheckNotificationParams,
) {
}
async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
) -> Result<Vec<WorkspaceSettings>, AnyError> {
Ok(vec![get_repl_workspace_settings(); scopes.len()])
}
async fn show_message(
&self,
_message_type: lsp::MessageType,
_message: String,
) {
}
async fn register_capability(
&self,
_registrations: Vec<lsp::Registration>,
) -> Result<(), AnyError> {
Ok(())
}
}