diff --git a/modules/dev/editor/copy-remote-path.nu b/modules/dev/editor/copy-remote-path.nu new file mode 100755 index 0000000..2816508 --- /dev/null +++ b/modules/dev/editor/copy-remote-path.nu @@ -0,0 +1,40 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i nu -p nushell jujutsu wl-clipboard + +# Generates a remote git URL for the given file and line, and copies it to the clipboard. +def main [ + file: string # Absolute or relative path to the file + --line-start (-s): string # The starting line number (e.g., cursor line) + --line-end (-e): string # The ending line number (for selections) +] { + let root = (jj workspace root | str trim) + let rel_path = ($file | path expand | path relative-to $root) + + # Intersect current ancestry with the ancestry of all remote bookmarks + let ref = (jj log -r "heads(::@ & ::remote_bookmarks())" -n 1 --no-graph -T "commit_id" | str trim) + + if ($ref | is-empty) { + print -e "Error: No pushed commits found in the current ancestry." + exit 1 + } + + let remote_url = ( + jj git remote list + | parse "{remote} {url}" + | where remote == "origin" + | get url.0 + | if ($in | str contains "://") { $in } else { $"https://($in | str replace ':' '/')" } + | url parse + ) + + # Construct the line number suffix (GitHub format) + let start = if ($line_start | is-empty) { "" } else { $line_start } + let end = if ($line_end | is-empty) { "" } else { $"-L($line_end)" } + let line_suffix = if ($start | is-empty) { "" } else { $"#L($start)($end)" } + + # Construct the final URL + let url = $"https://($remote_url.host)($remote_url.path | str replace '.git' '')/blob/($ref)/($rel_path)($line_suffix)" + + $url | wl-copy + print -e $"Copied to clipboard: ($url)" +} diff --git a/modules/dev/editor/helix.nix b/modules/dev/editor/helix.nix index eada238..a71829e 100644 --- a/modules/dev/editor/helix.nix +++ b/modules/dev/editor/helix.nix @@ -49,16 +49,7 @@ lib.mkIf config.dev.editor.enable { let plusMenu = { g = '' - :sh ${pkgs.nushell}/bin/nu -c ' - let line = ("%{selection_line_start}" | default "%{cursor_line}") - let line_end = (if ("%{selection_line_end}" | is-not-empty) {$"-L%{selection_line_end}"} else "") - let root = (${pkgs.jujutsu}/bin/jj workspace root | str trim) - let rel_path = ("%{file_path_absolute}" | path relative-to $root) - let ref = (${pkgs.jujutsu}/bin/jj log -r "heads(::@ & bookmarks())" -n 1 --no-graph -T "commit_id") - let remote_url = (${pkgs.jujutsu}/bin/jj git remote list | parse "{remote} {url}" | where remote == origin | get url.0 | if ($in | str contains '://') {$in} else $"https://($in | str replace ':' '/')" | url parse) - let url = $"https://($remote_url.host)($remote_url.path | str replace ".git" "")/blob/($ref)/($rel_path)#L($line)($line_end)" - $url | ${pkgs.wl-clipboard}/bin/wl-copy - ' + :sh ${./copy-remote-path.nu} "%{file_path_absolute}" --line-start "%{selection_line_start}" --line-end "%{selection_line_end}" ''; b = ":echo %sh{git blame -L %{cursor_line},+1 %{buffer_name}}"; p = ":sh echo %{buffer_name} | ${pkgs.wl-clipboard}/bin/wl-copy";