Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1 | # Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this. |
| 2 | _clang() |
| 3 | { |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame^] | 4 | local cur prev words cword arg |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 5 | _init_completion -n : || return |
| 6 | |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame^] | 7 | # bash always separates '=' as a token even if there's no space before/after '='. |
| 8 | # On the other hand, '=' is just a regular character for clang options that |
| 9 | # contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=". |
| 10 | # So, we need to partially undo bash tokenization here for integrity. |
| 11 | local w1="${COMP_WORDS[$cword - 1]}" |
| 12 | local w2="${COMP_WORDS[$cword - 2]}" |
| 13 | if [[ "$cur" == -* ]]; then |
| 14 | # -foo<tab> |
| 15 | arg="$cur" |
| 16 | elif [[ "$w1" == -* && "$cur" == '=' ]]; then |
| 17 | # -foo=<tab> |
| 18 | arg="$w1=," |
| 19 | elif [[ "$w1" == -* ]]; then |
| 20 | # -foo <tab> or -foo bar<tab> |
| 21 | arg="$w1,$cur" |
| 22 | elif [[ "$w2" == -* && "$w1" == '=' ]]; then |
| 23 | # -foo=bar<tab> |
| 24 | arg="$w2=,$cur" |
| 25 | else |
| 26 | _filedir |
| 27 | fi |
| 28 | |
| 29 | local flags=$( clang --autocomplete="$arg" ) |
| 30 | if [[ "$cur" == "=" ]]; then |
| 31 | COMPREPLY=( $( compgen -W "$flags" -- "") ) |
| 32 | elif [[ "$flags" == "" ]]; then |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 33 | _filedir |
| 34 | else |
| 35 | COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) ) |
| 36 | fi |
Yuka Takahashi | b036027 | 2017-05-23 18:52:27 +0000 | [diff] [blame] | 37 | } |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 38 | complete -F _clang clang |