sync_lsp/workspace/did_change_configuration.rs
1//! Implementation of the `worksapce/didChangeConfiguration` notification.
2//!
3//! # Usage
4//! A server might require certain settings to be transferred from the client.
5//! These include settings like the format of a file or the location of a specific resource.
6//! Usually there is a ui provided for the user to change these settings. The
7//! [`Server::on_change_configuration`] notification is only triggered when the user
8//! changes these settings. Note that in the current version of this library,
9//! there is no way to get the current settings from the client by polling.
10
11use crate::{Server, TypeProvider};
12use crate::connection::{Callback, Endpoint};
13use serde::Deserialize;
14use serde_json::Value;
15
16#[derive(Default, Clone)]
17pub(crate) struct DidChangeConfigurationOptions;
18
19#[derive(Deserialize)]
20#[serde(rename_all = "camelCase")]
21struct DidChangeConfigurationParams<T> {
22 settings: T
23}
24
25impl DidChangeConfigurationOptions {
26
27 pub(crate) const METHOD: &'static str = "workspace/didChangeConfiguration";
28
29 pub(crate) fn endpoint<T: TypeProvider>() -> Endpoint<T, DidChangeConfigurationOptions> {
30 Endpoint::new(Callback::notification(|_, _: DidChangeConfigurationParams<Value>| ()))
31 }
32}
33
34impl<T: TypeProvider> Server<T> {
35
36 /// Set the request handler for [changing the server configuration](self)
37 ///
38 /// # Argument
39 /// * `callback` - A callback which is called with the following parameters if a change in configuration is received:
40 /// * The server instance receiving the response.
41 /// * The updated configuration of type [`TypeProvider::Configuration`].
42
43 pub fn on_change_configuration(&mut self, callback: fn(&mut Server<T>, T::Configuration)) {
44 self.workspace.did_change_configuration.set_callback(Callback::notification(move |server, params: DidChangeConfigurationParams<T::Configuration>| {
45 callback(server, params.settings)
46 }))
47 }
48}