diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index a715515065a495eebbe66ac82935eb6d949b7d20..3c4104570a92827e467f59bda8d61c3d67f4e7ae 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -194,6 +194,7 @@ gpui::actions!( NewlineBelow, NextScreen, OpenExcerpts, + OpenPermalinkToLine, Outdent, PageDown, PageUp, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 92273dfe4bf689be415d40886b87c0ed032bb353..2c913e2388988f3a5385b68bfb1faa468c5cabec 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -8393,7 +8393,7 @@ impl Editor { } } - pub fn copy_permalink_to_line(&mut self, _: &CopyPermalinkToLine, cx: &mut ViewContext) { + fn get_permalink_to_line(&mut self, cx: &mut ViewContext) -> Result { use git::permalink::{build_permalink, BuildPermalinkParams}; let permalink = maybe!({ @@ -8439,6 +8439,11 @@ impl Editor { selection: selection.map(|selection| selection.range()), }) }); + permalink + } + + pub fn copy_permalink_to_line(&mut self, _: &CopyPermalinkToLine, cx: &mut ViewContext) { + let permalink = self.get_permalink_to_line(cx); match permalink { Ok(permalink) => { @@ -8458,6 +8463,27 @@ impl Editor { } } + pub fn open_permalink_to_line(&mut self, _: &OpenPermalinkToLine, cx: &mut ViewContext) { + let permalink = self.get_permalink_to_line(cx); + + match permalink { + Ok(permalink) => { + cx.open_url(&permalink.to_string()); + } + Err(err) => { + let message = format!("Failed to open permalink: {err}"); + + Err::<(), anyhow::Error>(err).log_err(); + + if let Some(workspace) = self.workspace() { + workspace.update(cx, |workspace, cx| { + workspace.show_toast(Toast::new(0x45a8978, message), cx) + }) + } + } + } + } + pub fn highlight_rows(&mut self, rows: Option>) { self.highlighted_rows = rows; } diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index ce348b21d514518c1ea5b3148457a755416ac9cc..e504c0f68018aa15f4c8f4183f9feeebd8b9678e 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -278,6 +278,7 @@ impl EditorElement { register_action(view, cx, Editor::copy_relative_path); 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, action, cx| { if let Some(task) = editor.format(action, cx) { task.detach_and_log_err(cx);