Skip to content

Commit

Permalink
Merge pull request #5684 from epage/endless
Browse files Browse the repository at this point in the history
fix(complete): Don't cause endless completions for bash/zsh
  • Loading branch information
epage authored Aug 19, 2024
2 parents d811585 + 0209a79 commit 232af62
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 48 deletions.
30 changes: 20 additions & 10 deletions clap_complete/src/command/shells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,21 @@ impl CommandCompleter for Bash {

let script = r#"
_clap_complete_NAME() {
export IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
local IFS=$'\013'
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false
local _CLAP_COMPLETE_SPACE=false
else
export _CLAP_COMPLETE_SPACE=true
local _CLAP_COMPLETE_SPACE=true
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
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
Expand Down Expand Up @@ -471,10 +477,14 @@ impl CommandCompleter for Zsh {

let script = r#"#compdef BIN
function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n'
local completions=("${(@f)$(COMPLETER complete zsh -- ${words} 2>/dev/null)}")
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
local _CLAP_IFS=$'\n'
local completions=("${(@f)$( \
_CLAP_IFS="$_CLAP_IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
COMPLETER complete zsh -- ${words} 2>/dev/null \
)}")
if [[ -n $completions ]]; then
compadd -a completions
Expand Down
33 changes: 21 additions & 12 deletions clap_complete/src/env/shells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ impl EnvCompleter for Bash {

let script = r#"
_clap_complete_NAME() {
export IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
local IFS=$'\013'
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false
local _CLAP_COMPLETE_SPACE=false
else
export _CLAP_COMPLETE_SPACE=true
local _CLAP_COMPLETE_SPACE=true
fi
export VAR="bash"
COMPREPLY=( $("COMPLETER" -- "${COMP_WORDS[@]}") )
COMPREPLY=( $( \
IFS="$IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
VAR="bash" \
"COMPLETER" -- "${COMP_WORDS[@]}" \
) )
if [[ $? != 0 ]]; then
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
Expand Down Expand Up @@ -347,11 +352,15 @@ impl EnvCompleter for Zsh {

let script = r#"#compdef BIN
function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n'
export VAR="zsh"
local completions=("${(@f)$(COMPLETER -- ${words} 2>/dev/null)}")
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
local _CLAP_IFS=$'\n'
local completions=("${(@f)$( \
_CLAP_IFS="$_CLAP_IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
VAR="zsh" \
COMPLETER -- ${words} 2>/dev/null \
)}")
if [[ -n $completions ]]; then
compadd -a completions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ PS1='% '
. /etc/bash_completion

_clap_complete_exhaustive() {
export IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
local IFS=$'\013'
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false
local _CLAP_COMPLETE_SPACE=false
else
export _CLAP_COMPLETE_SPACE=true
local _CLAP_COMPLETE_SPACE=true
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
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#compdef exhaustive
function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n'
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
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
compadd -a completions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ PS1='% '
. /etc/bash_completion

_clap_complete_exhaustive() {
export IFS=$'\013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
local IFS=$'\013'
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false
local _CLAP_COMPLETE_SPACE=false
else
export _CLAP_COMPLETE_SPACE=true
local _CLAP_COMPLETE_SPACE=true
fi
export COMPLETE="bash"
COMPREPLY=( $("exhaustive" -- "${COMP_WORDS[@]}") )
COMPREPLY=( $( \
IFS="$IFS" \
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
COMPLETE="bash" \
"exhaustive" -- "${COMP_WORDS[@]}" \
) )
if [[ $? != 0 ]]; then
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#compdef exhaustive
function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n'
export COMPLETE="zsh"
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
local _CLAP_IFS=$'\n'

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
compadd -a completions
Expand Down
18 changes: 12 additions & 6 deletions clap_complete/tests/snapshots/register_minimal.bash
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@

_clap_complete_my_app() {
export IFS=$'/013'
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
local IFS=$'/013'
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
if compopt +o nospace 2> /dev/null; then
export _CLAP_COMPLETE_SPACE=false
local _CLAP_COMPLETE_SPACE=false
else
export _CLAP_COMPLETE_SPACE=true
local _CLAP_COMPLETE_SPACE=true
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
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
Expand Down

0 comments on commit 232af62

Please sign in to comment.