Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

project_panel: Continue batch delete when individual entries fail (#59595)

justinschmitz9731fc9d5f4710
diff --git a/Cargo.lock b/Cargo.lock
index 920c4e48122..bc57517ab75 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14091,6 +14091,7 @@ dependencies = [
"gpui",
"itertools 0.14.0",
"language",
+ "log",
"markdown_preview",
"menu",
"notifications",
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index 746c45d9f82..4c4a3912263 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -2397,7 +2397,18 @@ impl FakeFs {
self.state
.lock()
.remove_dir_errors
- .insert(normalize_path(path.as_ref()), message);
+ .insert(Self::remove_dir_error_key(path.as_ref()), message);
+ }
+
+ /// Entry resolution in `try_entry` ignores drive prefixes, so the error
+ /// injection map must too.
+ /// Otherwise, on Windows, a key like `C:\workspace\dir` would never match a
+ /// lookup for `\workspace\dir`.
+ fn remove_dir_error_key(path: &Path) -> PathBuf {
+ normalize_path(path)
+ .components()
+ .skip_while(|component| matches!(component, Component::Prefix(_)))
+ .collect()
}
pub fn paths(&self, include_dot_git: bool) -> Vec<PathBuf> {
@@ -2542,7 +2553,12 @@ impl FakeFs {
self.simulate_random_delay().await;
let path = normalize_path(path);
- if let Some(message) = self.state.lock().remove_dir_errors.get(&path) {
+ if let Some(message) = self
+ .state
+ .lock()
+ .remove_dir_errors
+ .get(&Self::remove_dir_error_key(&path))
+ {
anyhow::bail!("{message}");
}
let parent_path = path.parent().context("cannot remove the root")?;
diff --git a/crates/project_panel/Cargo.toml b/crates/project_panel/Cargo.toml
index eac58314f08..89e97ee9c78 100644
--- a/crates/project_panel/Cargo.toml
+++ b/crates/project_panel/Cargo.toml
@@ -51,6 +51,7 @@ telemetry.workspace = true
notifications.workspace = true
feature_flags.workspace = true
fs.workspace = true
+log.workspace = true
[dev-dependencies]
client = { workspace = true, features = ["test-support"] }
diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs
index 059f0685078..bf3b33efc72 100644
--- a/crates/project_panel/src/project_panel.rs
+++ b/crates/project_panel/src/project_panel.rs
@@ -640,6 +640,16 @@ fn get_item_color(is_sticky: bool, cx: &App) -> ItemColors {
}
}
+enum DeleteEntryOutcome {
+ /// Entry was successfully trashed, returning the `Change` that can be
+ /// recorded to the undo stack.
+ Trashed(Change),
+ /// Entry was successfully deleted, or there was nothing to delete.
+ Deleted,
+ /// Deleting or trashing the entry failed at the OS level.
+ Failed,
+}
+
impl ProjectPanel {
fn new(
workspace: &mut Workspace,
@@ -2062,6 +2072,32 @@ impl ProjectPanel {
}))
}
+ async fn resolve_delete_entry(
+ task: Option<Task<Result<Option<fs::TrashedEntry>>>>,
+ worktree_id: WorktreeId,
+ trash: bool,
+ ) -> DeleteEntryOutcome {
+ let Some(task) = task else {
+ // If there's no task to be awaited on, it means that the entry, or
+ // worktree, were likely already deleted.
+ return DeleteEntryOutcome::Deleted;
+ };
+
+ match task.await {
+ Ok(Some(trashed_entry)) if trash => {
+ DeleteEntryOutcome::Trashed(Change::Trashed(worktree_id, trashed_entry))
+ }
+ Err(err) => {
+ log::error!(
+ "Failed to {} an entry: {err:#}",
+ if trash { "trash" } else { "delete" }
+ );
+ DeleteEntryOutcome::Failed
+ }
+ Ok(_) => DeleteEntryOutcome::Deleted,
+ }
+ }
+
fn discard_edit_state(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if let Some(edit_state) = self.state.edit_state.take() {
self.state.temporarily_unfolded_pending_state = edit_state
@@ -2621,29 +2657,37 @@ impl ProjectPanel {
}
let mut changes = Vec::new();
+ let total_count = file_paths.len();
+ let mut failed_count = 0;
+
+ let tasks = panel.update(cx, |panel, cx| {
+ panel.project.update(cx, |project, cx| {
+ file_paths
+ .into_iter()
+ .map(|(entry_id, worktree_id, _)| {
+ (worktree_id, project.delete_entry(entry_id, trash, cx))
+ })
+ .collect::<Vec<_>>()
+ })
+ })?;
- for (entry_id, worktree_id, _) in file_paths {
- let trashed_entry = panel
- .update(cx, |panel, cx| {
- panel
- .project
- .update(cx, |project, cx| project.delete_entry(entry_id, trash, cx))
- .context("no such entry")
- })??
- .await?;
-
- // Keep track of trashed change so that we can then record
- // all of the changes at once, such that undoing and redoing
- // restores or trashes all files in batch.
- if trash && let Some(trashed_entry) = trashed_entry {
- changes.push(Change::Trashed(worktree_id, trashed_entry));
+ for (worktree_id, task) in tasks {
+ match Self::resolve_delete_entry(task, worktree_id, trash).await {
+ DeleteEntryOutcome::Deleted => {}
+ DeleteEntryOutcome::Trashed(change) => changes.push(change),
+ DeleteEntryOutcome::Failed => failed_count += 1,
}
}
+
panel.update_in(cx, |panel, window, cx| {
if trash {
panel.undo_manager.record(changes).log_err();
}
+ if failed_count > 0 {
+ panel.show_remove_failure_toast(trash, total_count, failed_count, cx);
+ }
+
if let Some(next_selection) = next_selection {
panel.update_visible_entries(
Some((next_selection.worktree_id, next_selection.entry_id)),
@@ -2663,6 +2707,39 @@ impl ProjectPanel {
});
}
+ /// Displays a toast notification, informing that the application was not
+ /// able to delete/trash some of the files the user intended to
+ /// delete/trash.
+ fn show_remove_failure_toast(
+ &self,
+ trash: bool,
+ total_count: usize,
+ failed_count: usize,
+ cx: &mut Context<Self>,
+ ) {
+ let message = match (trash, total_count) {
+ (true, 1) => format!("Failed to trash {failed_count} of {total_count} file."),
+ (true, _) => format!("Failed to trash {failed_count} of {total_count} files."),
+ (false, 1) => format!("Failed to delete {failed_count} of {total_count} file."),
+ (false, _) => format!("Failed to delete {failed_count} of {total_count} files."),
+ };
+
+ let toast = StatusToast::new(message, cx, |this, _| {
+ this.icon(
+ Icon::new(IconName::XCircle)
+ .size(IconSize::Small)
+ .color(Color::Error),
+ )
+ .dismiss_button(true)
+ });
+
+ self.workspace
+ .update(cx, |workspace, cx| {
+ workspace.toggle_status_toast(toast, cx);
+ })
+ .ok();
+ }
+
fn find_next_selection_after_deletion(
&self,
sanitized_entries: BTreeSet<SelectedEntry>,
diff --git a/crates/project_panel/src/tests/undo.rs b/crates/project_panel/src/tests/undo.rs
index 4315a6ecb4c..1e69c7443ae 100644
--- a/crates/project_panel/src/tests/undo.rs
+++ b/crates/project_panel/src/tests/undo.rs
@@ -382,3 +382,25 @@ async fn trash_undo_redo(cx: &mut gpui::TestAppContext) {
cx.redo().await;
cx.assert_fs_state_is(&[]);
}
+
+#[gpui::test]
+async fn trash_continues_when_one_entry_fails(cx: &mut gpui::TestAppContext) {
+ let mut cx = TestContext::new_with_tree(
+ cx,
+ json!({
+ "0_dir": {},
+ "a.txt": "",
+ "b.txt": "",
+ }),
+ )
+ .await;
+
+ cx.fs
+ .set_remove_dir_error(path("/workspace/0_dir"), "simulated failure".into());
+
+ cx.trash(&["0_dir", "a.txt", "b.txt"]).await;
+ cx.assert_fs_state_is(&["0_dir/"]);
+
+ cx.undo().await;
+ cx.assert_fs_state_is(&["0_dir/", "a.txt", "b.txt"]);
+}