sync_lsp/text_document/
did_change.rs

1//! implementation of the `textDocument/didChange` notification
2//! 
3//! # Usage
4//! Whenever a document is changed, [`Server::on_change`] is invoked.
5//! The client should only send this if it claimed ownership of the document
6//! via [`Server::on_open`] before.
7
8use crate::TypeProvider;
9use crate::{Server, connection::Endpoint};
10use crate::connection::Callback;
11use serde::Deserialize;
12use super::{VersionedTextDocumentIdentifier, Range};
13
14#[derive(Default, Clone)]
15pub(crate) struct DidChangeOptions;
16
17/// A change to a text document.
18#[derive(Deserialize, Debug)]
19#[serde(rename_all = "camelCase")]
20pub struct TextDocumentContentChangeEvent {
21    /// If range is omitted, the new text is considered to be the full content of the document.
22    pub range: Option<Range>,
23    /// The length of the range that got replaced.
24    pub range_length: Option<i32>,
25    /// The new text of the range/document.
26    pub text: String,
27}
28
29#[derive(Deserialize)]
30#[serde(rename_all = "camelCase")]
31struct DidChangeTextDocumentParams {
32    text_document: VersionedTextDocumentIdentifier,
33    content_changes: Vec<TextDocumentContentChangeEvent>
34}
35
36impl DidChangeOptions {
37
38    pub(crate) const METHOD: &'static str = "textDocument/didChange";
39    
40    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DidChangeOptions> {
41        Endpoint::new(Callback::notification(|_, _: DidChangeTextDocumentParams| ()))
42    }
43}
44
45impl<T: TypeProvider> Server<T> {
46
47    /// Sets the callback that will be called if a [change to a file](self) is detected.
48    /// 
49    /// # Argument
50    /// * `callback` - A callback which is called with the following parameters as soon as a change is detected:
51    ///     * The server instance receiving the response.
52    ///     * The [`VersionedTextDocumentIdentifier`] of the document that changed.
53    ///     * The [`Vec<TextDocumentContentChangeEvent>`] that contains the changes to the document.
54    
55    pub fn on_change(&mut self, callback: fn(&mut Server<T>, VersionedTextDocumentIdentifier, Vec<TextDocumentContentChangeEvent>)) {
56        self.text_document.did_change.set_callback(Callback::notification(move |server, params: DidChangeTextDocumentParams| {
57            callback(server, params.text_document, params.content_changes)
58        }));
59    }
60}