martinvonz / jj

A Git-compatible VCS that is both simple and powerful
https://martinvonz.github.io/jj/
Apache License 2.0
7.24k stars 240 forks source link

Tab-complete branch names for `jj branch` subcommands #1217

Open chooglen opened 1 year ago

chooglen commented 1 year ago

Tab-completing branch names would be useful for all branch subcommands today execpt list.

e.g. I run into this on a regular basis:

$ jj branch delete # wait what that branch again?
$ jj branch list
$ jj branch delete <furiously typed branch name and/or copied>

or

$ jj branch set <typo-ed branch name>
# Ugh
$ jj undo
$ jj branch set <correct branch name>

It seems that Clap completion doesn't have support for this yet https://github.com/clap-rs/clap/issues/1232.

senekor commented 1 month ago

Here are my custom additions to the auto-generated completions (fish shell):

complete -c jj -n "__fish_seen_subcommand_from branch; and __fish_seen_subcommand_from delete" -f -a "(jj branch list | cut --delimiter ':' --fields 1)"
complete -c jj -n "__fish_seen_subcommand_from branch; and __fish_seen_subcommand_from forget" -f -a "(jj branch list | cut --delimiter ':' --fields 1)"
complete -c jj -n "__fish_seen_subcommand_from branch; and __fish_seen_subcommand_from rename" -f -a "(jj branch list | cut --delimiter ':' --fields 1)"
complete -c jj -n "__fish_seen_subcommand_from branch; and __fish_seen_subcommand_from set" -f -a "(jj branch list | cut --delimiter ':' --fields 1)"
complete -c jj -n "__fish_seen_subcommand_from branch; and __fish_seen_subcommand_from track" -f -a "(jj branch list --all-remotes | cut --delimiter ':' --fields 1 | grep '\w@\w')"
complete -c jj -n "__fish_seen_subcommand_from branch; and __fish_seen_subcommand_from untrack" -f -a '(
for line in (jj branch list -a | cut --delimiter ":" --fields 1)
    if echo $line | grep --quiet "^\S"
        set --function __local_branch $line
        continue
    end
    if [ $line = "  @git" ]
        continue
    end
    echo "$__local_branch$(string trim $line)"
end
set --erase __local_branch
)'

As mentioned in this comment, adding that to the output of jj util completion should be simple.

I would be happy to make that contribution, unless maintainers consider this an undue maintenance burden. In my opinion, if it does turn out to be a burden, it can just be deleted again. I don't think shell completions come with any expection of stability.

martinvonz commented 1 month ago

That seems fine to me.

Since #3651, jj branch list | cut --delimiter ':' --fields 1 can be replaced by jj branch list -T 'name ++ "\n"'. Maybe the untrack script can also be simplified somehow.

senekor commented 1 month ago

Great! I didn't think of using templates, great idea. I'll see how simple I can make it and open a PR.