1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! implementation of the `textDocument/formatting` request
//!
//! # Usage
//! Some server may be capable of formatting a document. If so, [`Server::on_formatting`] can be used to
//! provide this functionality to the client. It can be triggered either manually or automatically.
use crate::TypeProvider;
use crate::{Server, connection::Endpoint};
use crate::connection::Callback;
use super::{TextDocumentIdentifer, TextEdit};
use serde::Deserialize;
#[derive(Default, Clone)]
pub(crate) struct DocumentFormattingOptions;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct DocumentFormattingParams {
text_document: TextDocumentIdentifer,
options: FormattingOptions
}
/// Specifies how a document should be formatted.
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct FormattingOptions<T = ()> {
/// The size of tabs in spaces.
pub tab_size: u32,
/// Specifies whether tabs should be used instead of spaces.
pub insert_spaces: bool,
/// Additional options can be handled by the server if a struct with the corresponding fields is provided here.
#[serde(flatten)]
pub properties: T
}
impl DocumentFormattingOptions {
pub(crate) const METHOD: &'static str = "textDocument/formatting";
pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DocumentFormattingOptions> {
Endpoint::new(Callback::request(|_, _: DocumentFormattingParams| Vec::<TextEdit>::new()))
}
}
impl<T: TypeProvider> Server<T> {
/// Sets the callback that will be called to [format a document](self).
///
/// # Argument
/// * `callback` - A callback which is called with the following parameters as soon as a document is formatted:
/// * `server` - The server on which the request was received.
/// * `document` - The [`TextDocumentIdentifer`] of the target document.
/// * `options` - The [`FormattingOptions`] that specify how the document should be formatted.
/// * `return` - A list of edits to apply to the document.
pub fn on_formatting(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, FormattingOptions) -> Vec<TextEdit>) {
self.text_document.formatting.set_callback(Callback::request(move |server, params: DocumentFormattingParams | {
callback(server, params.text_document, params.options)
}))
}
}