@@ -506,4 +506,71 @@ impl Client {
506506
507507 Ok(response)
508508 }
509+
510+ // formatting
511+
512+ pub async fn text_document_formatting(
513+ &self,
514+ text_document: lsp::TextDocumentIdentifier,
515+ options: lsp::FormattingOptions,
516+ ) -> anyhow::Result<Vec<lsp::TextEdit>> {
517+ let capabilities = self.capabilities.as_ref().unwrap(); // TODO: needs post init
518+
519+ // check if we're able to format
520+ let _capabilities = match capabilities.document_formatting_provider {
521+ Some(lsp::OneOf::Left(true)) => (),
522+ Some(lsp::OneOf::Right(_)) => (),
523+ // None | Some(false)
524+ _ => return Ok(Vec::new()),
525+ };
526+ // TODO: return err::unavailable so we can fall back to tree sitter formatting
527+
528+ let params = lsp::DocumentFormattingParams {
529+ text_document,
530+ options,
531+ // TODO: support these tokens
532+ work_done_progress_params: lsp::WorkDoneProgressParams {
533+ work_done_token: None,
534+ },
535+ };
536+
537+ let response = self.request::<lsp::request::Formatting>(params).await?;
538+
539+ Ok(response.unwrap_or_default())
540+ }
541+
542+ pub async fn text_document_range_formatting(
543+ &self,
544+ text_document: lsp::TextDocumentIdentifier,
545+ range: lsp::Range,
546+ options: lsp::FormattingOptions,
547+ ) -> anyhow::Result<Vec<lsp::TextEdit>> {
548+ let capabilities = self.capabilities.as_ref().unwrap(); // TODO: needs post init
549+
550+ log::info!("{:?}", capabilities.document_range_formatting_provider);
551+ // check if we're able to format
552+ let _capabilities = match capabilities.document_range_formatting_provider {
553+ Some(lsp::OneOf::Left(true)) => (),
554+ Some(lsp::OneOf::Right(_)) => (),
555+ // None | Some(false)
556+ _ => return Ok(Vec::new()),
557+ };
558+ // TODO: return err::unavailable so we can fall back to tree sitter formatting
559+
560+ let params = lsp::DocumentRangeFormattingParams {
561+ text_document,
562+ range,
563+ options,
564+ // TODO: support these tokens
565+ work_done_progress_params: lsp::WorkDoneProgressParams {
566+ work_done_token: None,
567+ },
568+ };
569+
570+ let response = self
571+ .request::<lsp::request::RangeFormatting>(params)
572+ .await?;
573+
574+ Ok(response.unwrap_or_default())
575+ }
509576}