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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use lsp_types_max::*;
use serde::Serialize;
use serde_json::Value;
use std::fmt::Display;
use super::Client;
use crate::jsonrpc;
impl Client {
// Lifecycle Messages
/// Registers a new capability with the client.
///
/// This corresponds to the [`client/registerCapability`] request.
///
/// [`client/registerCapability`]: https://microsoft.github.io/language-server-protocol/specification#client_registerCapability
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
pub async fn register_capability(
&self,
registrations: Vec<Registration>,
) -> jsonrpc::Result<()> {
use lsp_types_max::request::RegisterCapability;
self.send_request::<RegisterCapability>(RegistrationParams { registrations })
.await
}
/// Unregisters a capability with the client.
///
/// This corresponds to the [`client/unregisterCapability`] request.
///
/// [`client/unregisterCapability`]: https://microsoft.github.io/language-server-protocol/specification#client_unregisterCapability
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
pub async fn unregister_capability(
&self,
unregisterations: Vec<Unregistration>,
) -> jsonrpc::Result<()> {
use lsp_types_max::request::UnregisterCapability;
self.send_request::<UnregisterCapability>(UnregistrationParams { unregisterations })
.await
}
// Window Features
/// Notifies the client to display a particular message in the user interface.
///
/// This corresponds to the [`window/showMessage`] notification.
///
/// [`window/showMessage`]: https://microsoft.github.io/language-server-protocol/specification#window_showMessage
pub async fn show_message<M: Display>(&self, typ: MessageType, message: M) {
use lsp_types_max::notification::ShowMessage;
self.send_notification_unchecked::<ShowMessage>(ShowMessageParams {
typ,
message: message.to_string(),
})
.await;
}
/// Requests the client to display a particular message in the user interface.
///
/// Unlike the `show_message` notification, this request can also pass a list of actions and
/// wait for an answer from the client.
///
/// This corresponds to the [`window/showMessageRequest`] request.
///
/// [`window/showMessageRequest`]: https://microsoft.github.io/language-server-protocol/specification#window_showMessageRequest
pub async fn show_message_request<M: Display>(
&self,
typ: MessageType,
message: M,
actions: Option<Vec<MessageActionItem>>,
) -> jsonrpc::Result<Option<MessageActionItem>> {
use lsp_types_max::request::ShowMessageRequest;
self.send_request_unchecked::<ShowMessageRequest>(ShowMessageRequestParams {
typ,
message: message.to_string(),
actions,
})
.await
}
/// Notifies the client to log a particular message.
///
/// This corresponds to the [`window/logMessage`] notification.
///
/// [`window/logMessage`]: https://microsoft.github.io/language-server-protocol/specification#window_logMessage
pub async fn log_message<M: Display>(&self, typ: MessageType, message: M) {
use lsp_types_max::notification::LogMessage;
self.send_notification_unchecked::<LogMessage>(LogMessageParams {
typ,
message: message.to_string(),
})
.await;
}
/// Notifies the client to log a trace.
///
/// This corresponds to the [`$/logTrace`] notification.
///
/// [`$/logTrace`]: https://microsoft.github.io/language-server-protocol/specification#logTrace
pub async fn log_trace(&self, params: LogTraceParams) {
use lsp_types_max::notification::LogTrace;
self.send_notification_unchecked::<LogTrace>(params).await;
}
/// Asks the client to display a particular resource referenced by a URI in the user interface.
///
/// Returns `Ok(true)` if the document was successfully shown, or `Ok(false)` otherwise.
///
/// This corresponds to the [`window/showDocument`] request.
///
/// [`window/showDocument`]: https://microsoft.github.io/language-server-protocol/specification#window_showDocument
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.16.0.
pub async fn show_document(&self, params: ShowDocumentParams) -> jsonrpc::Result<bool> {
use lsp_types_max::request::ShowDocument;
let response = self.send_request::<ShowDocument>(params).await?;
Ok(response.success)
}
/// Asks the client to create a work done progress token.
///
/// This corresponds to the [`window/workDoneProgress/create`] request.
///
/// [`window/workDoneProgress/create`]: https://microsoft.github.io/language-server-protocol/specification#window_workDoneProgress_create
///
/// # Compatibility
///
/// This request was introduced in specification version 3.15.0.
pub async fn work_done_progress_create(
&self,
params: WorkDoneProgressCreateParams,
) -> jsonrpc::Result<()> {
use lsp_types_max::request::WorkDoneProgressCreate;
self.send_request::<WorkDoneProgressCreate>(params).await
}
/// Notifies the client to log a telemetry event.
///
/// This corresponds to the [`telemetry/event`] notification.
///
/// [`telemetry/event`]: https://microsoft.github.io/language-server-protocol/specification#telemetry_event
pub async fn telemetry_event<S: Serialize>(&self, data: S) {
use lsp_types_max::notification::TelemetryEvent;
match serde_json::to_value(data) {
Err(e) => tracing::error!("invalid JSON in `telemetry/event` notification: {}", e),
Ok(mut value) => {
if !value.is_null() && !value.is_array() && !value.is_object() {
value = Value::Array(vec![value]);
}
let params = match value {
Value::Object(map) => OneOf::Left(map),
Value::Array(vec) => OneOf::Right(vec),
_ => OneOf::Right(Vec::new()),
};
self.send_notification_unchecked::<TelemetryEvent>(params)
.await;
}
}
}
/// Asks the client to refresh the code lenses currently shown in editors. As a result, the
/// client should ask the server to recompute the code lenses for these editors.
///
/// This is useful if a server detects a configuration change which requires a re-calculation
/// of all code lenses.
///
/// Note that the client still has the freedom to delay the re-calculation of the code lenses
/// if for example an editor is currently not visible.
///
/// This corresponds to the [`workspace/codeLens/refresh`] request.
///
/// [`workspace/codeLens/refresh`]: https://microsoft.github.io/language-server-protocol/specification#codeLens_refresh
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.16.0.
pub async fn code_lens_refresh(&self) -> jsonrpc::Result<()> {
use lsp_types_max::request::CodeLensRefresh;
self.send_request::<CodeLensRefresh>(()).await
}
/// Asks the client to refresh the editors for which this server provides semantic tokens. As a
/// result, the client should ask the server to recompute the semantic tokens for these
/// editors.
///
/// This is useful if a server detects a project-wide configuration change which requires a
/// re-calculation of all semantic tokens. Note that the client still has the freedom to delay
/// the re-calculation of the semantic tokens if for example an editor is currently not visible.
///
/// This corresponds to the [`workspace/semanticTokens/refresh`] request.
///
/// [`workspace/semanticTokens/refresh`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_semanticTokens
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.16.0.
pub async fn semantic_tokens_refresh(&self) -> jsonrpc::Result<()> {
use lsp_types_max::request::SemanticTokensRefresh;
self.send_request::<SemanticTokensRefresh>(()).await
}
/// Asks the client to refresh the inline values currently shown in editors. As a result, the
/// client should ask the server to recompute the inline values for these editors.
///
/// This is useful if a server detects a configuration change which requires a re-calculation
/// of all inline values. Note that the client still has the freedom to delay the
/// re-calculation of the inline values if for example an editor is currently not visible.
///
/// This corresponds to the [`workspace/inlineValue/refresh`] request.
///
/// [`workspace/inlineValue/refresh`]: https://microsoft.github.io/language-server-protocol/specification#workspace_inlineValue_refresh
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.17.0.
pub async fn inline_value_refresh(&self) -> jsonrpc::Result<()> {
use lsp_types_max::request::InlineValueRefreshRequest;
self.send_request::<InlineValueRefreshRequest>(()).await
}
/// Asks the client to refresh the inlay hints currently shown in editors. As a result, the
/// client should ask the server to recompute the inlay hints for these editors.
///
/// This is useful if a server detects a configuration change which requires a re-calculation
/// of all inlay hints. Note that the client still has the freedom to delay the re-calculation
/// of the inlay hints if for example an editor is currently not visible.
///
/// This corresponds to the [`workspace/inlayHint/refresh`] request.
///
/// [`workspace/inlayHint/refresh`]: https://microsoft.github.io/language-server-protocol/specification#workspace_inlayHint_refresh
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.17.0.
pub async fn inlay_hint_refresh(&self) -> jsonrpc::Result<()> {
use lsp_types_max::request::InlayHintRefreshRequest;
self.send_request::<InlayHintRefreshRequest>(()).await
}
/// Asks the client to refresh all folding ranges.
///
/// This corresponds to the [`workspace/foldingRange/refresh`] request.
///
/// [`workspace/foldingRange/refresh`]: https://microsoft.github.io/language-server-protocol/specification#workspace_foldingRange_refresh
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.18.0.
pub async fn folding_range_refresh(&self) -> jsonrpc::Result<()> {
self.send_request::<super::FoldingRangeRefresh>(()).await
}
/// Asks the client to refresh all needed document and workspace diagnostics.
///
/// This is useful if a server detects a project wide configuration change which requires a
/// re-calculation of all diagnostics.
///
/// This corresponds to the [`workspace/diagnostic/refresh`] request.
///
/// [`workspace/diagnostic/refresh`]: https://microsoft.github.io/language-server-protocol/specification#diagnostic_refresh
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.17.0.
pub async fn workspace_diagnostic_refresh(&self) -> jsonrpc::Result<()> {
use lsp_types_max::request::WorkspaceDiagnosticRefresh;
self.send_request::<WorkspaceDiagnosticRefresh>(()).await
}
/// Asks the client to refresh the content of a text document.
///
/// This corresponds to the [`workspace/textDocumentContent/refresh`](lsp_max_protocol::lsp_3_18::TextDocumentContentRefreshRequest) request.
pub async fn text_document_content_refresh(
&self,
params: lsp_max_protocol::lsp_3_18::TextDocumentContentRefreshParams,
) -> jsonrpc::Result<()> {
self.send_request::<lsp_max_protocol::lsp_3_18::TextDocumentContentRefreshRequest>(params)
.await
}
/// Submits validation diagnostics for an open file with the given URI.
///
/// This corresponds to the [`textDocument/publishDiagnostics`] notification.
///
/// [`textDocument/publishDiagnostics`]: https://microsoft.github.io/language-server-protocol/specification#textDocument_publishDiagnostics
///
/// # Initialization
///
/// This notification will only be sent if the server is initialized.
pub async fn publish_diagnostics(
&self,
uri: Uri,
diags: Vec<Diagnostic>,
version: Option<i32>,
) {
use lsp_types_max::notification::PublishDiagnostics;
self.send_notification_unchecked::<PublishDiagnostics>(PublishDiagnosticsParams::new(
uri, diags, version,
))
.await;
}
// Workspace Features
/// Fetches configuration settings from the client.
///
/// The request can fetch several configuration settings in one roundtrip. The order of the
/// returned configuration settings correspond to the order of the passed
/// [`ConfigurationItem`]s (e.g. the first item in the response is the result for the first
/// configuration item in the params).
///
/// This corresponds to the [`workspace/configuration`] request.
///
/// [`workspace/configuration`]: https://microsoft.github.io/language-server-protocol/specification#workspace_configuration
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.6.0.
pub async fn configuration(
&self,
items: Vec<ConfigurationItem>,
) -> jsonrpc::Result<Vec<Value>> {
use lsp_types_max::request::WorkspaceConfiguration;
self.send_request::<WorkspaceConfiguration>(ConfigurationParams { items })
.await
}
/// Fetches the current open list of workspace folders.
///
/// Returns `None` if only a single file is open in the tool. Returns an empty `Vec` if a
/// workspace is open but no folders are configured.
///
/// This corresponds to the [`workspace/workspaceFolders`] request.
///
/// [`workspace/workspaceFolders`]: https://microsoft.github.io/language-server-protocol/specification#workspace_workspaceFolders
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
///
/// # Compatibility
///
/// This request was introduced in specification version 3.6.0.
pub async fn workspace_folders(&self) -> jsonrpc::Result<Option<Vec<WorkspaceFolder>>> {
use lsp_types_max::request::WorkspaceFoldersRequest;
self.send_request::<WorkspaceFoldersRequest>(()).await
}
/// Requests a workspace resource be edited on the client side and returns whether the edit was
/// applied.
///
/// This corresponds to the [`workspace/applyEdit`] request.
///
/// [`workspace/applyEdit`]: https://microsoft.github.io/language-server-protocol/specification#workspace_applyEdit
///
/// # Initialization
///
/// If the request is sent to the client before the server has been initialized, this will
/// immediately return `Err` with JSON-RPC error code `-32002` ([read more]).
///
/// [read more]: https://microsoft.github.io/language-server-protocol/specification#initialize
pub async fn apply_edit(
&self,
edit: WorkspaceEdit,
) -> jsonrpc::Result<ApplyWorkspaceEditResponse> {
use lsp_types_max::request::ApplyWorkspaceEdit;
self.send_request::<ApplyWorkspaceEdit>(ApplyWorkspaceEditParams { edit, label: None })
.await
}
/// Starts a stream of `$/progress` notifications for a client-provided [`ProgressToken`].
///
/// This method also takes a `title` argument briefly describing the kind of operation being
/// performed, e.g. "Indexing" or "Linking Dependencies".
///
/// [`ProgressToken`]: https://docs.rs/lsp-types/latest/lsp_types/type.ProgressToken.html
///
/// # Initialization
///
/// These notifications will only be sent if the server is initialized.
pub fn progress<T>(&self, token: ProgressToken, title: T) -> super::Progress
where
T: Into<String>,
{
super::Progress::new(self.clone(), token, title.into())
}
}