From 71082d4cdc3697005854b1cb0d6be8d1e3731e65 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 Jan 2022 09:58:24 +0100 Subject: [PATCH] Return a `Task>` in `{ItemView,Buffer,MultiBuffer}::save` --- crates/diagnostics/src/diagnostics.rs | 2 +- crates/editor/src/items.rs | 10 ++++------ crates/editor/src/multi_buffer.rs | 8 ++++---- crates/language/src/buffer.rs | 15 ++++++++------- crates/project/src/worktree.rs | 6 +++--- crates/server/src/rpc.rs | 3 +-- crates/workspace/src/workspace.rs | 15 ++++++--------- 7 files changed, 27 insertions(+), 32 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 2f253bff28..5c0fcc38bb 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -560,7 +560,7 @@ impl workspace::ItemView for ProjectDiagnosticsEditor { true } - fn save(&mut self, cx: &mut ViewContext) -> Result>> { + fn save(&mut self, cx: &mut ViewContext) -> Task> { self.excerpts.update(cx, |excerpts, cx| excerpts.save(cx)) } diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 24dbebb6c7..e534f3b8fa 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -160,20 +160,18 @@ impl ItemView for Editor { self.project_entry(cx).is_some() } - fn save(&mut self, cx: &mut ViewContext) -> Result>> { + fn save(&mut self, cx: &mut ViewContext) -> Task> { let buffer = self.buffer().clone(); - Ok(cx.spawn(|editor, mut cx| async move { + cx.spawn(|editor, mut cx| async move { buffer .update(&mut cx, |buffer, cx| buffer.format(cx).log_err()) .await; editor.update(&mut cx, |editor, cx| { editor.request_autoscroll(Autoscroll::Fit, cx) }); - buffer - .update(&mut cx, |buffer, cx| buffer.save(cx))? - .await?; + buffer.update(&mut cx, |buffer, cx| buffer.save(cx)).await?; Ok(()) - })) + }) } fn can_save_as(&self, _: &AppContext) -> bool { diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 7ae7b56867..04190ed39a 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -812,18 +812,18 @@ impl MultiBuffer { }) } - pub fn save(&mut self, cx: &mut ModelContext) -> Result>> { + pub fn save(&mut self, cx: &mut ModelContext) -> Task> { let mut save_tasks = Vec::new(); for BufferState { buffer, .. } in self.buffers.borrow().values() { - save_tasks.push(buffer.update(cx, |buffer, cx| buffer.save(cx))?); + save_tasks.push(buffer.update(cx, |buffer, cx| buffer.save(cx))); } - Ok(cx.spawn(|_, _| async move { + cx.spawn(|_, _| async move { for save in save_tasks { save.await?; } Ok(()) - })) + }) } pub fn language<'a>(&self, cx: &'a AppContext) -> Option<&'a Arc> { diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index d0410b4701..042c014844 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -510,21 +510,22 @@ impl Buffer { pub fn save( &mut self, cx: &mut ModelContext, - ) -> Result>> { - let file = self - .file - .as_ref() - .ok_or_else(|| anyhow!("buffer has no file"))?; + ) -> Task> { + let file = if let Some(file) = self.file.as_ref() { + file + } else { + return Task::ready(Err(anyhow!("buffer has no file"))); + }; let text = self.as_rope().clone(); let version = self.version(); let save = file.save(self.remote_id(), text, version, cx.as_mut()); - Ok(cx.spawn(|this, mut cx| async move { + cx.spawn(|this, mut cx| async move { let (version, mtime) = save.await?; this.update(&mut cx, |this, cx| { this.did_save(version.clone(), mtime, None, cx); }); Ok((version, mtime)) - })) + }) } pub fn set_language(&mut self, language: Option>, cx: &mut ModelContext) { diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index af28441f15..9c322f267e 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -455,7 +455,7 @@ impl Worktree { let worktree_id = envelope.payload.worktree_id; let buffer_id = envelope.payload.buffer_id; let save = cx.spawn(|_, mut cx| async move { - buffer.update(&mut cx, |buffer, cx| buffer.save(cx))?.await + buffer.update(&mut cx, |buffer, cx| buffer.save(cx)).await }); cx.background() @@ -3094,7 +3094,7 @@ mod tests { .unwrap(); let save = buffer.update(&mut cx, |buffer, cx| { buffer.edit(Some(0..0), "a line of text.\n".repeat(10 * 1024), cx); - buffer.save(cx).unwrap() + buffer.save(cx) }); save.await.unwrap(); @@ -3132,7 +3132,7 @@ mod tests { .unwrap(); let save = buffer.update(&mut cx, |buffer, cx| { buffer.edit(Some(0..0), "a line of text.\n".repeat(10 * 1024), cx); - buffer.save(cx).unwrap() + buffer.save(cx) }); save.await.unwrap(); diff --git a/crates/server/src/rpc.rs b/crates/server/src/rpc.rs index cf00149fb1..adfbeacd13 100644 --- a/crates/server/src/rpc.rs +++ b/crates/server/src/rpc.rs @@ -1474,7 +1474,7 @@ mod tests { .await; // Edit the buffer as the host and concurrently save as guest B. - let save_b = buffer_b.update(&mut cx_b, |buf, cx| buf.save(cx).unwrap()); + let save_b = buffer_b.update(&mut cx_b, |buf, cx| buf.save(cx)); buffer_a.update(&mut cx_a, |buf, cx| buf.edit([0..0], "hi-a, ", cx)); save_b.await.unwrap(); assert_eq!( @@ -1591,7 +1591,6 @@ mod tests { buffer_b .update(&mut cx_b, |buf, cx| buf.save(cx)) - .unwrap() .await .unwrap(); worktree_b diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 8fc329e4b5..242fbcf560 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -168,14 +168,14 @@ pub trait ItemView: View { false } fn can_save(&self, cx: &AppContext) -> bool; - fn save(&mut self, cx: &mut ViewContext) -> Result>>; + fn save(&mut self, cx: &mut ViewContext) -> Task>; fn can_save_as(&self, cx: &AppContext) -> bool; fn save_as( &mut self, project: ModelHandle, abs_path: PathBuf, cx: &mut ViewContext, - ) -> Task>; + ) -> Task>; fn should_activate_item_on_event(_: &Self::Event) -> bool { false } @@ -222,7 +222,7 @@ pub trait ItemViewHandle { fn has_conflict(&self, cx: &AppContext) -> bool; fn can_save(&self, cx: &AppContext) -> bool; fn can_save_as(&self, cx: &AppContext) -> bool; - fn save(&self, cx: &mut MutableAppContext) -> Result>>; + fn save(&self, cx: &mut MutableAppContext) -> Task>; fn save_as( &self, project: ModelHandle, @@ -377,7 +377,7 @@ impl ItemViewHandle for ViewHandle { self.update(cx, |this, cx| this.navigate(data, cx)); } - fn save(&self, cx: &mut MutableAppContext) -> Result>> { + fn save(&self, cx: &mut MutableAppContext) -> Task> { self.update(cx, |item, cx| item.save(cx)) } @@ -822,15 +822,12 @@ impl Workspace { cx.spawn(|_, mut cx| async move { let answer = answer.recv().await; if answer == Some(0) { - cx.update(|cx| item.save(cx))?.await?; + cx.update(|cx| item.save(cx)).await?; } Ok(()) }) } else { - cx.spawn(|_, mut cx| async move { - cx.update(|cx| item.save(cx))?.await?; - Ok(()) - }) + item.save(cx) } } else if item.can_save_as(cx) { let worktree = self.worktrees(cx).first();