sync_lsp/text_document/
did_save.rs

1//! implementation of the `textDocument/onSave` notification
2//! 
3//! # Usage
4//! Whenever a document is saved, [`Server::on_save`] is invoked. Optionally,
5//! [`Server::set_save_include_text`] can be used to include the content of the
6//! file on save.
7
8
9use crate::{Server, TypeProvider};
10use crate::connection::{Callback, Endpoint};
11use serde::Deserialize;
12use super::TextDocumentIdentifer;
13use serde::Serialize;
14
15#[derive(Serialize, Default, Clone)]
16#[serde(rename_all = "camelCase")]
17pub(crate) struct DidSaveOptions {
18    include_text: bool
19}
20
21#[derive(Deserialize)]
22#[serde(rename_all = "camelCase")]
23struct DidSaveTextDocumentParams {
24    text_document: TextDocumentIdentifer,
25    text: Option<String>,
26}
27
28
29impl DidSaveOptions {
30
31    pub(crate) const METHOD: &'static str = "textDocument/didSave";
32    
33    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DidSaveOptions> {
34        Endpoint::new(Callback::notification(|_, _: DidSaveTextDocumentParams| ()))
35    }
36}
37
38impl<T: TypeProvider> Server<T> {
39
40    /// Sets the callback that will be called if a [file is saved](self).
41    /// 
42    /// # Argument
43    /// * `callback` - A callback which is called with the following parameters as soon as a file is saved:
44    ///     * The server instance receiving the response.
45    ///     * The [`TextDocumentIdentifer`] of the saved document.
46    ///     * The content of the file, if enabled via [`Server::set_save_include_text`].
47
48    pub fn on_save(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Option<String>)) {
49        self.text_document.did_save.set_callback(Callback::notification(move |server, params: DidSaveTextDocumentParams| {
50            callback(server, params.text_document, params.text)
51        }))
52    }
53
54    /// Sets whether the content of the file should be included in the [`Server::on_save`] callback.
55    /// 
56    /// # Argument
57    /// * `value` - If `true`, the content of the file will be included in the callback.
58
59    pub fn set_save_include_text(&mut self, value: bool) {
60        self.text_document.did_save.options_mut().include_text = value;
61    }
62}