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. |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 2 | |
| 3 | _clang_filedir() |
| 4 | { |
| 5 | # _filedir function provided by recent versions of bash-completion package is |
| 6 | # better than "compgen -f" because the former honors spaces in pathnames while |
| 7 | # the latter doesn't. So we use compgen only when _filedir is not provided. |
| 8 | _filedir 2> /dev/null || COMPREPLY=( $( compgen -f ) ) |
| 9 | } |
| 10 | |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 11 | _clang() |
| 12 | { |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 13 | local cur prev words cword arg flags w1 w2 |
| 14 | # If latest bash-completion is not supported just initialize COMPREPLY and |
| 15 | # initialize variables by setting manualy. |
| 16 | _init_completion -n 2> /dev/null |
| 17 | if [[ "$?" != 0 ]]; then |
| 18 | COMPREPLY=() |
| 19 | cword=$COMP_CWORD |
| 20 | cur="${COMP_WORDS[$cword]}" |
| 21 | fi |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 22 | |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 23 | w1="${COMP_WORDS[$cword - 1]}" |
| 24 | if [[ $cword > 1 ]]; then |
| 25 | w2="${COMP_WORDS[$cword - 2]}" |
Yuka Takahashi | 8561c2e | 2017-07-15 09:09:51 +0000 | [diff] [blame] | 26 | fi |
| 27 | |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 28 | # Clang want to know if -cc1 or -Xclang option is specified or not, because we don't want to show |
| 29 | # cc1 options otherwise. |
| 30 | if [[ "${COMP_WORDS[1]}" == "-cc1" || "$w1" == "-Xclang" ]]; then |
| 31 | arg="#" |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 32 | fi |
Yuka Takahashi | 8561c2e | 2017-07-15 09:09:51 +0000 | [diff] [blame] | 33 | |
| 34 | # bash always separates '=' as a token even if there's no space before/after '='. |
| 35 | # On the other hand, '=' is just a regular character for clang options that |
| 36 | # contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=". |
| 37 | # So, we need to partially undo bash tokenization here for integrity. |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 38 | if [[ "$cur" == -* ]]; then |
| 39 | # -foo<tab> |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 40 | arg="$arg$cur" |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 41 | elif [[ "$w1" == -* && "$cur" == '=' ]]; then |
| 42 | # -foo=<tab> |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 43 | arg="$arg$w1=," |
Yuka Takahashi | 15309d1 | 2017-07-08 17:34:02 +0000 | [diff] [blame] | 44 | elif [[ "$cur" == -*= ]]; then |
| 45 | # -foo=<tab> |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 46 | arg="$arg$cur," |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 47 | elif [[ "$w1" == -* ]]; then |
| 48 | # -foo <tab> or -foo bar<tab> |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 49 | arg="$arg$w1,$cur" |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 50 | elif [[ "$w2" == -* && "$w1" == '=' ]]; then |
| 51 | # -foo=bar<tab> |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 52 | arg="$arg$w2=,$cur" |
Yuka Takahashi | 15309d1 | 2017-07-08 17:34:02 +0000 | [diff] [blame] | 53 | elif [[ ${cur: -1} != '=' && ${cur/=} != $cur ]]; then |
| 54 | # -foo=bar<tab> |
Yuka Takahashi | 33cf63b | 2017-07-08 17:48:59 +0000 | [diff] [blame] | 55 | arg="$arg${cur%=*}=,${cur#*=}" |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 56 | fi |
| 57 | |
Yuka Takahashi | 558f3dd | 2017-07-01 16:30:02 +0000 | [diff] [blame] | 58 | # expand ~ to $HOME |
| 59 | eval local path=${COMP_WORDS[0]} |
| 60 | flags=$( "$path" --autocomplete="$arg" 2>/dev/null ) |
Yuka Takahashi | 79d21c2 | 2017-06-28 15:59:55 +0000 | [diff] [blame] | 61 | # If clang is old that it does not support --autocomplete, |
| 62 | # fall back to the filename completion. |
| 63 | if [[ "$?" != 0 ]]; then |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 64 | _clang_filedir |
Yuka Takahashi | 79d21c2 | 2017-06-28 15:59:55 +0000 | [diff] [blame] | 65 | return |
| 66 | fi |
| 67 | |
Yuka Takahashi | 8c00011 | 2017-07-26 13:30:36 +0000 | [diff] [blame^] | 68 | # When clang does not emit any possible autocompletion, or user pushed tab after " ", |
| 69 | # just autocomplete files. |
| 70 | if [[ "$flags" == "$(echo -e '\n')" || "$arg" == "" ]]; then |
| 71 | # If -foo=<tab> and there was no possible values, autocomplete files. |
| 72 | [[ "$cur" == '=' || "$cur" == -*= ]] && cur="" |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 73 | _clang_filedir |
Yuka Takahashi | 8c00011 | 2017-07-26 13:30:36 +0000 | [diff] [blame^] | 74 | elif [[ "$cur" == '=' ]]; then |
| 75 | COMPREPLY=( $( compgen -W "$flags" -- "") ) |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 76 | else |
Yuka Takahashi | a4a8780 | 2017-06-26 00:35:36 +0000 | [diff] [blame] | 77 | # Bash automatically appends a space after '=' by default. |
| 78 | # Disable it so that it works nicely for options in the form of -foo=bar. |
Yuka Takahashi | 4776cb0 | 2017-07-01 18:32:55 +0000 | [diff] [blame] | 79 | [[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 80 | COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) ) |
| 81 | fi |
Yuka Takahashi | b036027 | 2017-05-23 18:52:27 +0000 | [diff] [blame] | 82 | } |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 83 | complete -F _clang clang |