sync_lsp/text_document/
will_save.rs

1//! implementation of the `textDocument/willSave` notification.
2//! 
3//! # Usage
4//! Whenever a document is about to be saved, [`Server::on_will_save`] is called
5//! to notify the server.
6
7use crate::{Server, TypeProvider};
8use crate::connection::{Callback, Endpoint};
9use serde::Deserialize;
10use serde_repr::Deserialize_repr;
11use super::TextDocumentIdentifer;
12
13#[derive(Default, Clone)]
14pub(crate) struct WillSaveOptions;
15
16/// The reason why a text document is saved.
17#[repr(i32)]
18#[derive(Deserialize_repr, Debug)]
19pub enum TextDocumentSaveReason {
20    Manual = 1,
21    AfterDelay = 2,
22    FocusOut = 3
23}
24
25#[derive(Deserialize)]
26#[serde(rename_all = "camelCase")]
27struct WillSaveTextDocumentParams {
28    text_document: TextDocumentIdentifer,
29    reason: TextDocumentSaveReason
30}
31
32impl WillSaveOptions {
33
34    pub(crate) const METHOD: &'static str = "textDocument/willSave";
35    
36    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, WillSaveOptions> {
37        Endpoint::new(Callback::notification(|_, _: WillSaveTextDocumentParams| ()))
38    }
39}
40
41impl<T: TypeProvider> Server<T> {
42
43    /// Sets the callback that will be called to [notify the server that a document is about to be saved](self).
44    /// 
45    /// # Argument
46    /// * `callback` - A callback which is called with the following parameters as soon as a document is about to be saved:
47    ///     * The server instance receiving the response.
48    ///     * The [`TextDocumentIdentifer`] of the target document.
49    ///     * The [`TextDocumentSaveReason`] that specifies why the document is saved. 
50    
51    pub fn on_will_save(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, TextDocumentSaveReason)) {
52        self.text_document.will_save.set_callback(Callback::notification(move |server, params: WillSaveTextDocumentParams| {
53            callback(server, params.text_document, params.reason)
54        }))
55    }
56}