diff --git a/scripts/create-completion-script.sh b/scripts/create-completion-script.sh index 1f64fcfc..fccaa88e 100755 --- a/scripts/create-completion-script.sh +++ b/scripts/create-completion-script.sh @@ -29,11 +29,11 @@ case $1 in exit 0 ;; bash) - src_command="source" + src_command="bash_source" target_file="watson.completion" ;; zsh) - src_command="source_zsh" + src_command="zsh_source" target_file="watson.zsh-completion" ;; *) diff --git a/watson.completion b/watson.completion index 6c675b3c..9311170a 100644 --- a/watson.completion +++ b/watson.completion @@ -1,21 +1,29 @@ _watson_completion() { - local IFS=$' -' - COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \ - COMP_CWORD=$COMP_CWORD \ - _WATSON_COMPLETE=complete $1 ) ) + local IFS=$'\n' + local response + + response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _WATSON_COMPLETE=bash_complete $1) + + for completion in $response; do + IFS=',' read type value <<< "$completion" + + if [[ $type == 'dir' ]]; then + COMPREPLY=() + compopt -o dirnames + elif [[ $type == 'file' ]]; then + COMPREPLY=() + compopt -o default + elif [[ $type == 'plain' ]]; then + COMPREPLY+=($value) + fi + done + return 0 } -_watson_completionetup() { - local COMPLETION_OPTIONS="" - local BASH_VERSION_ARR=(${BASH_VERSION//./ }) - # Only BASH version 4.4 and later have the nosort option. - if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then - COMPLETION_OPTIONS="-o nosort" - fi - - complete $COMPLETION_OPTIONS -F _watson_completion watson +_watson_completion_setup() { + complete -o nosort -F _watson_completion watson } -_watson_completionetup; +_watson_completion_setup; + diff --git a/watson.zsh-completion b/watson.zsh-completion index 3594054c..fc65ba5b 100644 --- a/watson.zsh-completion +++ b/watson.zsh-completion @@ -6,17 +6,20 @@ _watson_completion() { local -a response (( ! $+commands[watson] )) && return 1 - response=("${(@f)$( env COMP_WORDS="${words[*]}" \ - COMP_CWORD=$((CURRENT-1)) \ - _WATSON_COMPLETE="complete_zsh" \ - watson )}") + response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _WATSON_COMPLETE=zsh_complete watson)}") - for key descr in ${(kv)response}; do - if [[ "$descr" == "_" ]]; then - completions+=("$key") - else - completions_with_descriptions+=("$key":"$descr") - fi + for type key descr in ${response}; do + if [[ "$type" == "plain" ]]; then + if [[ "$descr" == "_" ]]; then + completions+=("$key") + else + completions_with_descriptions+=("$key":"$descr") + fi + elif [[ "$type" == "dir" ]]; then + _path_files -/ + elif [[ "$type" == "file" ]]; then + _path_files -f + fi done if [ -n "$completions_with_descriptions" ]; then @@ -26,7 +29,7 @@ _watson_completion() { if [ -n "$completions" ]; then compadd -U -V unsorted -a completions fi - compstate[insert]="automenu" } compdef _watson_completion watson; +