xref: /netbsd-src/external/apache2/llvm/dist/clang/utils/bash-autocomplete.sh (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg# Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this.
2*7330f729Sjoerg
3*7330f729Sjoerg_clang_filedir()
4*7330f729Sjoerg{
5*7330f729Sjoerg  # _filedir function provided by recent versions of bash-completion package is
6*7330f729Sjoerg  # better than "compgen -f" because the former honors spaces in pathnames while
7*7330f729Sjoerg  # the latter doesn't. So we use compgen only when _filedir is not provided.
8*7330f729Sjoerg  _filedir 2> /dev/null || COMPREPLY=( $( compgen -f ) )
9*7330f729Sjoerg}
10*7330f729Sjoerg
11*7330f729Sjoerg_clang()
12*7330f729Sjoerg{
13*7330f729Sjoerg  local cur prev words cword arg flags w1 w2
14*7330f729Sjoerg  # If latest bash-completion is not supported just initialize COMPREPLY and
15*7330f729Sjoerg  # initialize variables by setting manually.
16*7330f729Sjoerg  _init_completion -n 2> /dev/null
17*7330f729Sjoerg  if [[ "$?" != 0 ]]; then
18*7330f729Sjoerg    COMPREPLY=()
19*7330f729Sjoerg    cword=$COMP_CWORD
20*7330f729Sjoerg    cur="${COMP_WORDS[$cword]}"
21*7330f729Sjoerg  fi
22*7330f729Sjoerg
23*7330f729Sjoerg  w1="${COMP_WORDS[$cword - 1]}"
24*7330f729Sjoerg  if [[ $cword > 1 ]]; then
25*7330f729Sjoerg    w2="${COMP_WORDS[$cword - 2]}"
26*7330f729Sjoerg  fi
27*7330f729Sjoerg
28*7330f729Sjoerg  # Pass all the current command-line flags to clang, so that clang can handle
29*7330f729Sjoerg  # these internally.
30*7330f729Sjoerg  # '=' is separated differently by bash, so we have to concat them without ','
31*7330f729Sjoerg  for i in `seq 1 $cword`; do
32*7330f729Sjoerg    if [[ $i == $cword || "${COMP_WORDS[$(($i+1))]}" == '=' ]]; then
33*7330f729Sjoerg      arg="$arg${COMP_WORDS[$i]}"
34*7330f729Sjoerg    else
35*7330f729Sjoerg      arg="$arg${COMP_WORDS[$i]},"
36*7330f729Sjoerg    fi
37*7330f729Sjoerg  done
38*7330f729Sjoerg
39*7330f729Sjoerg  # expand ~ to $HOME
40*7330f729Sjoerg  eval local path=${COMP_WORDS[0]}
41*7330f729Sjoerg  # Use $'\t' so that bash expands the \t for older versions of sed.
42*7330f729Sjoerg  flags=$( "$path" --autocomplete="$arg" 2>/dev/null | sed -e $'s/\t.*//' )
43*7330f729Sjoerg  # If clang is old that it does not support --autocomplete,
44*7330f729Sjoerg  # fall back to the filename completion.
45*7330f729Sjoerg  if [[ "$?" != 0 ]]; then
46*7330f729Sjoerg    _clang_filedir
47*7330f729Sjoerg    return
48*7330f729Sjoerg  fi
49*7330f729Sjoerg
50*7330f729Sjoerg  # When clang does not emit any possible autocompletion, or user pushed tab after " ",
51*7330f729Sjoerg  # just autocomplete files.
52*7330f729Sjoerg  if [[ "$flags" == "$(echo -e '\n')" ]]; then
53*7330f729Sjoerg    # If -foo=<tab> and there was no possible values, autocomplete files.
54*7330f729Sjoerg    [[ "$cur" == '=' || "$cur" == -*= ]] && cur=""
55*7330f729Sjoerg    _clang_filedir
56*7330f729Sjoerg  elif [[ "$cur" == '=' ]]; then
57*7330f729Sjoerg    COMPREPLY=( $( compgen -W "$flags" -- "") )
58*7330f729Sjoerg  else
59*7330f729Sjoerg    # Bash automatically appends a space after '=' by default.
60*7330f729Sjoerg    # Disable it so that it works nicely for options in the form of -foo=bar.
61*7330f729Sjoerg    [[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null
62*7330f729Sjoerg    COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
63*7330f729Sjoerg  fi
64*7330f729Sjoerg}
65*7330f729Sjoergcomplete -F _clang clang
66