languageserver_types/
notification.rs

1use super::*;
2
3pub trait Notification {
4    type Params;
5    const METHOD: &'static str;
6}
7
8#[macro_export]
9macro_rules! lsp_notification {
10    ("$/cancelRequest") => {
11        $crate::notification::Cancel
12    };
13    ("initialized") => {
14        $crate::notification::Initialized
15    };
16    ("exit") => {
17        $crate::notification::Exit
18    };
19
20    ("window/showMessage") => {
21        $crate::notification::ShowMessage
22    };
23    ("window/logMessage") => {
24        $crate::notification::LogMessage
25    };
26
27    ("telemetry/event") => {
28        $crate::notification::TelemetryEvent
29    };
30
31    ("textDocument/didOpen") => {
32        $crate::notification::DidOpenTextDocument
33    };
34    ("textDocument/didChange") => {
35        $crate::notification::DidChangeTextDocument
36    };
37    ("textDocument/willSave") => {
38        $crate::notification::WillSaveTextDocument
39    };
40    ("textDocument/willSaveWaitUntil") => {
41        $crate::notification::WillSaveWaitUntil
42    };
43    ("textDocument/didSave") => {
44        $crate::notification::DidSaveTextDocument
45    };
46    ("textDocument/didClose") => {
47        $crate::notification::DidCloseTextDocument
48    };
49    ("textDocument/publishDiagnostics") => {
50        $crate::notification::PublishDiagnostics
51    };
52
53    ("workspace/didChangeConfiguration") => {
54        $crate::notification::DidChangeConfiguration
55    };
56    ("workspace/didChangeWatchedFiles") => {
57        $crate::notification::DidChangeWatchedFiles
58    };
59    ("workspace/didChangeWorkspaceFolders") => {
60        $crate::notification::DidChangeWorkspaceFolders
61    };
62}
63
64/// The base protocol now offers support for request cancellation. To cancel a request,
65/// a notification message with the following properties is sent:
66///
67/// A request that got canceled still needs to return from the server and send a response back.
68/// It can not be left open / hanging. This is in line with the JSON RPC protocol that requires
69/// that every request sends a response back. In addition it allows for returning partial results on cancel.
70#[derive(Debug)]
71pub enum Cancel {}
72
73impl Notification for Cancel {
74    type Params = CancelParams;
75    const METHOD: &'static str = "$/cancelRequest";
76}
77
78/// The initialized notification is sent from the client to the server after the client received
79/// the result of the initialize request but before the client is sending any other request or
80/// notification to the server. The server can use the initialized notification for example to
81/// dynamically register capabilities.
82#[derive(Debug)]
83pub enum Initialized {}
84
85impl Notification for Initialized {
86    type Params = InitializedParams;
87    const METHOD: &'static str = "initialized";
88}
89
90/**
91 * A notification to ask the server to exit its process.
92 * The server should exit with success code 0 if the shutdown request has been received before;
93 * otherwise with error code 1.
94 */
95#[derive(Debug)]
96pub enum Exit {}
97
98impl Notification for Exit {
99    type Params = ();
100    const METHOD: &'static str = "exit";
101}
102
103/**
104 * The show message notification is sent from a server to a client to ask the client to display a particular message
105 * in the user interface.
106 */
107#[derive(Debug)]
108pub enum ShowMessage {}
109
110impl Notification for ShowMessage {
111    type Params = ShowMessageParams;
112    const METHOD: &'static str = "window/showMessage";
113}
114
115/**
116 * The log message notification is sent from the server to the client to ask the client to log a particular message.
117 */
118#[derive(Debug)]
119pub enum LogMessage {}
120
121impl Notification for LogMessage {
122    type Params = LogMessageParams;
123    const METHOD: &'static str = "window/logMessage";
124}
125
126/**
127 * The telemetry notification is sent from the server to the client to ask the client to log a telemetry event.
128 */
129#[derive(Debug)]
130pub enum TelemetryEvent {}
131
132impl Notification for TelemetryEvent {
133    type Params = serde_json::Value;
134    const METHOD: &'static str = "telemetry/event";
135}
136
137/**
138 * A notification sent from the client to the server to signal the change of configuration settings.
139 */
140#[derive(Debug)]
141pub enum DidChangeConfiguration {}
142
143impl Notification for DidChangeConfiguration {
144    type Params = DidChangeConfigurationParams;
145    const METHOD: &'static str = "workspace/didChangeConfiguration";
146}
147
148/**
149 * The document open notification is sent from the client to the server to signal newly opened text documents.
150 * The document's truth is now managed by the client and the server must not try to read the document's truth
151 * using the document's uri.
152 */
153#[derive(Debug)]
154pub enum DidOpenTextDocument {}
155
156impl Notification for DidOpenTextDocument {
157    type Params = DidOpenTextDocumentParams;
158    const METHOD: &'static str = "textDocument/didOpen";
159}
160
161/**
162 * The document change notification is sent from the client to the server to signal changes to a text document.
163 * In 2.0 the shape of the params has changed to include proper version numbers and language ids.
164 */
165#[derive(Debug)]
166pub enum DidChangeTextDocument {}
167
168impl Notification for DidChangeTextDocument {
169    type Params = DidChangeTextDocumentParams;
170    const METHOD: &'static str = "textDocument/didChange";
171}
172
173/// The document will save notification is sent from the client to the server before the document
174/// is actually saved.
175#[derive(Debug)]
176pub enum WillSaveTextDocument {}
177
178impl Notification for WillSaveTextDocument {
179    type Params = WillSaveTextDocumentParams;
180    const METHOD: &'static str = "textDocument/willSave";
181}
182
183/// The document will save request is sent from the client to the server before the document is
184/// actually saved. The request can return an array of TextEdits which will be applied to the text
185/// document before it is saved. Please note that clients might drop results if computing the text
186/// edits took too long or if a server constantly fails on this request. This is done to keep the
187/// save fast and reliable.
188#[derive(Debug)]
189pub enum WillSaveWaitUntil {}
190
191impl Notification for WillSaveWaitUntil {
192    type Params = WillSaveTextDocumentParams;
193    const METHOD: &'static str = "textDocument/willSaveWaitUntil";
194}
195
196/**
197 * The document close notification is sent from the client to the server when the document got closed in the client.
198 * The document's truth now exists where the document's uri points to (e.g. if the document's uri is a file uri
199 * the truth now exists on disk).
200 */
201#[derive(Debug)]
202pub enum DidCloseTextDocument {}
203
204impl Notification for DidCloseTextDocument {
205    type Params = DidCloseTextDocumentParams;
206    const METHOD: &'static str = "textDocument/didClose";
207}
208
209/**
210 * The document save notification is sent from the client to the server when the document was saved in the client.
211 */
212#[derive(Debug)]
213pub enum DidSaveTextDocument {}
214
215impl Notification for DidSaveTextDocument {
216    type Params = DidSaveTextDocumentParams;
217    const METHOD: &'static str = "textDocument/didSave";
218}
219
220/**
221 * The watched files notification is sent from the client to the server when the client detects changes to files
222 * watched by the language client.
223 */
224#[derive(Debug)]
225pub enum DidChangeWatchedFiles {}
226
227impl Notification for DidChangeWatchedFiles {
228    type Params = DidChangeWatchedFilesParams;
229    const METHOD: &'static str = "workspace/didChangeWatchedFiles";
230}
231
232/**
233 * The workspace/didChangeWorkspaceFolders notification is sent from the client to the server to inform the server
234 * about workspace folder configuration changes
235 */
236#[derive(Debug)]
237pub enum DidChangeWorkspaceFolders {}
238
239impl Notification for DidChangeWorkspaceFolders {
240    type Params = DidChangeWorkspaceFoldersParams;
241    const METHOD: &'static str = "workspace/didChangeWorkspaceFolders";
242}
243
244/**
245 * Diagnostics notification are sent from the server to the client to signal results of validation runs.
246 */
247#[derive(Debug)]
248pub enum PublishDiagnostics {}
249
250impl Notification for PublishDiagnostics {
251    type Params = PublishDiagnosticsParams;
252    const METHOD: &'static str = "textDocument/publishDiagnostics";
253}
254
255#[cfg(test)]
256mod test {
257    use super::*;
258
259    fn fake_call<N>()
260    where
261        N: Notification,
262        N::Params: serde::Serialize,
263    {
264    }
265
266    macro_rules! check_macro {
267        ($name:tt) => {
268            // check whethe the macro name matches the method
269            assert_eq!(<lsp_notification!($name) as Notification>::METHOD, $name);
270            // test whether type checking passes for each component
271            fake_call::<lsp_notification!($name)>();
272        };
273    }
274
275    #[test]
276    fn check_macro_definitions() {
277        check_macro!("$/cancelRequest");
278        check_macro!("initialized");
279        check_macro!("exit");
280        check_macro!("window/showMessage");
281        check_macro!("window/logMessage");
282        check_macro!("telemetry/event");
283        check_macro!("textDocument/didOpen");
284        check_macro!("textDocument/didChange");
285        check_macro!("textDocument/willSave");
286        check_macro!("textDocument/willSaveWaitUntil");
287        check_macro!("textDocument/didSave");
288        check_macro!("textDocument/didClose");
289        check_macro!("textDocument/publishDiagnostics");
290        check_macro!("workspace/didChangeConfiguration");
291        check_macro!("workspace/didChangeWatchedFiles");
292        check_macro!("workspace/didChangeWorkspaceFolders");
293    }
294}