on master help sign in
lsp: refactor format so we stop cloning the language_server
commit184637c55acca49380372ca118f13b3390bcb003 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 1 Sep 2021, 14:49 (5 years ago) parentc00cf23
2 files changed+22−15
helix-lsp/src/client.rs+9−5 helix-view/src/document.rs+13−10
helix-lsp/src/client.rs+9−5
@@ -583,19 +583,19 @@ impl Client {
583583
584584 // formatting
585585
586− pub async fn text_document_formatting(
586+ pub fn text_document_formatting(
587587 &self,
588588 text_document: lsp::TextDocumentIdentifier,
589589 options: lsp::FormattingOptions,
590590 work_done_token: Option<lsp::ProgressToken>,
591− ) -> anyhow::Result<Vec<lsp::TextEdit>> {
591+ ) -> Option<impl Future<Output = Result<Vec<lsp::TextEdit>>>> {
592592 let capabilities = self.capabilities.get().unwrap();
593593
594594 // check if we're able to format
595595 match capabilities.document_formatting_provider {
596596 Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => (),
597597 // None | Some(false)
598− _ => return Ok(Vec::new()),
598+ _ => return None,
599599 };
600600 // TODO: return err::unavailable so we can fall back to tree sitter formatting
601601
@@ -605,9 +605,13 @@ impl Client {
605605 work_done_progress_params: lsp::WorkDoneProgressParams { work_done_token },
606606 };
607607
608− let response = self.request::<lsp::request::Formatting>(params).await?;
608+ let request = self.call::<lsp::request::Formatting>(params);
609609
610− Ok(response.unwrap_or_default())
610+ Some(async move {
611+ let json = request.await?;
612+ let response: Vec<lsp::TextEdit> = serde_json::from_value(json)?;
613+ Ok(response)
614+ })
611615 }
612616
613617 pub async fn text_document_range_formatting(
helix-view/src/document.rs+13−10
@@ -386,21 +386,24 @@ impl Document {
386386 /// If supported, returns the changes that should be applied to this document in order
387387 /// to format it nicely.
388388 pub fn format(&self) -> Option<impl Future<Output = LspFormatting> + 'static> {
389− if let Some(language_server) = self.language_server.clone() {
389+ if let Some(language_server) = self.language_server() {
390390 let text = self.text.clone();
391− let id = self.identifier();
391+ let offset_encoding = language_server.offset_encoding();
392+ let request = language_server.text_document_formatting(
393+ self.identifier(),
394+ lsp::FormattingOptions::default(),
395+ None,
396+ )?;
397+
392398 let fut = async move {
393− let edits = language_server
394− .text_document_formatting(id, lsp::FormattingOptions::default(), None)
395− .await
396− .unwrap_or_else(|e| {
397− log::warn!("LSP formatting failed: {}", e);
398− Default::default()
399− });
399+ let edits = request.await.unwrap_or_else(|e| {
400+ log::warn!("LSP formatting failed: {}", e);
401+ Default::default()
402+ });
400403 LspFormatting {
401404 doc: text,
402405 edits,
403− offset_encoding: language_server.offset_encoding(),
406+ offset_encoding,
404407 }
405408 };
406409 Some(fut)