ls_types/
notification.rs

1use super::{
2    CancelParams, CreateFilesParams, Debug, DeleteFilesParams, DidChangeConfigurationParams,
3    DidChangeNotebookDocumentParams, DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
4    DidChangeWorkspaceFoldersParams, DidCloseNotebookDocumentParams, DidCloseTextDocumentParams,
5    DidOpenNotebookDocumentParams, DidOpenTextDocumentParams, DidSaveNotebookDocumentParams,
6    DidSaveTextDocumentParams, InitializedParams, LSPArray, LSPObject, LogMessageParams,
7    LogTraceParams, OneOf, ProgressParams, PublishDiagnosticsParams, RenameFilesParams,
8    SetTraceParams, ShowMessageParams, WillSaveTextDocumentParams, WorkDoneProgressCancelParams,
9};
10
11use serde::{Serialize, de::DeserializeOwned};
12
13pub trait Notification {
14    type Params: DeserializeOwned + Serialize + Send + Sync + 'static;
15    const METHOD: &'static str;
16}
17
18#[macro_export]
19macro_rules! lsp_notification {
20    ("$/cancelRequest") => {
21        $crate::notification::Cancel
22    };
23    ("$/setTrace") => {
24        $crate::notification::SetTrace
25    };
26    ("$/logTrace") => {
27        $crate::notification::LogTrace
28    };
29    ("initialized") => {
30        $crate::notification::Initialized
31    };
32    ("exit") => {
33        $crate::notification::Exit
34    };
35
36    ("window/showMessage") => {
37        $crate::notification::ShowMessage
38    };
39    ("window/logMessage") => {
40        $crate::notification::LogMessage
41    };
42    ("window/workDoneProgress/cancel") => {
43        $crate::notification::WorkDoneProgressCancel
44    };
45
46    ("telemetry/event") => {
47        $crate::notification::TelemetryEvent
48    };
49
50    ("textDocument/didOpen") => {
51        $crate::notification::DidOpenTextDocument
52    };
53    ("textDocument/didChange") => {
54        $crate::notification::DidChangeTextDocument
55    };
56    ("textDocument/willSave") => {
57        $crate::notification::WillSaveTextDocument
58    };
59    ("textDocument/didSave") => {
60        $crate::notification::DidSaveTextDocument
61    };
62    ("textDocument/didClose") => {
63        $crate::notification::DidCloseTextDocument
64    };
65    ("textDocument/publishDiagnostics") => {
66        $crate::notification::PublishDiagnostics
67    };
68
69    ("notebookDocument/didOpen") => {
70        $crate::notification::DidOpenNotebookDocument
71    };
72    ("notebookDocument/didChange") => {
73        $crate::notification::DidChangeNotebookDocument
74    };
75    ("notebookDocument/didSave") => {
76        $crate::notification::DidSaveNotebookDocument
77    };
78    ("notebookDocument/didClose") => {
79        $crate::notification::DidCloseNotebookDocument
80    };
81
82    ("workspace/didChangeConfiguration") => {
83        $crate::notification::DidChangeConfiguration
84    };
85    ("workspace/didChangeWatchedFiles") => {
86        $crate::notification::DidChangeWatchedFiles
87    };
88    ("workspace/didChangeWorkspaceFolders") => {
89        $crate::notification::DidChangeWorkspaceFolders
90    };
91    ("$/progress") => {
92        $crate::notification::Progress
93    };
94    ("workspace/didCreateFiles") => {
95        $crate::notification::DidCreateFiles
96    };
97    ("workspace/didRenameFiles") => {
98        $crate::notification::DidRenameFiles
99    };
100    ("workspace/didDeleteFiles") => {
101        $crate::notification::DidDeleteFiles
102    };
103}
104
105/// The base protocol now offers support for request cancellation. To cancel a request,
106/// a notification message with the following properties is sent:
107///
108/// A request that got canceled still needs to return from the server and send a response back.
109/// It can not be left open / hanging. This is in line with the JSON RPC protocol that requires
110/// that every request sends a response back. In addition it allows for returning partial results on cancel.
111#[derive(Debug)]
112pub enum Cancel {}
113
114impl Notification for Cancel {
115    type Params = CancelParams;
116    const METHOD: &'static str = "$/cancelRequest";
117}
118
119/// A notification that should be used by the client to modify the trace
120/// setting of the server.
121#[derive(Debug)]
122pub enum SetTrace {}
123
124impl Notification for SetTrace {
125    type Params = SetTraceParams;
126    const METHOD: &'static str = "$/setTrace";
127}
128
129/// A notification to log the trace of the server’s execution.
130/// The amount and content of these notifications depends on the current trace configuration.
131///
132/// `LogTrace` should be used for systematic trace reporting. For single debugging messages,
133/// the server should send `LogMessage` notifications.
134#[derive(Debug)]
135pub enum LogTrace {}
136
137impl Notification for LogTrace {
138    type Params = LogTraceParams;
139    const METHOD: &'static str = "$/logTrace";
140}
141
142/// The initialized notification is sent from the client to the server after the client received
143/// the result of the initialize request but before the client is sending any other request or
144/// notification to the server. The server can use the initialized notification for example to
145/// dynamically register capabilities.
146#[derive(Debug)]
147pub enum Initialized {}
148
149impl Notification for Initialized {
150    type Params = InitializedParams;
151    const METHOD: &'static str = "initialized";
152}
153
154/// A notification to ask the server to exit its process.
155/// The server should exit with success code 0 if the shutdown request has been received before;
156/// otherwise with error code 1.
157#[derive(Debug)]
158pub enum Exit {}
159
160impl Notification for Exit {
161    type Params = ();
162    const METHOD: &'static str = "exit";
163}
164
165/// The show message notification is sent from a server to a client to ask the client to display a particular message
166/// in the user interface.
167#[derive(Debug)]
168pub enum ShowMessage {}
169
170impl Notification for ShowMessage {
171    type Params = ShowMessageParams;
172    const METHOD: &'static str = "window/showMessage";
173}
174
175/// The log message notification is sent from the server to the client to ask the client to log a particular message.
176#[derive(Debug)]
177pub enum LogMessage {}
178
179impl Notification for LogMessage {
180    type Params = LogMessageParams;
181    const METHOD: &'static str = "window/logMessage";
182}
183
184/// The telemetry notification is sent from the server to the client to ask the client to log a telemetry event.
185///
186/// The protocol doesn't specify the payload since no interpretation of the data happens in the protocol. Most clients even don't handle
187/// the event directly but forward them to the extensions owning the corresponding server issuing the event.
188#[derive(Debug)]
189pub enum TelemetryEvent {}
190
191impl Notification for TelemetryEvent {
192    type Params = OneOf<LSPObject, LSPArray>;
193    const METHOD: &'static str = "telemetry/event";
194}
195
196/// A notification sent from the client to the server to signal the change of configuration settings.
197#[derive(Debug)]
198pub enum DidChangeConfiguration {}
199
200impl Notification for DidChangeConfiguration {
201    type Params = DidChangeConfigurationParams;
202    const METHOD: &'static str = "workspace/didChangeConfiguration";
203}
204
205/// The document open notification is sent from the client to the server to signal newly opened text documents.
206///
207/// The document's truth is now managed by the client and the server must not try to read the document's truth
208/// using the document's uri.
209#[derive(Debug)]
210pub enum DidOpenTextDocument {}
211
212impl Notification for DidOpenTextDocument {
213    type Params = DidOpenTextDocumentParams;
214    const METHOD: &'static str = "textDocument/didOpen";
215}
216
217/// The document change notification is sent from the client to the server to signal changes to a text document.
218///
219/// In 2.0 the shape of the params has changed to include proper version numbers and language ids.
220#[derive(Debug)]
221pub enum DidChangeTextDocument {}
222
223impl Notification for DidChangeTextDocument {
224    type Params = DidChangeTextDocumentParams;
225    const METHOD: &'static str = "textDocument/didChange";
226}
227
228/// The document will save notification is sent from the client to the server before the document
229/// is actually saved.
230#[derive(Debug)]
231pub enum WillSaveTextDocument {}
232
233impl Notification for WillSaveTextDocument {
234    type Params = WillSaveTextDocumentParams;
235    const METHOD: &'static str = "textDocument/willSave";
236}
237
238/// The document close notification is sent from the client to the server when the document got closed in the client.
239///
240/// The document's truth now exists where the document's uri points to (e.g. if the document's uri is a file uri
241/// the truth now exists on disk).
242#[derive(Debug)]
243pub enum DidCloseTextDocument {}
244
245impl Notification for DidCloseTextDocument {
246    type Params = DidCloseTextDocumentParams;
247    const METHOD: &'static str = "textDocument/didClose";
248}
249
250/// The document save notification is sent from the client to the server when the document was saved in the client.
251#[derive(Debug)]
252pub enum DidSaveTextDocument {}
253
254impl Notification for DidSaveTextDocument {
255    type Params = DidSaveTextDocumentParams;
256    const METHOD: &'static str = "textDocument/didSave";
257}
258
259#[derive(Debug)]
260pub enum DidOpenNotebookDocument {}
261impl Notification for DidOpenNotebookDocument {
262    type Params = DidOpenNotebookDocumentParams;
263    const METHOD: &'static str = "notebookDocument/didOpen";
264}
265
266#[derive(Debug)]
267pub enum DidChangeNotebookDocument {}
268impl Notification for DidChangeNotebookDocument {
269    type Params = DidChangeNotebookDocumentParams;
270    const METHOD: &'static str = "notebookDocument/didChange";
271}
272
273#[derive(Debug)]
274pub enum DidSaveNotebookDocument {}
275impl Notification for DidSaveNotebookDocument {
276    type Params = DidSaveNotebookDocumentParams;
277    const METHOD: &'static str = "notebookDocument/didSave";
278}
279
280#[derive(Debug)]
281pub enum DidCloseNotebookDocument {}
282impl Notification for DidCloseNotebookDocument {
283    type Params = DidCloseNotebookDocumentParams;
284    const METHOD: &'static str = "notebookDocument/didClose";
285}
286
287/// The watched files notification is sent from the client to the server when the client detects changes to files and folders
288/// watched by the language client (note although the name suggest that only file events are sent it is about file system events which include folders as well).
289/// It is recommended that servers register for these file system events using the registration mechanism.
290/// In former implementations clients pushed file events without the server actively asking for it.
291#[derive(Debug)]
292pub enum DidChangeWatchedFiles {}
293
294impl Notification for DidChangeWatchedFiles {
295    type Params = DidChangeWatchedFilesParams;
296    const METHOD: &'static str = "workspace/didChangeWatchedFiles";
297}
298
299/// The workspace/didChangeWorkspaceFolders notification is sent from the client to the server to inform the server
300/// about workspace folder configuration changes
301#[derive(Debug)]
302pub enum DidChangeWorkspaceFolders {}
303
304impl Notification for DidChangeWorkspaceFolders {
305    type Params = DidChangeWorkspaceFoldersParams;
306    const METHOD: &'static str = "workspace/didChangeWorkspaceFolders";
307}
308
309/// Diagnostics notification are sent from the server to the client to signal results of validation runs.
310#[derive(Debug)]
311pub enum PublishDiagnostics {}
312
313impl Notification for PublishDiagnostics {
314    type Params = PublishDiagnosticsParams;
315    const METHOD: &'static str = "textDocument/publishDiagnostics";
316}
317
318/// The progress notification is sent from the server to the client to ask
319/// the client to indicate progress.
320#[derive(Debug)]
321pub enum Progress {}
322
323impl Notification for Progress {
324    type Params = ProgressParams;
325    const METHOD: &'static str = "$/progress";
326}
327
328/// The `window/workDoneProgress/cancel` notification is sent from the client
329/// to the server to cancel a progress initiated on the server side using the `window/workDoneProgress/create`.
330#[derive(Debug)]
331pub enum WorkDoneProgressCancel {}
332
333impl Notification for WorkDoneProgressCancel {
334    type Params = WorkDoneProgressCancelParams;
335    const METHOD: &'static str = "window/workDoneProgress/cancel";
336}
337
338/// The did create files notification is sent from the client to the server when files were created from within the client.
339#[derive(Debug)]
340pub enum DidCreateFiles {}
341
342impl Notification for DidCreateFiles {
343    type Params = CreateFilesParams;
344    const METHOD: &'static str = "workspace/didCreateFiles";
345}
346
347/// The did rename files notification is sent from the client to the server when files were renamed from within the client.
348#[derive(Debug)]
349pub enum DidRenameFiles {}
350
351impl Notification for DidRenameFiles {
352    type Params = RenameFilesParams;
353    const METHOD: &'static str = "workspace/didRenameFiles";
354}
355
356/// The did delete files notification is sent from the client to the server when files were deleted from within the client.
357#[derive(Debug)]
358pub enum DidDeleteFiles {}
359
360impl Notification for DidDeleteFiles {
361    type Params = DeleteFilesParams;
362    const METHOD: &'static str = "workspace/didDeleteFiles";
363}
364
365#[cfg(test)]
366mod test {
367    use super::*;
368
369    fn fake_call<N>()
370    where
371        N: Notification,
372        N::Params: serde::Serialize,
373    {
374    }
375
376    macro_rules! check_macro {
377        ($name:tt) => {
378            // check whether the macro name matches the method
379            assert_eq!(<lsp_notification!($name) as Notification>::METHOD, $name);
380            // test whether type checking passes for each component
381            fake_call::<lsp_notification!($name)>();
382        };
383    }
384
385    #[test]
386    fn check_macro_definitions() {
387        check_macro!("$/cancelRequest");
388        check_macro!("$/progress");
389        check_macro!("$/logTrace");
390        check_macro!("$/setTrace");
391        check_macro!("initialized");
392        check_macro!("exit");
393        check_macro!("window/showMessage");
394        check_macro!("window/logMessage");
395        check_macro!("window/workDoneProgress/cancel");
396        check_macro!("telemetry/event");
397        check_macro!("textDocument/didOpen");
398        check_macro!("textDocument/didChange");
399        check_macro!("textDocument/willSave");
400        check_macro!("textDocument/didSave");
401        check_macro!("textDocument/didClose");
402        check_macro!("textDocument/publishDiagnostics");
403        check_macro!("workspace/didChangeConfiguration");
404        check_macro!("workspace/didChangeWatchedFiles");
405        check_macro!("workspace/didChangeWorkspaceFolders");
406        check_macro!("workspace/didCreateFiles");
407        check_macro!("workspace/didRenameFiles");
408        check_macro!("workspace/didDeleteFiles");
409    }
410
411    #[test]
412    #[cfg(feature = "proposed")]
413    fn check_proposed_macro_definitions() {}
414}