diff options
Diffstat (limited to 'utils/bash-autocomplete.sh')
-rwxr-xr-x | utils/bash-autocomplete.sh | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/utils/bash-autocomplete.sh b/utils/bash-autocomplete.sh index de4a435b8849f..ba908bcc0bc32 100755 --- a/utils/bash-autocomplete.sh +++ b/utils/bash-autocomplete.sh @@ -1,13 +1,38 @@ # Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this. _clang() { - local cur prev words cword flags + local cur prev words cword arg _init_completion -n : || return - flags=$( clang --autocomplete="$cur" ) - if [[ "$flags" == "" || "$cur" == "" ]]; then + # bash always separates '=' as a token even if there's no space before/after '='. + # On the other hand, '=' is just a regular character for clang options that + # contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=". + # So, we need to partially undo bash tokenization here for integrity. + local w1="${COMP_WORDS[$cword - 1]}" + local w2="${COMP_WORDS[$cword - 2]}" + if [[ "$cur" == -* ]]; then + # -foo<tab> + arg="$cur" + elif [[ "$w1" == -* && "$cur" == '=' ]]; then + # -foo=<tab> + arg="$w1=," + elif [[ "$w1" == -* ]]; then + # -foo <tab> or -foo bar<tab> + arg="$w1,$cur" + elif [[ "$w2" == -* && "$w1" == '=' ]]; then + # -foo=bar<tab> + arg="$w2=,$cur" + fi + + local flags=$( clang --autocomplete="$arg" ) + if [[ "$cur" == '=' ]]; then + COMPREPLY=( $( compgen -W "$flags" -- "") ) + elif [[ "$flags" == "" || "$arg" == "" ]]; then _filedir else + # Bash automatically appends a space after '=' by default. + # Disable it so that it works nicely for options in the form of -foo=bar. + [[ "${flags: -1}" == '=' ]] && compopt -o nospace COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) ) fi } |