From 1ae7c4339194c6f988202b24cd910d8c2edea737 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 26 Feb 2021 15:52:43 +0900 Subject: commands: = as range formatting (via lsp) --- helix-lsp/src/client.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'helix-lsp/src/client.rs') diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 77e877e4..45998c8c 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -506,4 +506,71 @@ impl Client { Ok(response) } + + // formatting + + pub async fn text_document_formatting( + &self, + text_document: lsp::TextDocumentIdentifier, + options: lsp::FormattingOptions, + ) -> anyhow::Result> { + let capabilities = self.capabilities.as_ref().unwrap(); // TODO: needs post init + + // check if we're able to format + let _capabilities = match capabilities.document_formatting_provider { + Some(lsp::OneOf::Left(true)) => (), + Some(lsp::OneOf::Right(_)) => (), + // None | Some(false) + _ => return Ok(Vec::new()), + }; + // TODO: return err::unavailable so we can fall back to tree sitter formatting + + let params = lsp::DocumentFormattingParams { + text_document, + options, + // TODO: support these tokens + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + }; + + let response = self.request::(params).await?; + + Ok(response.unwrap_or_default()) + } + + pub async fn text_document_range_formatting( + &self, + text_document: lsp::TextDocumentIdentifier, + range: lsp::Range, + options: lsp::FormattingOptions, + ) -> anyhow::Result> { + let capabilities = self.capabilities.as_ref().unwrap(); // TODO: needs post init + + log::info!("{:?}", capabilities.document_range_formatting_provider); + // check if we're able to format + let _capabilities = match capabilities.document_range_formatting_provider { + Some(lsp::OneOf::Left(true)) => (), + Some(lsp::OneOf::Right(_)) => (), + // None | Some(false) + _ => return Ok(Vec::new()), + }; + // TODO: return err::unavailable so we can fall back to tree sitter formatting + + let params = lsp::DocumentRangeFormattingParams { + text_document, + range, + options, + // TODO: support these tokens + work_done_progress_params: lsp::WorkDoneProgressParams { + work_done_token: None, + }, + }; + + let response = self + .request::(params) + .await?; + + Ok(response.unwrap_or_default()) + } } -- cgit v1.2.3-70-g09d2