Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generating shell completions #223

Closed
chrisvittal opened this issue Sep 9, 2017 · 26 comments
Closed

Add support for generating shell completions #223

chrisvittal opened this issue Sep 9, 2017 · 26 comments

Comments

@chrisvittal
Copy link
Contributor

clap supports generating shell completions either at buildtime or at runtime. It would be helpful to add them. I'm willing to do the work on it, but I wanted to ask if you would prefer having a completions option to generate them at runtime, or a build script to generate them at compile time?

@casey
Copy link
Owner

casey commented Sep 11, 2017

That would be great!

Is there any downside to doing both? It seems like it would be useful for distros if there was a completions file in the tarball that they could copy, and useful for end users that they can generate the completions file and use it no matter where they got just.

@siiptuo
Copy link

siiptuo commented Sep 11, 2017

This would be useful. However clap seems only to be able to generate completions for static things like options and their known values. To be useful, completion should also work on dynamic values like recipes from the current justfile. So I think completions should be maintained by hand (possibly based on the generated ones).

@casey
Copy link
Owner

casey commented Sep 11, 2017

@siiptuo, good point. Let's start with generated ones and then add support for completing recipes for the current justfile. Ideally we wouldn't switch to maintaining them entirely by hand, since clap will likely improve their completions over time, meaning that we would have to re-integrate changes to the clap-generated completions every time they change.

@king6cong
Copy link

Maybe support of https://github.com/scop/bash-completion can be added first?

Looks like the make completion can be reused, but doesn't work after a few attempts 😳

@linux-china
Copy link
Contributor

linux-china commented Oct 29, 2018

if you use o-my-zsh, following follow steps:

  • create plugin directory
mkdir -p ~/.oh-my-zsh/custom/plugins/just
  • fill ~/.oh-my-zsh/custom/plugins/just/just.plugin.zsh with following code
#compdef just

alias justl="\just --list"
alias juste="\just --evaluate"

subcmds=()

while read -r line ; do
   if [[ ! $line == Available* ]] ;
   then
      line2=${line/[[:space:]]*\#/:}
      subcmds+=($line2)
   fi
done < <(just --list)

_describe 'command' subcmds

  • active just plugin in your .zshrc file

@timglabisch
Copy link

@linux-china doesnt work for me.
when i login, i get:

Last login: Fri Dec 21 14:33:04 2018 from x
No justfile found.
_tags:comptags:36: can only be called from completion function
_tags:comptry:55: can only be called from completion function
_tags:comptags:60: can only be called from completion function
_tags:comptags:67: can only be called from completion function

@linux-china
Copy link
Contributor

linux-china commented Dec 21, 2018

@timglabisch please make sure you use completion with justfile present in current directory. https://github.com/linux-china/oh-my-zsh/tree/master/plugins/just . I submitted a PR to oh-my-zsh for approve, and no response now.

@alexbepple
Copy link

For fish shell the completion of recipe names is awfully trivial.

completions/just.fish:

complete -c just -a (just --summary)

@camerondavison
Copy link

camerondavison commented Mar 14, 2019

for simple recipe completion this worked for me using bash4 and bash completions brew install bash-completion

complete -W '$(just --summary)' just

@casey casey added this to the eventually milestone Apr 17, 2019
@dovahcrow
Copy link

@timglabisch please make sure you use completion with justfile present in current directory. https://github.com/linux-china/oh-my-zsh/tree/master/plugins/just . I submitted a PR to oh-my-zsh for approve, and no response now.

It's not working tho. I got same error as timglabisch got.

@heliostatic
Copy link

@timglabisch please make sure you use completion with justfile present in current directory. https://github.com/linux-china/oh-my-zsh/tree/master/plugins/just . I submitted a PR to oh-my-zsh for approve, and no response now.

It's not working tho. I got same error as timglabisch got.

Depending on your setup, you may need to wrap it in a function. Here's mine:

In .zshrc:

# load every completion after autocomplete loads
for file in ${(M)config_files:#*/completion.zsh}
do
 source $file
done

In .dotfiles/just/_just:

#compdef _just just
#autoload

alias justl="\just --list"
alias juste="\just --evaluate"

_just() {
   local context curcontext="$curcontext" state line
   typeset -A opt_args

   local subcmds=()

   while read -r line ; do
      if [[ ! $line == Available* ]] ;
      then
         subcmds+=(${line/[[:space:]]*\#/:})
      fi
   done < <(just --list)

   _describe 'command' subcmds
}

_just "@"

@peterkc
Copy link

peterkc commented May 24, 2019

Tested with latest 0.4.3, the following works:

In .zlogin:

fpath=($HOME/.zsh/completion $fpath)

autoload -Uz compinit
compinit -u

In $HOME/.zsh/completion/_just:

#compdef _just just
#autoload

_just () {
    local -a subcmds

    subcmds=($(just --summary))

    _describe 'command' subcmds
}

_just "$@"

@dovahcrow
Copy link

@peterkc You made my day! Thanks!

@casey casey modified the milestones: eventually, 1.0 Jun 22, 2019
@casey casey modified the milestones: 1.0, eventually Nov 7, 2019
@heyrict
Copy link
Contributor

heyrict commented Nov 8, 2019

I've added the following code to ~/.local/share/bash-completion/completions/just based on @camerondavison 's comment.

if [ -f justfile ]; then
    complete -W "`just --summary`" just
fi

# ex: ts=4 sw=4 et filetype=sh

@bew
Copy link
Contributor

bew commented Nov 8, 2019

Your micro completions script are nice but they do not allow to complete just options...

@heyrict
Copy link
Contributor

heyrict commented Nov 8, 2019

Year, they are just a workaround. So it'd be great if just can have some completion files that ships along it.

@gotcha
Copy link

gotcha commented Jan 8, 2020

I made a bash completion script that supposedly works for 0.5.4.

See https://github.com/gotcha/just-bash-completion

FTR, this is my first attempt at a completion script so any comments are welcome.

@camerondavison @heyrict @bew

@un-def
Copy link

un-def commented Jan 13, 2020

Completion plugin for ohmyzsh: https://gist.github.com/un-def/ee696955bf0fb85e7cf2fc32f26eaa34

Thanks to @peterkc #223 (comment)

@casey
Copy link
Owner

casey commented Jan 14, 2020

Thanks so much to everyone who has worked on completions! If there are additional features in just that would help make completions better (like printing out recipes in a different format, etc) do let me know!

Also, would it make sense to move or mirror the completion plug-ins into the just repository itself, so that they're more easily discoverable? They could also be included in the release tarballs, and would also be available for downstream packagers, like Homebrew et al.

I don't have a lot of experience with how shell completion scripts are developed and distributed, so, for example, I don't know if it's more common for them to be packaged with the app, or packaged with the shell. (I.e. if the completion scripts should be bundled with just, or if it's better for them to be bundled with shell, as is the case with, for example, fish.)

@gotcha
Copy link

gotcha commented Jan 14, 2020

@casey I know nothing about completion scripts distribution.

Nevertheless, I am totally fine with the idea of moving the bash completion script I did to your repo.
IOW, if you feel like it, go !

@casey
Copy link
Owner

casey commented Jan 15, 2020

My thinking is that if the completion scripts are distributed with the shell, then they might not be in sync with the latest version of just, but if they're distributed with just, then they might not be in sync with the latest version of the shell.

However, it seems like a discoverability win for users if they're available in the Just repository.

@heyrict
Copy link
Contributor

heyrict commented Jan 15, 2020

@casey After a quick search in the repo of cargo, which ships completion files in their repo, I haven't found issues about completions related to the shell version (except this, which is probably not related).

Afaik, aside from shipping the completion files, using a completion subcommand or generating completion file from clap are also an option for completion.

@casey
Copy link
Owner

casey commented Jan 15, 2020

I just landed #572, which adds a --completions <SHELL> command to print out completions for SHELL (Bash, Zsh, Fish, PowerShell, and Elvish are currently supported.) As well as checked the generated scripts into the completions directory.

The scripts are incomplete, however. They were generated with Clap, which is the argument parsing library that Just uses. As such, they don't support completing recipe names (build, test, etc), which is definitely important functionality. So, the next step is to modify those scripts to support that, probably by calling just --summary. I'll try to fix them up, hopefully using the completion scripts that others have used as a reference, but if anyone else wants to give it a shot, please go ahead!

@casey
Copy link
Owner

casey commented Jan 15, 2020

@heyrict Thanks for the info! If cargo ships completion files in their repo, then it's probably safe for Just too as well :P

@casey
Copy link
Owner

casey commented Jan 15, 2020

PS I opened #573 to track adding recipes to the shell completion scripts.

@casey
Copy link
Owner

casey commented Feb 20, 2020

I think the only thing left to do is to modify the completions generated with the --completions subcommand to include functionality to get recipes from the current justfile, if it exists. I'm going to close this in favor of #573, which tracks that specifically.

@casey casey closed this as completed Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests