sync_lsp/text_document/
will_save_wait_until.rs

1//! implementation of the `textDocument/willSaveWaitUntil` notification.
2//! 
3//! # Usage
4//! Whenever a document is about to be saved, [`Server::on_will_save_wait_until`]
5//! gives the server a chance to modify the document before it is saved.
6
7use crate::TypeProvider;
8use crate::{Server, connection::Endpoint};
9use crate::connection::Callback;
10use serde::Deserialize;
11use super::will_save::TextDocumentSaveReason;
12use super::{TextDocumentIdentifer, TextEdit};
13
14#[derive(Default, Clone)]
15pub(crate) struct WillSaveWaitUntilOptions;
16
17#[derive(Deserialize)]
18#[serde(rename_all = "camelCase")]
19struct WillSaveWaitUntilTextDocumentParams {
20    text_document: TextDocumentIdentifer,
21    reason: TextDocumentSaveReason
22}
23
24impl WillSaveWaitUntilOptions {
25
26    pub(crate) const METHOD: &'static str = "textDocument/willSaveWaitUntil";
27    
28    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, WillSaveWaitUntilOptions> {
29        Endpoint::new(Callback::request(|_, _: WillSaveWaitUntilTextDocumentParams| Vec::<TextEdit>::new()))
30    }
31}
32
33impl<T: TypeProvider> Server<T> {
34
35    /// Sets the callback that will be called to [modify a document before it is saved](self).
36    /// 
37    /// # Argument
38    /// * `callback` - A callback which is called with the following parameters as soon as a document is about to be saved:
39    ///     * The server instance receiving the response.
40    ///     * The [`TextDocumentIdentifer`] of the target document.
41    ///     * The [`TextDocumentSaveReason`] that specifies why the document is saved.
42    ///     * `return` - A list of edits to apply to the document.
43
44    pub fn on_will_save_wait_until(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, TextDocumentSaveReason) -> Vec<TextEdit>) {
45        self.text_document.will_save_wait_until.set_callback(Callback::request(move |server, params: WillSaveWaitUntilTextDocumentParams| {
46            callback(server, params.text_document, params.reason)
47        }))
48    }
49}