1
0
Fork 0
mirror of synced 2024-11-05 01:08:59 -05:00

Rework zsh completion fallback

Don't rely on internals from the git completion. Instead set up the
environment and then simply call _git and let it do the completion as
it see fit.

See #292.
This commit is contained in:
Erik Flodin 2021-01-05 23:16:10 +01:00
parent aaf519623b
commit 57eed9742a
No known key found for this signature in database
GPG key ID: 420A7C865EE3F85F

View file

@ -1,9 +1,11 @@
#compdef yadm #compdef yadm
# This completion tries to fallback to git's completion for git commands. # This completion tries to fallback to git's completion for git commands.
# It handles two different types of fallbacks:
# - The git completion shipped with git. zstyle -T ':completion:*:yadm:argument-1:descriptions:' format && \
# - The git completion shipped with zsh. zstyle ':completion:*:yadm:argument-1:descriptions' format '%d:'
zstyle -T ':completion:*:yadm:*:yadm' group-name && \
zstyle ':completion:*:yadm:*:yadm' group-name ''
_yadm-alt() { _yadm-alt() {
return 0 return 0
@ -72,7 +74,9 @@ _yadm-transcrypt() {
} }
_yadm-upgrade() { _yadm-upgrade() {
return 0 _arguments \
'-f[force deinit of submodules]' \
': '
} }
_yadm-version() { _yadm-version() {
@ -80,56 +84,40 @@ _yadm-version() {
} }
_yadm_commands() { _yadm_commands() {
local -a commands local -a commands=(
commands=( alt:'create links for alternates'
alt:'create links for alternates (yadm)' bootstrap:'execute bootstrap'
bootstrap:'execute bootstrap (yadm)' clone:'clone an existing yadm repository'
clone:'clone an existing repository (yadm)'
config:'configure an yadm setting' config:'configure an yadm setting'
decrypt:'decrypt files (yadm)' decrypt:'decrypt files'
encrypt:'encrypt files (yadm)' encrypt:'encrypt files'
enter:'run sub-shell with GIT variables set' enter:'run sub-shell with GIT variables set'
git-crypt:'run git-crypt commands for the yadm repository' git-crypt:'run git-crypt commands for the yadm repository'
gitconfig:'run the git config command' gitconfig:'run the git config command'
help:'display yadm help information' help:'display yadm help information'
init:'initialize an empty yadm repository' init:'initialize an empty yadm repository'
list:'list files tracked by yadm' list:'list files tracked by yadm'
perms:'fix perms for private files (yadm)' perms:'fix perms for private files'
transcrypt:'run transcrypt commands for the yadm repository' transcrypt:'run transcrypt commands for the yadm repository'
upgrade:'upgrade legacy yadm paths' upgrade:'upgrade legacy yadm paths'
version:'show yadm version' version:'show yadm version'
) )
integer _ret=1 local oldcontext="$curcontext"
local curcontext="${curcontext%:*:*}:git:"
if (( $+functions[_git_commands] )); then words=("git" "${words[-1]}") CURRENT=2 service=git _git
zstyle ':completion:*:*:yadm:*' user-commands $commands
_call_function _ret _git_commands
zstyle -d ':completion:*:*:yadm:*' user-commands
else
local curcontext=${curcontext%:*:*}:git:
_tags common-commands alias-commands all-commands
while _tags; do
_requested common-commands && __git_zsh_cmd_common
_requested alias-commands && __git_zsh_cmd_alias
_requested all-commands && __git_zsh_cmd_all
let _ret || break
done
_describe "yadm commands" commands
fi
return _ret curcontext="$oldcontext"
_describe -t yadm "yadm commands" commands
return 0
} }
_yadm() { _yadm() {
local curcontext=$curcontext state state_descr line local curcontext=$curcontext state state_descr line
declare -A opt_args declare -A opt_args
local -a orig_words=( ${words[@]} )
local cur=${words[CURRENT]}
local prev=${words[CURRENT-1]}
let cword=CURRENT-1
_arguments -C \ _arguments -C \
'(-Y --yadm-dir)'{-Y,--yadm-dir}'[override the standard yadm directory]: :_files -/' \ '(-Y --yadm-dir)'{-Y,--yadm-dir}'[override the standard yadm directory]: :_files -/' \
'--yadm-data[override the standard yadm data directory]: :_files -/' \ '--yadm-data[override the standard yadm data directory]: :_files -/' \
@ -147,7 +135,7 @@ _yadm() {
(( $+opt_args[--yadm-repo] )) && repo_args+=(--yadm-repo "$opt_args[--yadm-repo]") (( $+opt_args[--yadm-repo] )) && repo_args+=(--yadm-repo "$opt_args[--yadm-repo]")
(( $+opt_args[--yadm-data] )) && repo_args+=(--yadm-data "$opt_args[--yadm-data]") (( $+opt_args[--yadm-data] )) && repo_args+=(--yadm-data "$opt_args[--yadm-data]")
local -x GIT_DIR="$(_call_program gitdir yadm "${repo_args[@]}" introspect repo)" local -x GIT_DIR="$(_call_program gitdir yadm "${repo_args[@]}" introspect repo)"
local __git_dir="$GIT_DIR" [[ -z "$GIT_DIR" ]] && return 1
integer _ret=1 integer _ret=1
case $state in case $state in
@ -155,35 +143,17 @@ _yadm() {
_yadm_commands && _ret=0 _yadm_commands && _ret=0
;; ;;
(option-or-argument) (option-or-argument)
local command=$words[1] curcontext=${curcontext%:*:*}:yadm-${words[1]}:
if ! _call_function _ret _yadm-${words[1]}; then
# First try to complete yadm commands
curcontext=${curcontext%:*:*}:yadm-$command:
if ! _call_function _ret _yadm-$command; then
# Translate gitconfig to use the regular completion for config # Translate gitconfig to use the regular completion for config
[[ $command = "gitconfig" ]] && command=config [[ ${words[1]} = "gitconfig" ]] && words[1]=config
# If is wasn't a valid command, try git's completion if available words=("git" "${(@)words}")
if (( $+functions[__git_zsh_bash_func] )); then CURRENT=$(( CURRENT + 1 ))
words=( ${orig_words[@]} )
curcontext=${curcontext%:*:*}:git:
__git_zsh_bash_func $command curcontext=${curcontext%:*:*}:git:
let _ret && _default && _ret=0 service=git _git && _ret=0
else
# If git's completion wasn't available, try zsh's
curcontext=${curcontext%:*:*}:git-$command:
if ! _call_function _ret _git-$command; then
if [[ $words[1] = \!* ]]; then
words[1]=${words[1]##\!}
_normal && _ret=0
elif zstyle -T :completion:$curcontext: use-fallback; then
_default && _ret=0
else
_message "unknown sub-command: $command"
fi
fi
fi
fi fi
;; ;;
esac esac
@ -191,11 +161,4 @@ _yadm() {
return _ret return _ret
} }
# Ignore call from _git when using git's completion (( $+functions[_git] )) && _yadm
__yadm_zsh_main() {
_ret=0
}
# __git_zsh_bash_func comes from git's completion and _git_commands from zsh's
(( $+functions[__git_zsh_bash_func] + $+functions[_git_commands] )) || _git
_yadm