lsp: refactor format so we stop cloning the language_server

imgbot
Blaž Hrastnik 3 years ago
parent c00cf238af
commit 184637c55a

@ -583,19 +583,19 @@ impl Client {
// formatting // formatting
pub async fn text_document_formatting( pub fn text_document_formatting(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
options: lsp::FormattingOptions, options: lsp::FormattingOptions,
work_done_token: Option<lsp::ProgressToken>, work_done_token: Option<lsp::ProgressToken>,
) -> anyhow::Result<Vec<lsp::TextEdit>> { ) -> Option<impl Future<Output = Result<Vec<lsp::TextEdit>>>> {
let capabilities = self.capabilities.get().unwrap(); let capabilities = self.capabilities.get().unwrap();
// check if we're able to format // check if we're able to format
match capabilities.document_formatting_provider { match capabilities.document_formatting_provider {
Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => (), Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => (),
// None | Some(false) // None | Some(false)
_ => return Ok(Vec::new()), _ => return None,
}; };
// TODO: return err::unavailable so we can fall back to tree sitter formatting // TODO: return err::unavailable so we can fall back to tree sitter formatting
@ -605,9 +605,13 @@ impl Client {
work_done_progress_params: lsp::WorkDoneProgressParams { work_done_token }, work_done_progress_params: lsp::WorkDoneProgressParams { work_done_token },
}; };
let response = self.request::<lsp::request::Formatting>(params).await?; let request = self.call::<lsp::request::Formatting>(params);
Ok(response.unwrap_or_default()) Some(async move {
let json = request.await?;
let response: Vec<lsp::TextEdit> = serde_json::from_value(json)?;
Ok(response)
})
} }
pub async fn text_document_range_formatting( pub async fn text_document_range_formatting(

@ -386,21 +386,24 @@ impl Document {
/// If supported, returns the changes that should be applied to this document in order /// If supported, returns the changes that should be applied to this document in order
/// to format it nicely. /// to format it nicely.
pub fn format(&self) -> Option<impl Future<Output = LspFormatting> + 'static> { pub fn format(&self) -> Option<impl Future<Output = LspFormatting> + 'static> {
if let Some(language_server) = self.language_server.clone() { if let Some(language_server) = self.language_server() {
let text = self.text.clone(); let text = self.text.clone();
let id = self.identifier(); let offset_encoding = language_server.offset_encoding();
let request = language_server.text_document_formatting(
self.identifier(),
lsp::FormattingOptions::default(),
None,
)?;
let fut = async move { let fut = async move {
let edits = language_server let edits = request.await.unwrap_or_else(|e| {
.text_document_formatting(id, lsp::FormattingOptions::default(), None) log::warn!("LSP formatting failed: {}", e);
.await Default::default()
.unwrap_or_else(|e| { });
log::warn!("LSP formatting failed: {}", e);
Default::default()
});
LspFormatting { LspFormatting {
doc: text, doc: text,
edits, edits,
offset_encoding: language_server.offset_encoding(), offset_encoding,
} }
}; };
Some(fut) Some(fut)

Loading…
Cancel
Save