sync_lsp/workspace/
did_change_watched_files.rs

1//! implementation of the `workspace/didChangeWatchedFiles` notification
2//! 
3//! # Usage
4//! The [`Server::on_change_watched_files`] notification signals changes to files or folders watched by the server.
5//! The recomended way for servers to collect file changes is this notification, 
6//! tough it's not the only way. Servers may use other ways like polling file system for changes.
7
8use serde_repr::Deserialize_repr;
9use serde::Deserialize;
10use crate::{Server, TypeProvider};
11use crate::connection::{Endpoint, Callback};
12
13#[derive(Default, Clone)]
14pub(crate) struct DidChangeWatchedFilesOptions;
15
16/// A file event that is sent by the client when a file is created, changed or deleted.
17#[derive(Deserialize, Debug)]
18pub struct FileEvent {
19    /// A file URI.
20    pub uri: String,
21    /// The file change type as defined in [`FileChangeType`].
22    #[serde(rename = "type")]
23    pub r#type: FileChangeType
24}
25
26/// The file change type attached to every [`FileEvent`].
27#[repr(i32)]
28#[derive(Deserialize_repr, Debug)]
29pub enum FileChangeType {
30    /// Denotes the creation of a file.
31    Created = 1,
32    /// Denotes a change to a file.
33    Changed = 2,
34    /// Denotes the deletion of a file.
35    Deleted = 3
36}
37
38#[derive(Deserialize)]
39struct DidChangeWatchedFilesParams {
40    changes: Vec<FileEvent>
41}
42
43impl<T: TypeProvider> Server<T> {
44    
45    /// Sets the callback that will be called when [watched files are changed](self).
46    /// 
47    /// # Argument
48    /// * `callback` - A callback which is called with the following parameters as soon as watch file changes are received:
49    ///     * The server instance receiving the response.
50    ///     * A vector of [`FileEvent`]s.
51
52    pub fn on_change_watched_files(&mut self, callback: fn (&mut Server<T>, Vec<FileEvent>)) {
53        self.workspace.did_change_watched_files.set_callback(Callback::notification(move |server, params: DidChangeWatchedFilesParams| {
54            callback(server, params.changes)
55        }))
56    }
57}
58
59impl DidChangeWatchedFilesOptions {
60    pub(super) const METHOD: &'static str = "workspace/didChangeWatchedFiles";
61
62    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DidChangeWatchedFilesOptions> {
63        Endpoint::<T, DidChangeWatchedFilesOptions>::new(
64            Callback::notification(|_, _: DidChangeWatchedFilesParams| ())
65        )
66    }
67}