Save buffers after restoring hunks in the project diff (#25620)

This PR fixes a bug where using the project diff editor to restore hunks
from a file that's not open in its own buffer would cause those reverts
to be lost once the project diff drops its excerpts for that file.

The fix is to save the buffers after restoring them but before the
excerpts are (potentially) dropped. This is done for the project diff
editor only. If we fail to save the affected files, we add their buffers
to the active workspace, so that the reverted contents are preserved and
the user can try again to save them.

- [x] Get it working
- [x] Test
- [ ] ~~Clean up boolean soup~~

Co-authored-by: Max <max@zed.dev>

Release Notes:

- N/A
This commit is contained in:
Cole Miller
2025-02-26 15:16:17 -05:00
committed by GitHub
co-authored by Max
parent add7ae8052
commit 7a34dd9888
10 changed files with 195 additions and 19 deletions
+41 -3
View File
@@ -7722,7 +7722,7 @@ impl Editor {
drop(chunk_by);
if !revert_changes.is_empty() {
self.transact(window, cx, |editor, window, cx| {
editor.revert(revert_changes, window, cx);
editor.restore(revert_changes, window, cx);
});
}
}
@@ -15886,13 +15886,16 @@ impl Editor {
FILE_HEADER_HEIGHT
}
pub fn revert(
pub fn restore(
&mut self,
revert_changes: HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.buffer().update(cx, |multi_buffer, cx| {
let workspace = self.workspace();
let project = self.project.as_ref();
let save_tasks = self.buffer().update(cx, |multi_buffer, cx| {
let mut tasks = Vec::new();
for (buffer_id, changes) in revert_changes {
if let Some(buffer) = multi_buffer.buffer(buffer_id) {
buffer.update(cx, |buffer, cx| {
@@ -15904,9 +15907,44 @@ impl Editor {
cx,
);
});
if let Some(project) =
project.filter(|_| multi_buffer.all_diff_hunks_expanded())
{
project.update(cx, |project, cx| {
tasks.push((buffer.clone(), project.save_buffer(buffer, cx)));
})
}
}
}
tasks
});
cx.spawn_in(window, |_, mut cx| async move {
for (buffer, task) in save_tasks {
let result = task.await;
if result.is_err() {
let Some(path) = buffer
.read_with(&cx, |buffer, cx| buffer.project_path(cx))
.ok()
else {
continue;
};
if let Some((workspace, path)) = workspace.as_ref().zip(path) {
let Some(task) = cx
.update_window_entity(&workspace, |workspace, window, cx| {
workspace
.open_path_preview(path, None, false, false, false, window, cx)
})
.ok()
else {
continue;
};
task.await.log_err();
}
}
}
})
.detach();
self.change_selections(None, window, cx, |selections| selections.refresh());
}