From 62de03f2869766168c6a6b2a9a034f8942a0fd6f Mon Sep 17 00:00:00 2001 From: CharlesChen0823 Date: Tue, 15 Oct 2024 14:27:34 +0800 Subject: [PATCH] tab: Fix copy wrong relative path for tab (#19206) Closes #19204 Release Notes: - Fixed relative paths copied incorrectly from tabs ([#19204](https://github.com/zed-industries/zed/issues/19204)) --- crates/workspace/src/pane.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 9333afa42a..c205f2a8e5 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -1747,17 +1747,6 @@ impl Pane { } } - fn copy_relative_path(&mut self, _: &CopyRelativePath, cx: &mut ViewContext) { - if let Some(clipboard_text) = self - .active_item() - .as_ref() - .and_then(|entry| entry.project_path(cx)) - .map(|p| p.path.to_string_lossy().to_string()) - { - cx.write_to_clipboard(ClipboardItem::new_string(clipboard_text)); - } - } - pub fn icon_color(selected: bool) -> Color { if selected { Color::Default @@ -2102,6 +2091,11 @@ impl Pane { let parent_abs_path = entry_abs_path .as_deref() .and_then(|abs_path| Some(abs_path.parent()?.to_path_buf())); + let relative_path = pane + .read(cx) + .item_for_entry(entry, cx) + .and_then(|item| item.project_path(cx)) + .map(|project_path| project_path.path); let entry_id = entry.to_proto(); menu = menu @@ -2117,13 +2111,17 @@ impl Pane { }), ) }) - .entry( - "Copy Relative Path", - Some(Box::new(CopyRelativePath)), - cx.handler_for(&pane, move |pane, cx| { - pane.copy_relative_path(&CopyRelativePath, cx); - }), - ) + .when_some(relative_path, |menu, relative_path| { + menu.entry( + "Copy Relative Path", + Some(Box::new(CopyRelativePath)), + cx.handler_for(&pane, move |_, cx| { + cx.write_to_clipboard(ClipboardItem::new_string( + relative_path.to_string_lossy().to_string(), + )); + }), + ) + }) .map(pin_tab_entries) .separator() .entry(