dsifford / yarn-completion

Bash completion for Yarn
MIT License
277 stars 25 forks source link

How can we plug this into our own bash autocompletions? #31

Closed Nxt3 closed 5 years ago

Nxt3 commented 5 years ago

I've created a function in my .bash_profile that I want to include autocompletions from yarn for.

This is what I'm trying:

complete -o default -o nospace -F _yarn yarnupgrade

but I get the following error:

bash: completion: function '_yarn' not found

Ideally I'd like to only autocomplete the choices output from yarn upgrade.

Otherwise, yarn completion works great!

dsifford commented 5 years ago

.bash_profile is loaded before .bashrc so depending on where you have the completions being loaded, your call to complete Is likely happening before the completions are sourced.

Nxt3 commented 5 years ago

I'm doing something similar with git: complete -o default -o nospace -F _git g and that works fine. It's only the yarn one.

Nxt3 commented 5 years ago

The completions are being loaded in .bash_profile as well.

Nxt3 commented 5 years ago

So _git is working differently since git-completion has nothing to do with bash-completion@2.

I'm thinking it's not possible to access the functions defined in your autocompletion globally. 🤔

dsifford commented 5 years ago

Yes it is.

dsifford commented 5 years ago

Here's an example from my dotfiles....

https://github.com/dsifford/.dotfiles/blob/ed2be686ffc689c66814e5891cada8aa70e4bbdd/local/lib/bashrc.d/completion-aliases.sh#L52-L55

Nxt3 commented 5 years ago

Can I ask what that's doing (like, step-by-step)? I've never seen something in bash like that.

dsifford commented 5 years ago
# IF the yarn command exists in the PATH
command -v yarn \
  # AND a completion was able to be sourced for yarn
  && __load_completion yarn \
  # AND after sourcing the yarn completions, there exists a function available named _yarn
  && command -v _yarn \
  # THEN complete my alias for yarn "y" with the yarn completions
  && complete -F _yarn y
Nxt3 commented 5 years ago

Awesome. This helped me achieve what I was looking for. Thank you so much!