Move Buffer::format to Project::format

This commit is contained in:
Antonio Scandurra 2022-02-11 11:05:25 +01:00
parent 645df73a37
commit 4929b8c525
10 changed files with 290 additions and 177 deletions

View file

@ -201,9 +201,6 @@ pub trait File {
cx: &mut MutableAppContext,
) -> Task<Result<(clock::Global, SystemTime)>>;
fn format_remote(&self, buffer_id: u64, cx: &mut MutableAppContext)
-> Option<Task<Result<()>>>;
fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext);
fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext);
@ -278,10 +275,6 @@ impl File for FakeFile {
cx.spawn(|_| async move { Ok((Default::default(), SystemTime::UNIX_EPOCH)) })
}
fn format_remote(&self, _: u64, _: &mut MutableAppContext) -> Option<Task<Result<()>>> {
None
}
fn buffer_updated(&self, _: u64, _: Operation, _: &mut MutableAppContext) {}
fn buffer_removed(&self, _: u64, _: &mut MutableAppContext) {}
@ -540,52 +533,6 @@ impl Buffer {
self.file.as_deref()
}
pub fn format(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
let file = if let Some(file) = self.file.as_ref() {
file
} else {
return Task::ready(Err(anyhow!("buffer has no file")));
};
if let Some(LanguageServerState { server, .. }) = self.language_server.as_ref() {
let server = server.clone();
let abs_path = file.as_local().unwrap().abs_path(cx);
let version = self.version();
cx.spawn(|this, mut cx| async move {
let edits = server
.request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
text_document: lsp::TextDocumentIdentifier::new(
lsp::Url::from_file_path(&abs_path).unwrap(),
),
options: Default::default(),
work_done_progress_params: Default::default(),
})
.await?;
if let Some(edits) = edits {
this.update(&mut cx, |this, cx| {
if this.version == version {
this.apply_lsp_edits(edits, None, cx)?;
Ok(())
} else {
Err(anyhow!("buffer edited since starting to format"))
}
})
} else {
Ok(())
}
})
} else {
let format = file.format_remote(self.remote_id(), cx.as_mut());
cx.spawn(|_, _| async move {
if let Some(format) = format {
format.await?;
}
Ok(())
})
}
}
pub fn save(
&mut self,
cx: &mut ModelContext<Self>,