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.

git_panel: Add copy path actions to the context menu (#62352)

狐狸6634c945d3af
diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json
index f2c37efc6f9..239129d9d4a 100644
--- a/assets/keymaps/default-linux.json
+++ b/assets/keymaps/default-linux.json
@@ -1024,6 +1024,10 @@
"ctrl-alt-y": "git::ToggleStaged",
"space": "git::ToggleStaged",
"shift-space": "git::StageRange",
+ "alt-copy": "workspace::CopyPath",
+ "ctrl-alt-c": "workspace::CopyPath",
+ "alt-shift-copy": "workspace::CopyRelativePath",
+ "alt-ctrl-shift-c": "workspace::CopyRelativePath",
"tab": "git_panel::FocusEditor",
"shift-tab": "git_panel::FocusEditor",
"escape": "git_panel::ToggleFocus",
diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.json
index e41f83eecf0..e2048107f6d 100644
--- a/assets/keymaps/default-macos.json
+++ b/assets/keymaps/default-macos.json
@@ -1086,6 +1086,8 @@
"shift-space": "git::StageRange",
"cmd-y": "git::StageFile",
"cmd-shift-y": "git::UnstageFile",
+ "cmd-alt-c": "workspace::CopyPath",
+ "alt-cmd-shift-c": "workspace::CopyRelativePath",
"alt-down": "git_panel::FocusEditor",
"tab": "git_panel::FocusEditor",
"shift-tab": "git_panel::FocusEditor",
diff --git a/assets/keymaps/default-windows.json b/assets/keymaps/default-windows.json
index b9adaf88dd3..3de7be2cf45 100644
--- a/assets/keymaps/default-windows.json
+++ b/assets/keymaps/default-windows.json
@@ -1017,6 +1017,8 @@
"shift-alt-y": "git::UnstageFile",
"space": "git::ToggleStaged",
"shift-space": "git::StageRange",
+ "shift-alt-c": "workspace::CopyPath",
+ "ctrl-k ctrl-shift-c": "workspace::CopyRelativePath",
"tab": "git_panel::FocusEditor",
"shift-tab": "git_panel::FocusEditor",
"escape": "git_panel::ToggleFocus",
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 4fd06705801..3f59188947a 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -48,9 +48,10 @@ use git::{
};
use gpui::{
AbsoluteLength, Action, Anchor, AnyElement, AsyncApp, AsyncWindowContext, ClickEvent,
- DismissEvent, Empty, Entity, EventEmitter, FocusHandle, Focusable, KeyContext, MouseButton,
- MouseDownEvent, Pixels, Point, PromptLevel, ScrollStrategy, Subscription, Task, TaskExt,
- TextStyle, UniformListScrollHandle, WeakEntity, actions, anchored, deferred, uniform_list,
+ ClipboardItem, DismissEvent, Empty, Entity, EventEmitter, FocusHandle, Focusable, KeyContext,
+ MouseButton, MouseDownEvent, Pixels, Point, PromptLevel, ScrollStrategy, Subscription, Task,
+ TaskExt, TextStyle, UniformListScrollHandle, WeakEntity, actions, anchored, deferred,
+ uniform_list,
};
use itertools::Itertools;
use language::{Buffer, BufferEvent, File};
@@ -102,7 +103,9 @@ use workspace::{
notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},
};
use zed_actions::{
- DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize, git_panel::ToggleFocus,
+ DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize,
+ git_panel::ToggleFocus,
+ workspace::{CopyPath, CopyRelativePath},
};
const GIT_PANEL_KEY: &str = "GitPanel";
@@ -204,6 +207,7 @@ fn git_panel_context_menu(
has_unstaged_changes: bool,
has_new_changes: bool,
has_stash_items: bool,
+ include_copy_paths: bool,
focus_handle: FocusHandle,
window: &mut Window,
cx: &mut App,
@@ -226,6 +230,12 @@ fn git_panel_context_menu(
)
.action_disabled_when(!has_stash_items, "Stash Pop", StashPop.boxed_clone())
.action("View Stash", zed_actions::git::ViewStash.boxed_clone())
+ .when(include_copy_paths, |context_menu| {
+ context_menu
+ .separator()
+ .action("Copy Path", CopyPath.boxed_clone())
+ .action("Copy Relative Path", CopyRelativePath.boxed_clone())
+ })
.separator()
.action_disabled_when(
!has_tracked_changes,
@@ -616,6 +626,15 @@ impl GitListEntry {
GitListEntry::Status(_) | GitListEntry::TreeStatus(_) | GitListEntry::Directory(_)
)
}
+
+ fn repo_path(&self) -> Option<&RepoPath> {
+ match self {
+ GitListEntry::Status(entry) => Some(&entry.repo_path),
+ GitListEntry::TreeStatus(entry) => Some(&entry.entry.repo_path),
+ GitListEntry::Directory(entry) => Some(&entry.key.path),
+ GitListEntry::Header(_) | GitListEntry::EmptySection(_) => None,
+ }
+ }
}
enum GitPanelViewMode {
@@ -1975,6 +1994,32 @@ impl GitPanel {
});
}
+ fn copy_path(&mut self, _: &CopyPath, _: &mut Window, cx: &mut Context<Self>) {
+ if let Some((repo_path, repo)) = self
+ .get_selected_entry()
+ .and_then(GitListEntry::repo_path)
+ .zip(self.active_repository.as_ref())
+ {
+ let path = repo.read(cx).repo_path_to_abs_path(repo_path);
+ cx.write_to_clipboard(ClipboardItem::new_string(
+ path.to_string_lossy().into_owned(),
+ ));
+ } else {
+ cx.propagate();
+ }
+ }
+
+ fn copy_relative_path(&mut self, _: &CopyRelativePath, _: &mut Window, cx: &mut Context<Self>) {
+ if let Some(repo_path) = self.get_selected_entry().and_then(GitListEntry::repo_path) {
+ let path_style = self.project.read(cx).path_style(cx);
+ cx.write_to_clipboard(ClipboardItem::new_string(
+ repo_path.display(path_style).into_owned(),
+ ));
+ } else {
+ cx.propagate();
+ }
+ }
+
fn open_selected_entry_on_click(
&mut self,
secondary: bool,
@@ -5560,6 +5605,7 @@ impl GitPanel {
has_unstaged_changes,
has_new_changes,
has_stash_items,
+ false,
focus_handle.clone(),
window,
cx,
@@ -7062,7 +7108,7 @@ impl GitPanel {
.on_mouse_down(
MouseButton::Right,
cx.listener(move |this, event: &MouseDownEvent, window, cx| {
- this.deploy_panel_context_menu(event.position, window, cx)
+ this.deploy_panel_context_menu(event.position, false, window, cx)
}),
)
.custom_scrollbars(
@@ -7215,6 +7261,12 @@ impl GitPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
+ if matches!(self.entries.get(ix), Some(GitListEntry::Directory(_))) {
+ self.selected_entry = Some(ix);
+ self.deploy_panel_context_menu(position, true, window, cx);
+ return;
+ }
+
let stage_intent = self.stage_intent_for_entry_index(ix);
let Some(entry) = self.entries.get(ix).and_then(|e| e.status_entry()) else {
return;
@@ -7247,6 +7299,9 @@ impl GitPanel {
.action("Unstaged Changes", ViewUnstagedChanges.boxed_clone())
.action("Staged Changes", ViewStagedChanges.boxed_clone())
.separator()
+ .action("Copy Path", CopyPath.boxed_clone())
+ .action("Copy Relative Path", CopyRelativePath.boxed_clone())
+ .separator()
.action_disabled_when(
!is_created,
"Add to .gitignore",
@@ -7274,6 +7329,7 @@ impl GitPanel {
fn deploy_panel_context_menu(
&mut self,
position: Point<Pixels>,
+ include_copy_paths: bool,
window: &mut Window,
cx: &mut Context<Self>,
) {
@@ -7289,6 +7345,7 @@ impl GitPanel {
has_unstaged_changes,
has_new_changes,
has_stash_items,
+ include_copy_paths,
self.focus_handle.clone(),
window,
cx,
@@ -7749,6 +7806,13 @@ impl GitPanel {
this.toggle_directory(&key, window, cx);
})
})
+ .on_mouse_down(
+ MouseButton::Right,
+ cx.listener(move |this, event: &MouseDownEvent, window, cx| {
+ cx.stop_propagation();
+ this.deploy_entry_context_menu(event.position, ix, window, cx);
+ }),
+ )
.into_any_element()
}
@@ -8093,6 +8157,8 @@ impl Render for GitPanel {
.on_action(cx.listener(Self::open_diff))
.on_action(cx.listener(Self::open_solo_diff))
.on_action(cx.listener(Self::view_file))
+ .on_action(cx.listener(Self::copy_path))
+ .on_action(cx.listener(Self::copy_relative_path))
.on_action(cx.listener(Self::view_unstaged_changes))
.on_action(cx.listener(Self::view_staged_changes))
.on_action(cx.listener(Self::focus_changes_list))
@@ -9266,6 +9332,77 @@ mod tests {
assert_editor_opened_with_path(&workspace, Path::new("src/a/foo.rs"), &mut cx);
}
+ #[gpui::test]
+ async fn test_copy_paths(cx: &mut TestAppContext) {
+ init_test(cx);
+ cx.update(|cx| {
+ SettingsStore::update_global(cx, |store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings.git_panel.get_or_insert_default().tree_view = Some(true);
+ })
+ });
+ });
+
+ let (_, _, workspace, panel, mut cx) = setup_git_panel_with_changes(
+ cx,
+ json!({
+ ".git": {},
+ "src": {
+ "main.rs": "fn main() {}",
+ },
+ }),
+ &[("src/main.rs", StatusCode::Modified)],
+ )
+ .await;
+
+ workspace.update_in(&mut cx, |workspace, window, cx| {
+ workspace.add_panel(panel.clone(), window, cx);
+ workspace.open_panel::<GitPanel>(window, cx);
+ });
+ panel.update_in(&mut cx, |panel, window, cx| {
+ let entry_index = entry_index_for_repo_path(panel, &repo_path("src/main.rs"))
+ .expect("main.rs should exist in the changes list");
+ panel.selected_entry = Some(entry_index);
+ panel.focus_handle.focus(window, cx);
+ });
+ cx.update(|window, cx| window.draw(cx).clear(cx));
+
+ cx.dispatch_action(CopyRelativePath);
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some(path!("src/main.rs").to_owned())
+ );
+
+ cx.dispatch_action(CopyPath);
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some(path!("/project/src/main.rs").to_owned())
+ );
+
+ panel.update(&mut cx, |panel, _| {
+ let entry_index = panel
+ .entries
+ .iter()
+ .position(|entry| {
+ matches!(entry, GitListEntry::Directory(dir) if dir.key.path == repo_path("src"))
+ })
+ .expect("src directory should exist in the changes list");
+ panel.selected_entry = Some(entry_index);
+ });
+
+ cx.dispatch_action(CopyRelativePath);
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some(path!("src").to_owned())
+ );
+
+ cx.dispatch_action(CopyPath);
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some(path!("/project/src").to_owned())
+ );
+ }
+
async fn history_panel_for_project(
fs: Arc<FakeFs>,
cx: &mut TestAppContext,
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs
index 4512a0be5cd..a25fd88bece 100644
--- a/crates/project/src/git_store.rs
+++ b/crates/project/src/git_store.rs
@@ -5614,7 +5614,7 @@ impl RepositorySnapshot {
Self::abs_path_to_repo_path_inner(&self.work_directory_abs_path, abs_path, self.path_style)
}
- fn repo_path_to_abs_path(&self, repo_path: &RepoPath) -> PathBuf {
+ pub fn repo_path_to_abs_path(&self, repo_path: &RepoPath) -> PathBuf {
let repo_path = repo_path.display(self.path_style);
PathBuf::from(
self.path_style