Make Project::save_buffer and ::save_buffers into methods

This commit is contained in:
Max Brunsfeld 2023-02-20 09:42:44 -08:00
parent 56b7eb6b6f
commit 010eba509c
5 changed files with 25 additions and 16 deletions

View File

@ -2244,7 +2244,7 @@ async fn test_propagate_saves_and_fs_changes(
});
// Edit the buffer as the host and concurrently save as guest B.
let save_b = cx_b.update(|cx| Project::save_buffer(buffer_b.clone(), cx));
let save_b = project_b.update(cx_b, |project, cx| project.save_buffer(buffer_b.clone(), cx));
buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "hi-a, ")], None, cx));
save_b.await.unwrap();
assert_eq!(
@ -2917,7 +2917,7 @@ async fn test_buffer_conflict_after_save(
assert!(!buf.has_conflict());
});
cx_b.update(|cx| Project::save_buffer(buffer_b.clone(), cx))
project_b.update(cx_b, |project, cx| project.save_buffer(buffer_b.clone(), cx))
.await
.unwrap();
cx_a.foreground().forbid_parking();

View File

@ -1073,7 +1073,7 @@ async fn randomly_query_and_mutate_buffers(
);
buffer.version()
});
let save = cx.update(|cx| Project::save_buffer(buffer, cx));
let save = project.update(cx, |project, cx| project.save_buffer(buffer, cx));
let save = cx.background().spawn(async move {
let (saved_version, _, _) = save
.await

View File

@ -610,9 +610,11 @@ impl Item for Editor {
self.report_event("save editor", cx);
let format = self.perform_format(project.clone(), cx);
let buffers = self.buffer().clone().read(cx).all_buffers();
cx.spawn(|_, mut cx| async move {
cx.as_mut().spawn(|mut cx| async move {
format.await?;
cx.update(|cx| Project::save_buffers(buffers, cx)).await?;
project
.update(&mut cx, |project, cx| project.save_buffers(buffers, cx))
.await?;
Ok(())
})
}

View File

@ -1429,21 +1429,23 @@ impl Project {
}
pub fn save_buffers(
&self,
buffers: HashSet<ModelHandle<Buffer>>,
cx: &mut MutableAppContext,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
cx.spawn(|mut cx| async move {
cx.spawn(|this, mut cx| async move {
let save_tasks = buffers
.into_iter()
.map(|buffer| cx.update(|cx| Self::save_buffer(buffer, cx)));
.map(|buffer| this.update(&mut cx, |this, cx| this.save_buffer(buffer, cx)));
try_join_all(save_tasks).await?;
Ok(())
})
}
pub fn save_buffer(
&self,
buffer: ModelHandle<Buffer>,
cx: &mut MutableAppContext,
cx: &mut ModelContext<Self>,
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
return Task::ready(Err(anyhow!("buffer doesn't have a file")));
@ -1460,7 +1462,7 @@ impl Project {
&mut self,
buffer: ModelHandle<Buffer>,
abs_path: PathBuf,
cx: &mut ModelContext<Project>,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let worktree_task = self.find_or_create_local_worktree(&abs_path, true, cx);
let old_path =
@ -5186,8 +5188,9 @@ impl Project {
})
.await;
let (saved_version, fingerprint, mtime) =
cx.update(|cx| Self::save_buffer(buffer, cx)).await?;
let (saved_version, fingerprint, mtime) = this
.update(&mut cx, |this, cx| this.save_buffer(buffer, cx))
.await?;
Ok(proto::BufferSaved {
project_id,
buffer_id,

View File

@ -243,7 +243,8 @@ async fn test_managing_language_servers(
);
// Save notifications are reported to all servers.
cx.update(|cx| Project::save_buffer(toml_buffer, cx))
project
.update(cx, |project, cx| project.save_buffer(toml_buffer, cx))
.await
.unwrap();
assert_eq!(
@ -2087,7 +2088,8 @@ async fn test_save_file(cx: &mut gpui::TestAppContext) {
buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], None, cx);
});
cx.update(|cx| Project::save_buffer(buffer.clone(), cx))
project
.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
.await
.unwrap();
@ -2115,7 +2117,8 @@ async fn test_save_in_single_file_worktree(cx: &mut gpui::TestAppContext) {
buffer.edit([(0..0, "a line of text.\n".repeat(10 * 1024))], None, cx);
});
cx.update(|cx| Project::save_buffer(buffer.clone(), cx))
project
.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
.await
.unwrap();
@ -2704,7 +2707,8 @@ async fn test_buffer_line_endings(cx: &mut gpui::TestAppContext) {
buffer2.update(cx, |buffer, cx| {
buffer.set_text("one\ntwo\nthree\nfour\n", cx);
});
cx.update(|cx| Project::save_buffer(buffer2, cx))
project
.update(cx, |project, cx| project.save_buffer(buffer2, cx))
.await
.unwrap();
assert_eq!(