Merge pull request #5684 from epage/endless

fix(complete): Don't cause endless completions for bash/zsh
This commit is contained in:
Ed Page 2024-08-19 10:32:35 -05:00 committed by GitHub
commit 232af62f7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 90 additions and 46 deletions

View file

@ -187,15 +187,21 @@ impl CommandCompleter for Bash {
let script = r#" let script = r#"
_clap_complete_NAME() { _clap_complete_NAME() {
export IFS=$'\013' local IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD} local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false local _CLAP_COMPLETE_SPACE=false
else else
export _CLAP_COMPLETE_SPACE=true local _CLAP_COMPLETE_SPACE=true
fi fi
COMPREPLY=( $("COMPLETER" complete bash -- "${COMP_WORDS[@]}") ) COMPREPLY=( $( \
IFS="$IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
"COMPLETER" complete bash -- "${COMP_WORDS[@]}" \
) )
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
unset COMPREPLY unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
@ -471,10 +477,14 @@ impl CommandCompleter for Zsh {
let script = r#"#compdef BIN let script = r#"#compdef BIN
function _clap_dynamic_completer() { function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n' local _CLAP_IFS=$'\n'
local completions=("${(@f)$(COMPLETER complete zsh -- ${words} 2>/dev/null)}") local completions=("${(@f)$( \
_CLAP_IFS="$_CLAP_IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
COMPLETER complete zsh -- ${words} 2>/dev/null \
)}")
if [[ -n $completions ]]; then if [[ -n $completions ]]; then
compadd -a completions compadd -a completions

View file

@ -31,16 +31,21 @@ impl EnvCompleter for Bash {
let script = r#" let script = r#"
_clap_complete_NAME() { _clap_complete_NAME() {
export IFS=$'\013' local IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD} local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false local _CLAP_COMPLETE_SPACE=false
else else
export _CLAP_COMPLETE_SPACE=true local _CLAP_COMPLETE_SPACE=true
fi fi
export VAR="bash" COMPREPLY=( $( \
COMPREPLY=( $("COMPLETER" -- "${COMP_WORDS[@]}") ) IFS="$IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
VAR="bash" \
"COMPLETER" -- "${COMP_WORDS[@]}" \
) )
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
unset COMPREPLY unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
@ -347,11 +352,15 @@ impl EnvCompleter for Zsh {
let script = r#"#compdef BIN let script = r#"#compdef BIN
function _clap_dynamic_completer() { function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n' local _CLAP_IFS=$'\n'
export VAR="zsh"
local completions=("${(@f)$(COMPLETER -- ${words} 2>/dev/null)}") local completions=("${(@f)$( \
_CLAP_IFS="$_CLAP_IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
VAR="zsh" \
COMPLETER -- ${words} 2>/dev/null \
)}")
if [[ -n $completions ]]; then if [[ -n $completions ]]; then
compadd -a completions compadd -a completions

View file

@ -2,15 +2,21 @@ PS1='% '
. /etc/bash_completion . /etc/bash_completion
_clap_complete_exhaustive() { _clap_complete_exhaustive() {
export IFS=$'\013' local IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD} local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false local _CLAP_COMPLETE_SPACE=false
else else
export _CLAP_COMPLETE_SPACE=true local _CLAP_COMPLETE_SPACE=true
fi fi
COMPREPLY=( $("exhaustive" complete bash -- "${COMP_WORDS[@]}") ) COMPREPLY=( $( \
IFS="$IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
"exhaustive" complete bash -- "${COMP_WORDS[@]}" \
) )
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
unset COMPREPLY unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

View file

@ -1,9 +1,13 @@
#compdef exhaustive #compdef exhaustive
function _clap_dynamic_completer() { function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n' local _CLAP_IFS=$'\n'
local completions=("${(@f)$(exhaustive complete zsh -- ${words} 2>/dev/null)}") local completions=("${(@f)$( \
_CLAP_IFS="$_CLAP_IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
exhaustive complete zsh -- ${words} 2>/dev/null \
)}")
if [[ -n $completions ]]; then if [[ -n $completions ]]; then
compadd -a completions compadd -a completions

View file

@ -2,16 +2,21 @@ PS1='% '
. /etc/bash_completion . /etc/bash_completion
_clap_complete_exhaustive() { _clap_complete_exhaustive() {
export IFS=$'\013' local IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD} local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false local _CLAP_COMPLETE_SPACE=false
else else
export _CLAP_COMPLETE_SPACE=true local _CLAP_COMPLETE_SPACE=true
fi fi
export COMPLETE="bash" COMPREPLY=( $( \
COMPREPLY=( $("exhaustive" -- "${COMP_WORDS[@]}") ) IFS="$IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
COMPLETE="bash" \
"exhaustive" -- "${COMP_WORDS[@]}" \
) )
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
unset COMPREPLY unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

View file

@ -1,10 +1,14 @@
#compdef exhaustive #compdef exhaustive
function _clap_dynamic_completer() { function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n' local _CLAP_IFS=$'\n'
export COMPLETE="zsh"
local completions=("${(@f)$(exhaustive -- ${words} 2>/dev/null)}") local completions=("${(@f)$( \
_CLAP_IFS="$_CLAP_IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
COMPLETE="zsh" \
exhaustive -- ${words} 2>/dev/null \
)}")
if [[ -n $completions ]]; then if [[ -n $completions ]]; then
compadd -a completions compadd -a completions

View file

@ -1,14 +1,20 @@
_clap_complete_my_app() { _clap_complete_my_app() {
export IFS=$'/013' local IFS=$'/013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD} local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false local _CLAP_COMPLETE_SPACE=false
else else
export _CLAP_COMPLETE_SPACE=true local _CLAP_COMPLETE_SPACE=true
fi fi
COMPREPLY=( $("my-app" complete bash -- "${COMP_WORDS[@]}") ) COMPREPLY=( $( /
IFS="$IFS" /
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" /
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" /
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" /
"my-app" complete bash -- "${COMP_WORDS[@]}" /
) )
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
unset COMPREPLY unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then