diff options
author | Blaž Hrastnik | 2021-02-26 06:52:43 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-02-26 06:52:43 +0000 |
commit | 1ae7c4339194c6f988202b24cd910d8c2edea737 (patch) | |
tree | f7aea3b0e26fc59f1bcb5693ee540fe31af8a8fa /helix-lsp | |
parent | 7162632eb739557ededaee902b23e48a577817b4 (diff) |
commands: = as range formatting (via lsp)
Diffstat (limited to 'helix-lsp')
-rw-r--r-- | helix-lsp/src/client.rs | 67 | ||||
-rw-r--r-- | helix-lsp/src/lib.rs | 9 |
2 files changed, 75 insertions, 1 deletions
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<Vec<lsp::TextEdit>> { + 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::<lsp::request::Formatting>(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<Vec<lsp::TextEdit>> { + 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::<lsp::request::RangeFormatting>(params) + .await?; + + Ok(response.unwrap_or_default()) + } } diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs index a51a11fc..93bc06b5 100644 --- a/helix-lsp/src/lib.rs +++ b/helix-lsp/src/lib.rs @@ -28,7 +28,7 @@ pub enum Error { pub mod util { use super::*; - use helix_core::{RopeSlice, State, Transaction}; + use helix_core::{Range, RopeSlice, State, Transaction}; pub fn lsp_pos_to_pos(doc: RopeSlice, pos: lsp::Position) -> usize { let line = doc.line_to_char(pos.line as usize); @@ -43,6 +43,13 @@ pub mod util { lsp::Position::new(line as u32, col as u32) } + pub fn range_to_lsp_range(doc: RopeSlice, range: Range) -> lsp::Range { + let start = pos_to_lsp_pos(doc, range.from()); + let end = pos_to_lsp_pos(doc, range.to()); + + lsp::Range::new(start, end) + } + pub fn generate_transaction_from_edits( state: &State, edits: Vec<lsp::TextEdit>, |