Allow completing variables and recipes after --set in zsh completion script (#697)

This commit is contained in:
Zhenhui Xie 2020-10-18 12:58:44 +08:00 committed by GitHub
parent b64718b295
commit 0e1af65565
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 10 deletions

View File

@ -64,15 +64,33 @@ _just() {
curcontext="${curcontext%:*}-${words[2]}:" curcontext="${curcontext%:*}-${words[2]}:"
local lastarg=${words[${#words}]} local lastarg=${words[${#words}]}
local recipe
if [[ ${lastarg} = */* ]]; then local cmds; cmds=(
${(s: :)$(_call_program commands just --summary)}
)
# Find first recipe name
for ((i = 2; i < $#words; i++ )) do
if [[ ${cmds[(I)${words[i]}]} -gt 0 ]]; then
recipe=${words[i]}
break
fi
done
if [[ $lastarg = */* ]]; then
# Arguments contain slash would be recognised as a file # Arguments contain slash would be recognised as a file
_arguments -s -S $common '*:: :_files' _arguments -s -S $common '*:: :_files'
else elif [[ $lastarg = *=* ]]; then
# Arguments contain equal would be recognised as a variable
_message "value"
elif [[ $recipe ]]; then
# Show usage message # Show usage message
_message "`just --show ${words[2]}`" _message "`just --show $recipe`"
# Or complete with other commands # Or complete with other commands
#_arguments -s -S $common '*:: :_just_commands' #_arguments -s -S $common '*:: :_just_commands'
else
_arguments -s -S $common '*:: :_just_commands'
fi fi
;; ;;
esac esac
@ -82,20 +100,43 @@ _just() {
(( $+functions[_just_commands] )) || (( $+functions[_just_commands] )) ||
_just_commands() { _just_commands() {
[[ $PREFIX = -* ]] && return 1
integer ret=1
local variables; variables=(
${(s: :)$(_call_program commands just --variables)}
)
local commands; commands=( local commands; commands=(
${${${(M)"${(f)$(_call_program commands just --list)}":# *}/ ##/}/ ##/:Args: } ${${${(M)"${(f)$(_call_program commands just --list)}":# *}/ ##/}/ ##/:Args: }
) )
_describe -t commands 'just commands' commands "$@" if compset -P '*='; then
case "${${words[-1]%=*}#*=}" in
*) _message 'value' && ret=0 ;;
esac
else
_describe -t variables 'variables' variables -qS "=" && ret=0
_describe -t commands 'just commands' commands "$@"
fi
} }
(( $+functions[_just_variables] )) || (( $+functions[_just_variables] )) ||
_just_variables() { _just_variables() {
[[ $PREFIX = -* ]] && return 1
integer ret=1
local variables; variables=( local variables; variables=(
${(s: :)$(_call_program commands just --variables)} ${(s: :)$(_call_program commands just --variables)}
) )
_describe -t variables 'variables' variables if compset -P '*='; then
case "${${words[-1]%=*}#*=}" in
*) _message 'value' && ret=0 ;;
esac
else
_describe -t variables 'variables' variables && ret=0
fi
return ret
} }
_just "$@" _just "$@"

View File

@ -73,15 +73,33 @@ const ZSH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
curcontext="${curcontext%:*}-${words[2]}:" curcontext="${curcontext%:*}-${words[2]}:"
local lastarg=${words[${#words}]} local lastarg=${words[${#words}]}
local recipe
if [[ ${lastarg} = */* ]]; then local cmds; cmds=(
${(s: :)$(_call_program commands just --summary)}
)
# Find first recipe name
for ((i = 2; i < $#words; i++ )) do
if [[ ${cmds[(I)${words[i]}]} -gt 0 ]]; then
recipe=${words[i]}
break
fi
done
if [[ $lastarg = */* ]]; then
# Arguments contain slash would be recognised as a file # Arguments contain slash would be recognised as a file
_arguments -s -S $common '*:: :_files' _arguments -s -S $common '*:: :_files'
else elif [[ $lastarg = *=* ]]; then
# Arguments contain equal would be recognised as a variable
_message "value"
elif [[ $recipe ]]; then
# Show usage message # Show usage message
_message "`just --show ${words[2]}`" _message "`just --show $recipe`"
# Or complete with other commands # Or complete with other commands
#_arguments -s -S $common '*:: :_just_commands' #_arguments -s -S $common '*:: :_just_commands'
else
_arguments -s -S $common '*:: :_just_commands'
fi fi
;; ;;
esac esac
@ -93,20 +111,47 @@ const ZSH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
" local commands; commands=( " local commands; commands=(
\x20\x20\x20\x20\x20\x20\x20\x20 \x20\x20\x20\x20\x20\x20\x20\x20
)", )",
r#" local commands; commands=( r#" [[ $PREFIX = -* ]] && return 1
integer ret=1
local variables; variables=(
${(s: :)$(_call_program commands just --variables)}
)
local commands; commands=(
${${${(M)"${(f)$(_call_program commands just --list)}":# *}/ ##/}/ ##/:Args: } ${${${(M)"${(f)$(_call_program commands just --list)}":# *}/ ##/}/ ##/:Args: }
) )
"#,
),
(
r#" _describe -t commands 'just commands' commands "$@""#,
r#" if compset -P '*='; then
case "${${words[-1]%=*}#*=}" in
*) _message 'value' && ret=0 ;;
esac
else
_describe -t variables 'variables' variables -qS "=" && ret=0
_describe -t commands 'just commands' commands "$@"
fi
"#, "#,
), ),
( (
r#"_just "$@""#, r#"_just "$@""#,
r#"(( $+functions[_just_variables] )) || r#"(( $+functions[_just_variables] )) ||
_just_variables() { _just_variables() {
[[ $PREFIX = -* ]] && return 1
integer ret=1
local variables; variables=( local variables; variables=(
${(s: :)$(_call_program commands just --variables)} ${(s: :)$(_call_program commands just --variables)}
) )
_describe -t variables 'variables' variables if compset -P '*='; then
case "${${words[-1]%=*}#*=}" in
*) _message 'value' && ret=0 ;;
esac
else
_describe -t variables 'variables' variables && ret=0
fi
return ret
} }
_just "$@""#, _just "$@""#,