sync_lsp/text_document/
range_formatting.rs

1//! implementation of the `textDocument/rangeFormatting` reques.
2//! 
3//! # Usage
4//! Via the [`Server::on_range_formatting`] callback a client is able to request the formatting
5//! of a specific range in a document.
6
7use crate::TypeProvider;
8use crate::{Server, connection::Endpoint};
9use crate::connection::Callback;
10use super::formatting::FormattingOptions;
11use super::{TextDocumentIdentifer, TextEdit, Range};
12use serde::Deserialize;
13
14#[derive(Default, Clone)]
15pub(crate) struct RangeFormattingOptions;
16
17#[derive(Deserialize)]
18#[serde(rename_all = "camelCase")]
19struct DocumentRangeFormattingParams {
20    text_document: TextDocumentIdentifer,
21    range: Range,
22    options: FormattingOptions
23}
24
25impl RangeFormattingOptions {
26
27    pub(crate) const METHOD: &'static str = "textDocument/rangeFormatting";
28    
29    pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, RangeFormattingOptions> {
30        Endpoint::new(Callback::request(|_, _: DocumentRangeFormattingParams| Vec::<TextEdit>::new()))
31    }
32}
33
34impl<T: TypeProvider> Server<T> {
35
36
37    /// Sets the callback that will be called to implement [range formatting](self).
38    ///
39    /// # Argument
40    /// * `callback` - A callback which is called with the following parameters as soon as a document range is formatted:
41    ///     * `server` - The server on which the request was received.
42    ///     * `document` - The [`TextDocumentIdentifer`] of the target document.
43    ///     * `range` - The [`Range`] that should be formatted.
44    ///     * `options` - The [`FormattingOptions`] that specify how the document should be formatted.
45    ///     * `return` - A list of edits to apply to the document.
46
47    pub fn on_range_formatting(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, Range, FormattingOptions) -> Vec<TextEdit>) {
48        self.text_document.range_formatting.set_callback(Callback::request(move |server, params: DocumentRangeFormattingParams | {
49            callback(server, params.text_document, params.range, params.options)
50        }))
51    }
52}