blob: bab193c56b16ef3589f420d54fdf8538261f3b61 [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
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 Takahashic8068db2017-05-23 18:39:08 +000022
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000023 # bash always separates '=' as a token even if there's no space before/after '='.
24 # On the other hand, '=' is just a regular character for clang options that
25 # contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=".
26 # So, we need to partially undo bash tokenization here for integrity.
Yuka Takahashi4776cb02017-07-01 18:32:55 +000027 w1="${COMP_WORDS[$cword - 1]}"
28 if [[ $cword > 1 ]]; then
29 w2="${COMP_WORDS[$cword - 2]}"
30 fi
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000031 if [[ "$cur" == -* ]]; then
32 # -foo<tab>
33 arg="$cur"
34 elif [[ "$w1" == -* && "$cur" == '=' ]]; then
35 # -foo=<tab>
36 arg="$w1=,"
Yuka Takahashi15309d12017-07-08 17:34:02 +000037 elif [[ "$cur" == -*= ]]; then
38 # -foo=<tab>
39 arg="$cur,"
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000040 elif [[ "$w1" == -* ]]; then
41 # -foo <tab> or -foo bar<tab>
42 arg="$w1,$cur"
43 elif [[ "$w2" == -* && "$w1" == '=' ]]; then
44 # -foo=bar<tab>
45 arg="$w2=,$cur"
Yuka Takahashi15309d12017-07-08 17:34:02 +000046 elif [[ ${cur: -1} != '=' && ${cur/=} != $cur ]]; then
47 # -foo=bar<tab>
48 arg="${cur%=*}=,${cur#*=}"
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000049 fi
50
Yuka Takahashi558f3dd2017-07-01 16:30:02 +000051 # expand ~ to $HOME
52 eval local path=${COMP_WORDS[0]}
53 flags=$( "$path" --autocomplete="$arg" 2>/dev/null )
Yuka Takahashi79d21c22017-06-28 15:59:55 +000054 # If clang is old that it does not support --autocomplete,
55 # fall back to the filename completion.
56 if [[ "$?" != 0 ]]; then
Yuka Takahashi4776cb02017-07-01 18:32:55 +000057 _clang_filedir
Yuka Takahashi79d21c22017-06-28 15:59:55 +000058 return
59 fi
60
Yuka Takahashia4a87802017-06-26 00:35:36 +000061 if [[ "$cur" == '=' ]]; then
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000062 COMPREPLY=( $( compgen -W "$flags" -- "") )
Yuka Takahashia4a87802017-06-26 00:35:36 +000063 elif [[ "$flags" == "" || "$arg" == "" ]]; then
Yuka Takahashi4776cb02017-07-01 18:32:55 +000064 _clang_filedir
Yuka Takahashic8068db2017-05-23 18:39:08 +000065 else
Yuka Takahashia4a87802017-06-26 00:35:36 +000066 # Bash automatically appends a space after '=' by default.
67 # Disable it so that it works nicely for options in the form of -foo=bar.
Yuka Takahashi4776cb02017-07-01 18:32:55 +000068 [[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null
Yuka Takahashic8068db2017-05-23 18:39:08 +000069 COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
70 fi
Yuka Takahashib0360272017-05-23 18:52:27 +000071}
Yuka Takahashic8068db2017-05-23 18:39:08 +000072complete -F _clang clang