blob: 2042f0b200cf044d3c78ff6d94c08e814cc12fb9 [file] [log] [blame]
Yuka Takahashic8068db2017-05-23 18:39:08 +00001# Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this.
Yuka Takahashi4776cb02017-07-01 18:32:55 +00002
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 Takahashic8068db2017-05-23 18:39:08 +000011_clang()
12{
Yuka Takahashi4776cb02017-07-01 18:32:55 +000013 local cur prev words cword arg flags w1 w2
14 # If latest bash-completion is not supported just initialize COMPREPLY and
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +000015 # initialize variables by setting manually.
Yuka Takahashi4776cb02017-07-01 18:32:55 +000016 _init_completion -n 2> /dev/null
17 if [[ "$?" != 0 ]]; then
18 COMPREPLY=()
19 cword=$COMP_CWORD
20 cur="${COMP_WORDS[$cword]}"
21 fi
Yuka Takahashic8068db2017-05-23 18:39:08 +000022
Yuka Takahashi4776cb02017-07-01 18:32:55 +000023 w1="${COMP_WORDS[$cword - 1]}"
24 if [[ $cword > 1 ]]; then
25 w2="${COMP_WORDS[$cword - 2]}"
Yuka Takahashi8561c2e2017-07-15 09:09:51 +000026 fi
27
Yuka Takahashi41789e42018-03-05 08:54:20 +000028 # Pass all the current command-line flags to clang, so that clang can handle
29 # these internally.
30 # '=' is separated differently by bash, so we have to concat them without ','
31 for i in `seq 1 $cword`; do
32 if [[ $i == $cword || "${COMP_WORDS[$(($i+1))]}" == '=' ]]; then
33 arg="$arg${COMP_WORDS[$i]}"
34 else
35 arg="$arg${COMP_WORDS[$i]},"
36 fi
37 done
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000038
Yuka Takahashi558f3dd2017-07-01 16:30:02 +000039 # expand ~ to $HOME
40 eval local path=${COMP_WORDS[0]}
Yuka Takahashi66256902017-07-26 13:36:58 +000041 flags=$( "$path" --autocomplete="$arg" 2>/dev/null | sed -e 's/\t.*//' )
Yuka Takahashi79d21c22017-06-28 15:59:55 +000042 # If clang is old that it does not support --autocomplete,
43 # fall back to the filename completion.
44 if [[ "$?" != 0 ]]; then
Yuka Takahashi4776cb02017-07-01 18:32:55 +000045 _clang_filedir
Yuka Takahashi79d21c22017-06-28 15:59:55 +000046 return
47 fi
48
Yuka Takahashi8c000112017-07-26 13:30:36 +000049 # When clang does not emit any possible autocompletion, or user pushed tab after " ",
50 # just autocomplete files.
Yuka Takahashi41789e42018-03-05 08:54:20 +000051 if [[ "$flags" == "$(echo -e '\n')" ]]; then
Yuka Takahashi8c000112017-07-26 13:30:36 +000052 # If -foo=<tab> and there was no possible values, autocomplete files.
53 [[ "$cur" == '=' || "$cur" == -*= ]] && cur=""
Yuka Takahashi4776cb02017-07-01 18:32:55 +000054 _clang_filedir
Yuka Takahashi8c000112017-07-26 13:30:36 +000055 elif [[ "$cur" == '=' ]]; then
56 COMPREPLY=( $( compgen -W "$flags" -- "") )
Yuka Takahashic8068db2017-05-23 18:39:08 +000057 else
Yuka Takahashia4a87802017-06-26 00:35:36 +000058 # Bash automatically appends a space after '=' by default.
59 # Disable it so that it works nicely for options in the form of -foo=bar.
Yuka Takahashi4776cb02017-07-01 18:32:55 +000060 [[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null
Yuka Takahashic8068db2017-05-23 18:39:08 +000061 COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
62 fi
Yuka Takahashib0360272017-05-23 18:52:27 +000063}
Yuka Takahashic8068db2017-05-23 18:39:08 +000064complete -F _clang clang