sync_lsp/text_document/
formatting.rs1use crate::TypeProvider;
8use crate::{Server, connection::Endpoint};
9use crate::connection::Callback;
10use super::{TextDocumentIdentifer, TextEdit};
11use serde::Deserialize;
12
13#[derive(Default, Clone)]
14pub(crate) struct DocumentFormattingOptions;
15
16#[derive(Deserialize)]
17#[serde(rename_all = "camelCase")]
18struct DocumentFormattingParams {
19 text_document: TextDocumentIdentifer,
20 options: FormattingOptions
21}
22
23#[derive(Deserialize, Debug)]
25#[serde(rename_all = "camelCase")]
26pub struct FormattingOptions<T = ()> {
27 pub tab_size: u32,
29 pub insert_spaces: bool,
31 #[serde(flatten)]
33 pub properties: T
34}
35
36impl DocumentFormattingOptions {
37
38 pub(crate) const METHOD: &'static str = "textDocument/formatting";
39
40 pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, DocumentFormattingOptions> {
41 Endpoint::new(Callback::request(|_, _: DocumentFormattingParams| Vec::<TextEdit>::new()))
42 }
43}
44
45impl<T: TypeProvider> Server<T> {
46
47 pub fn on_formatting(&mut self, callback: fn(&mut Server<T>, TextDocumentIdentifer, FormattingOptions) -> Vec<TextEdit>) {
57 self.text_document.formatting.set_callback(Callback::request(move |server, params: DocumentFormattingParams | {
58 callback(server, params.text_document, params.options)
59 }))
60 }
61}