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
parent add7ae8052
commit 7a34dd9888
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 195 additions and 19 deletions

View file

@ -849,6 +849,7 @@ impl Pane {
project_entry_id: Option<ProjectEntryId>,
focus_item: bool,
allow_preview: bool,
activate: bool,
suggested_position: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
@ -876,7 +877,9 @@ impl Pane {
}
}
}
self.activate_item(index, focus_item, focus_item, window, cx);
if activate {
self.activate_item(index, focus_item, focus_item, window, cx);
}
existing_item
} else {
// If the item is being opened as preview and we have an existing preview tab,
@ -892,10 +895,11 @@ impl Pane {
if allow_preview {
self.set_preview_item_id(Some(new_item.item_id()), cx);
}
self.add_item(
self.add_item_inner(
new_item.clone(),
true,
focus_item,
activate,
destination_index,
window,
cx,
@ -924,11 +928,13 @@ impl Pane {
}
}
pub fn add_item(
#[allow(clippy::too_many_arguments)]
pub fn add_item_inner(
&mut self,
item: Box<dyn ItemHandle>,
activate_pane: bool,
focus_item: bool,
activate: bool,
destination_index: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
@ -1015,23 +1021,47 @@ impl Pane {
cx.notify();
}
self.activate_item(insertion_index, activate_pane, focus_item, window, cx);
if activate {
self.activate_item(insertion_index, activate_pane, focus_item, window, cx);
}
} else {
self.items.insert(insertion_index, item.clone());
if insertion_index <= self.active_item_index
&& self.preview_item_idx() != Some(self.active_item_index)
{
self.active_item_index += 1;
}
if activate {
if insertion_index <= self.active_item_index
&& self.preview_item_idx() != Some(self.active_item_index)
{
self.active_item_index += 1;
}
self.activate_item(insertion_index, activate_pane, focus_item, window, cx);
self.activate_item(insertion_index, activate_pane, focus_item, window, cx);
}
cx.notify();
}
cx.emit(Event::AddItem { item });
}
pub fn add_item(
&mut self,
item: Box<dyn ItemHandle>,
activate_pane: bool,
focus_item: bool,
destination_index: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.add_item_inner(
item,
activate_pane,
focus_item,
true,
destination_index,
window,
cx,
)
}
pub fn items_len(&self) -> usize {
self.items.len()
}
@ -2941,6 +2971,7 @@ impl Pane {
project_entry_id,
true,
false,
true,
target,
window,
cx,

View file

@ -1499,6 +1499,7 @@ impl Workspace {
project_entry_id,
true,
entry.is_preview,
true,
None,
window, cx,
build_item,
@ -2801,15 +2802,17 @@ impl Workspace {
window: &mut Window,
cx: &mut App,
) -> Task<Result<Box<dyn ItemHandle>, anyhow::Error>> {
self.open_path_preview(path, pane, focus_item, false, window, cx)
self.open_path_preview(path, pane, focus_item, false, true, window, cx)
}
#[allow(clippy::too_many_arguments)]
pub fn open_path_preview(
&mut self,
path: impl Into<ProjectPath>,
pane: Option<WeakEntity<Pane>>,
focus_item: bool,
allow_preview: bool,
activate: bool,
window: &mut Window,
cx: &mut App,
) -> Task<Result<Box<dyn ItemHandle>, anyhow::Error>> {
@ -2830,6 +2833,7 @@ impl Workspace {
project_entry_id,
focus_item,
allow_preview,
activate,
None,
window,
cx,
@ -2888,6 +2892,7 @@ impl Workspace {
project_entry_id,
true,
allow_preview,
true,
None,
window,
cx,