From e2635a685ed3292cc3e99f7ef85f927bbfd4c5a9 Mon Sep 17 00:00:00 2001 From: Scott Lembcke Date: Mon, 26 Aug 2024 13:31:20 -0500 Subject: [PATCH] Add command to copy current file:line for working with external tools (#14793) Closes #14787. I made a quick draft implementation of this feature request: https://github.com/zed-industries/zed/issues/14787 I know how to use use gdb well, so lacking a built-in debugger is OK. BUT... Speaking personally, setting breakpoints is 50% of what I want an IDE to do for me when debugging. Having a feature where I can click, copy, "b [paste]", is a huge step up from typing the whole thing in manually. I figure this must be useful for other external tools, or even just regular-human-communication too. Open Questions: * Does this belong in the right click menu? (I put it next to "Copy Permalink" which is similar.) * Probably not useful enough to get a default keymap? * Relative vs absolute path? * Does this need tests? Release Notes: - Added `editor: copy file location` command to copy the current file location (FILE:LINE) to the clipboard ([#14787](https://github.com/zed-industries/zed/issues/14787)). --------- Co-authored-by: Marshall Bowers --- crates/editor/src/actions.rs | 1 + crates/editor/src/editor.rs | 11 +++++++++++ crates/editor/src/element.rs | 1 + 3 files changed, 13 insertions(+) diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 3fa241676feb00f3ecb5d5d452eb0288252a015d..e0ae75b6361e6a031473dfad556dd756738491f8 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -199,6 +199,7 @@ gpui::actions!( CopyHighlightJson, CopyPath, CopyPermalinkToLine, + CopyFileLocation, CopyRelativePath, Cut, CutToEndOfLine, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index a3cfc5a287807d238ab8f54906667cad460a3010..a458660185d43c384e8bd327c0bcf2beaf998047 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -10905,6 +10905,17 @@ impl Editor { } } + pub fn copy_file_location(&mut self, _: &CopyFileLocation, cx: &mut ViewContext) { + if let Some(buffer) = self.buffer().read(cx).as_singleton() { + if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) { + if let Some(path) = file.path().to_str() { + let selection = self.selections.newest::(cx).start.row + 1; + cx.write_to_clipboard(ClipboardItem::new_string(format!("{path}:{selection}"))); + } + } + } + } + pub fn open_permalink_to_line(&mut self, _: &OpenPermalinkToLine, cx: &mut ViewContext) { let permalink = self.get_permalink_to_line(cx); diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index c1ebdea5e2ad3c905b9012408f916a78dfe7fcae..0126f3491da57ee023d66427803d27a3e4b79e1f 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -353,6 +353,7 @@ impl EditorElement { register_action(view, cx, Editor::copy_highlight_json); register_action(view, cx, Editor::copy_permalink_to_line); register_action(view, cx, Editor::open_permalink_to_line); + register_action(view, cx, Editor::copy_file_location); register_action(view, cx, Editor::toggle_git_blame); register_action(view, cx, Editor::toggle_git_blame_inline); register_action(view, cx, Editor::toggle_hunk_diff);