Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions completions/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ bashcomp_DATA = 2to3 \
chrpath \
_chsh \
cksfv \
clang \
cleanarch \
clisp \
clone_member \
Expand Down
67 changes: 67 additions & 0 deletions completions/clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# clang(1) completion -*- shell-script -*-
_clang()
{
local cur prev words cword arg flags prev2
_init_completion || return

if [[ $cword > 1 ]]; then
prev2="${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" || "$prev" == "-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 [[ "$prev" == -* && "$cur" == '=' ]]; then
# -foo=<tab>
arg="$arg$prev=,"
elif [[ "$cur" == -*= ]]; then
# -foo=<tab>
arg="$arg$cur,"
elif [[ "$prev" == -* ]]; then
# -foo <tab> or -foo bar<tab>
arg="$arg$prev,$cur"
elif [[ "$prev2" == -* && "$prev" == '=' ]]; then
# -foo=bar<tab>
arg="$arg$prev2=,$cur"
elif [[ ${cur: -1} != '=' && ${cur/=} != $cur ]]; then
# -foo=bar<tab>
arg="$arg${cur%=*}=,${cur#*=}"
fi

# expand ~ to $HOME
eval local path=${COMP_WORDS[0]}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unnnecessary. Why not just invoke $words[0]?

Copy link
Author

Choose a reason for hiding this comment

The 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 [[ $? -ne 0 ]]; then
_filedir
return
fi

# When clang does not emit any possible autocompletion, or user pushed tab after " ",
# just autocomplete files.
if [[ "$flags" == $'\n' || "$arg" == "" ]]; then
# If -foo=<tab> and there was no possible values, autocomplete files.
[[ "$cur" == '=' || "$cur" == -*= ]] && cur=""
_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
} &&
complete -F _clang clang
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# ex: filetype=sh missing at EOF

# ex: filetype=sh
1 change: 1 addition & 0 deletions test/completion/clang.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert_source_completions clang
1 change: 1 addition & 0 deletions test/docker/Dockerfile-ubuntu14
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RUN dpkg --add-architecture i386 && \
chrpath \
cksfv \
clisp \
clang \
cowsay \
cppcheck \
cryptsetup-bin \
Expand Down
18 changes: 18 additions & 0 deletions test/lib/completions/clang.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
proc setup {} {
save_env
}


proc teardown {} {
assert_env_unmodified
}


setup


assert_complete_any "clang "
sync_after_int


teardown