blob: 3e9f65f10db2c6fcad16f18e7af6778328e0b7d1 [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.
2_clang()
3{
Yuka Takahashiba5d4af2017-06-20 16:31:31 +00004 local cur prev words cword arg
Yuka Takahashic8068db2017-05-23 18:39:08 +00005 _init_completion -n : || return
6
Yuka Takahashiba5d4af2017-06-20 16:31:31 +00007 # 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 Takahashic8068db2017-05-23 18:39:08 +000033 _filedir
34 else
35 COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
36 fi
Yuka Takahashib0360272017-05-23 18:52:27 +000037}
Yuka Takahashic8068db2017-05-23 18:39:08 +000038complete -F _clang clang