just icon indicating copy to clipboard operation
just copied to clipboard

Autocompletion for a just script?

Open kbd opened this issue 1 year ago • 1 comments

I've long had a setup program that makes symlinks to dotfiles, installs packages, etc. At some point I realized I could replace my code with just (thanks for that), so I rewrote it in terms of a justfile.

Now, I want autocomplete for my setup script. I tried making it a just script hoping that just+zsh would work some magic, but no such luck. I tried eval $(setup --completions zsh) on that just script, but nope. Is there a way of getting zsh to complete a just script? It works how I expect when doing just [tab] when there's a justfile in the current directory.

kbd avatar Sep 12 '24 16:09 kbd

Interesting idea! I'm not sure if this is possible, but I'll leave it open in case anyone has any ideas. I don't know if zsh or other shells can do autocompletion based on the shebang line of a binary, since in general just by knowing the shebang line, for example python or perl, you don't know anything about what arguments a script takes.

casey avatar Sep 13 '24 08:09 casey

Unless I'm misreading the issue (and with a caveat for it targeting bash,) I've adapted the just --completions bash to something close to what you're asking for.

I created a separate just file (just-shebang-bash-completions.just) which I import to my script (MYSCRIPT):

[private]
completions:
    sed 's/just$/{{file_name(justfile())}}/g ; s/just --summary/$1 --summary/g' <(just --completions bash)

In my (MYSCRIPT) just script: import '~/justs/just-shebang-bash-completions.just'

and finally, from my ~/.bashrc equivalent:

. <(MYSCRIPT completions)

Needless to say, this a hack that assumes that MYSCRIPT is on the PATH.

❯ diff <(just --completions bash) <(MYSCRIPT completions)
sed 's/just$/MYSCRIPT/g ; s/just --summary/$1 --summary/g' <(just --completions bash)
38c38
<                     local recipes=$(just --summary 2> /dev/null)
---
>                     local recipes=$($1 --summary 2> /dev/null)
42c42
<                         local recipes=$(just --summary 2> /dev/null -- "${path_prefix}")
---
>                         local recipes=$($1 --summary 2> /dev/null -- "${path_prefix}")
170c170
<     complete -F _just -o nosort -o bashdefault -o default just
---
>     complete -F _just -o nosort -o bashdefault -o default MYSCRIPT
172c172
<     complete -F _just -o bashdefault -o default just
---
>     complete -F _just -o bashdefault -o default MYSCRIPT

dwa avatar Jun 28 '25 23:06 dwa