From b7a66e449157abc3eb0c1db1e560b3c00b3981be Mon Sep 17 00:00:00 2001 From: Kajus Date: Thu, 22 Aug 2024 16:05:01 +0200 Subject: [PATCH] project_panel: Allow copying the paths of multiple selected files at once (#16558) Closes #16555 Release Notes: - Improved the "Copy Path" and "Copy Relative Path" actions in the project panel's context menu when selecting multiple files. All selected files' paths will now be copied, separated by newlines. --- crates/project_panel/src/project_panel.rs | 49 +++++++++++++++++------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index 9a09660fd664933830bd0dec021257831a1ba88d..ca2bdf1df117cd3dfd51c9736d3cf0165f155a4b 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -1356,22 +1356,47 @@ impl ProjectPanel { } fn copy_path(&mut self, _: &CopyPath, cx: &mut ViewContext) { - if let Some((worktree, entry)) = self.selected_entry(cx) { - cx.write_to_clipboard(ClipboardItem::new_string( - worktree - .abs_path() - .join(&entry.path) - .to_string_lossy() - .to_string(), - )); + let abs_file_paths = { + let project = self.project.read(cx); + self.marked_entries() + .into_iter() + .filter_map(|entry| { + let entry_path = project.path_for_entry(entry.entry_id, cx)?.path; + Some( + project + .worktree_for_id(entry.worktree_id, cx)? + .read(cx) + .abs_path() + .join(entry_path) + .to_string_lossy() + .to_string(), + ) + }) + .collect::>() + }; + if !abs_file_paths.is_empty() { + cx.write_to_clipboard(ClipboardItem::new_string(abs_file_paths.join("\n"))); } } fn copy_relative_path(&mut self, _: &CopyRelativePath, cx: &mut ViewContext) { - if let Some((_, entry)) = self.selected_entry(cx) { - cx.write_to_clipboard(ClipboardItem::new_string( - entry.path.to_string_lossy().to_string(), - )); + let file_paths = { + let project = self.project.read(cx); + self.marked_entries() + .into_iter() + .filter_map(|entry| { + Some( + project + .path_for_entry(entry.entry_id, cx)? + .path + .to_string_lossy() + .to_string(), + ) + }) + .collect::>() + }; + if !file_paths.is_empty() { + cx.write_to_clipboard(ClipboardItem::new_string(file_paths.join("\n"))); } }