Require save confirmation and prevent autosave for deleted files (#20742)

* `has_conflict` will now return true if the file has been deleted on
disk.  This is for treating multi-buffers as conflicted, and also
blocks auto-save.

* `has_deleted_file` is added so that the single-file buffer save can
specifically mention the delete conflict. This does not yet handle
discard (#20745).

Closes #9101
Closes #9568
Closes #20462

Release Notes:

- Improved handling of externally deleted files: auto-save will be
disabled, multibuffers will treat this as a save conflict, and single
buffers will ask for restore confirmation.

Co-authored-by: Conrad <conrad@zed.dev>
This commit is contained in:
Michael Sloan 2024-11-15 15:01:16 -07:00 committed by GitHub
parent ac5ecf5487
commit 369828f51c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 93 additions and 27 deletions

View file

@ -186,6 +186,7 @@ pub struct MultiBufferSnapshot {
non_text_state_update_count: usize,
edit_count: usize,
is_dirty: bool,
has_deleted_file: bool,
has_conflict: bool,
show_headers: bool,
}
@ -494,6 +495,10 @@ impl MultiBuffer {
self.read(cx).is_dirty()
}
pub fn has_deleted_file(&self, cx: &AppContext) -> bool {
self.read(cx).has_deleted_file()
}
pub fn has_conflict(&self, cx: &AppContext) -> bool {
self.read(cx).has_conflict()
}
@ -1419,6 +1424,7 @@ impl MultiBuffer {
snapshot.excerpts = Default::default();
snapshot.trailing_excerpt_update_count += 1;
snapshot.is_dirty = false;
snapshot.has_deleted_file = false;
snapshot.has_conflict = false;
self.subscriptions.publish_mut([Edit {
@ -2003,6 +2009,7 @@ impl MultiBuffer {
let mut excerpts_to_edit = Vec::new();
let mut non_text_state_updated = false;
let mut is_dirty = false;
let mut has_deleted_file = false;
let mut has_conflict = false;
let mut edited = false;
let mut buffers = self.buffers.borrow_mut();
@ -2028,6 +2035,7 @@ impl MultiBuffer {
edited |= buffer_edited;
non_text_state_updated |= buffer_non_text_state_updated;
is_dirty |= buffer.is_dirty();
has_deleted_file |= buffer.file().map_or(false, |file| file.is_deleted());
has_conflict |= buffer.has_conflict();
}
if edited {
@ -2037,6 +2045,7 @@ impl MultiBuffer {
snapshot.non_text_state_update_count += 1;
}
snapshot.is_dirty = is_dirty;
snapshot.has_deleted_file = has_deleted_file;
snapshot.has_conflict = has_conflict;
excerpts_to_edit.sort_unstable_by_key(|(locator, _, _)| *locator);
@ -3691,6 +3700,10 @@ impl MultiBufferSnapshot {
self.is_dirty
}
pub fn has_deleted_file(&self) -> bool {
self.has_deleted_file
}
pub fn has_conflict(&self) -> bool {
self.has_conflict
}