-
Notifications
You must be signed in to change notification settings - Fork 392
clang: new completion #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
ed5e509
5a7d378
c7f12e5
a417718
4ba53fa
50ead93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
_clang_filedir() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need this, just use _filedir. |
||
# _filedir function provided by recent versions of bash-completion package is | ||
# better than "compgen -f" because the former honors spaces in pathnames while | ||
# the latter doesn't. So we use compgen only when _filedir is not provided. | ||
_filedir 2> /dev/null || COMPREPLY=( $( compgen -f ) ) | ||
} | ||
|
||
_clang() | ||
{ | ||
local cur prev words cword arg flags w1 w2 | ||
# If latest bash-completion is not supported just initialize COMPREPLY and | ||
# initialize variables by setting manualy. | ||
_init_completion -n 2> /dev/null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We know latest bash-completion is installed if this file is, please simplify accordingly. |
||
if [[ "$?" != 0 ]]; then | ||
COMPREPLY=() | ||
cword=$COMP_CWORD | ||
cur="${COMP_WORDS[$cword]}" | ||
fi | ||
|
||
w1="${COMP_WORDS[$cword - 1]}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose we already have $prev for this? |
||
if [[ $cword > 1 ]]; then | ||
w2="${COMP_WORDS[$cword - 2]}" | ||
fi | ||
|
||
# Clang want to know if -cc1 or -Xclang option is specified or not, because we don't want to show | ||
# cc1 options otherwise. | ||
if [[ "${COMP_WORDS[1]}" == "-cc1" || "$w1" == "-Xclang" ]]; then | ||
arg="#" | ||
fi | ||
|
||
# bash always separates '=' as a token even if there's no space before/after '='. | ||
# On the other hand, '=' is just a regular character for clang options that | ||
# contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=". | ||
# So, we need to partially undo bash tokenization here for integrity. | ||
if [[ "$cur" == -* ]]; then | ||
# -foo<tab> | ||
arg="$arg$cur" | ||
elif [[ "$w1" == -* && "$cur" == '=' ]]; then | ||
# -foo=<tab> | ||
arg="$arg$w1=," | ||
elif [[ "$cur" == -*= ]]; then | ||
# -foo=<tab> | ||
arg="$arg$cur," | ||
elif [[ "$w1" == -* ]]; then | ||
# -foo <tab> or -foo bar<tab> | ||
arg="$arg$w1,$cur" | ||
elif [[ "$w2" == -* && "$w1" == '=' ]]; then | ||
# -foo=bar<tab> | ||
arg="$arg$w2=,$cur" | ||
elif [[ ${cur: -1} != '=' && ${cur/=} != $cur ]]; then | ||
# -foo=bar<tab> | ||
arg="$arg${cur%=*}=,${cur#*=}" | ||
fi | ||
|
||
# expand ~ to $HOME | ||
eval local path=${COMP_WORDS[0]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks unnnecessary. Why not just invoke There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we replace it with $words[0], it wouldn't work with something like ~/bin/clang where ~ needs to be expanded. |
||
flags=$( "$path" --autocomplete="$arg" 2>/dev/null | sed -e 's/\t.*//' ) | ||
# If clang is old that it does not support --autocomplete, | ||
# fall back to the filename completion. | ||
if [[ "$?" != 0 ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
_clang_filedir | ||
return | ||
fi | ||
|
||
# When clang does not emit any possible autocompletion, or user pushed tab after " ", | ||
# just autocomplete files. | ||
if [[ "$flags" == "$(echo -e '\n')" || "$arg" == "" ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The echo can probably be simplified as |
||
# If -foo=<tab> and there was no possible values, autocomplete files. | ||
[[ "$cur" == '=' || "$cur" == -*= ]] && cur="" | ||
_clang_filedir | ||
elif [[ "$cur" == '=' ]]; then | ||
COMPREPLY=( $( compgen -W "$flags" -- "") ) | ||
else | ||
# Bash automatically appends a space after '=' by default. | ||
# Disable it so that it works nicely for options in the form of -foo=bar. | ||
[[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null | ||
COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) ) | ||
fi | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
complete -F _clang clang | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First line including
-*- shell-script -*-
comment missing