-
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?
Conversation
A new autocompletion feature for clang, by using its new --autocomplete API to get possible flags dynamically.
completions/clang
Outdated
@@ -0,0 +1,81 @@ | |||
_clang_filedir() | |||
{ |
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.
We don't need this, just use _filedir.
completions/clang
Outdated
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 comment
The 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.
completions/clang
Outdated
cur="${COMP_WORDS[$cword]}" | ||
fi | ||
|
||
w1="${COMP_WORDS[$cword - 1]}" |
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.
I suppose we already have $prev for this?
completions/clang
Outdated
|
||
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The echo can probably be simplified as $'\n'
completions/clang
Outdated
[[ "${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 comment
The reason will be displayed to describe this comment to others. Learn more.
&&
missing here, see existing completions.
fi | ||
|
||
# expand ~ to $HOME | ||
eval local path=${COMP_WORDS[0]} |
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.
This looks unnnecessary. Why not just invoke $words[0]
?
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.
If we replace it with $words[0], it wouldn't work with something like ~/bin/clang where ~ needs to be expanded.
completions/clang
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
[[ $? -ne 0 ]]
COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) ) | ||
fi | ||
} | ||
complete -F _clang clang |
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.
# ex: filetype=sh
missing at EOF
completions/clang
Outdated
@@ -0,0 +1,81 @@ | |||
_clang_filedir() |
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
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.
Brief review done, some inline comments added. Additionally:
- completions/Makefile.am modifications missing
- Unit test cases missing
A new autocompletion feature for clang, by using its new --autocomplete API to get possible flags dynamically.
@scop |
Travis has the LLVM repository setup, so I think we could get the most recent clang from there for completion testing. |
@scop Could you review? |
Have you suggested including this with clang, instead of in bash-completion? That would solve any versioning issues, and the completion could just assume that the corresponding clang is installed etc. |
We already included this script in clang, as below. But we wanted to make this completion default on bash. https://github.com/llvm-mirror/clang/blob/master/utils/bash-autocomplete.sh |
I'm not sure if I understand correctly what you mean by "make this completion default on bash". But I guess what you're after is that it gets installed properly and auto-enabled as appropriate -- if so, you should be just installing it properly along with the rest of clang. There should be no reason to ship another copy with bash-completion; the duplication just causes problems and increases maintenance. We have support files for cmake and instructions how to use them in README.md, so proper install of the completion file should be straightforward. Something like this (untested) should work, feel free to submit it to clang upstream for me: https://github.com/scop/clang/tree/bash-completion-install |
@scop Hmm, I'm not sure how this would for example work on OS X. Because clang there is provided by Apple, but bash-completion scripts by the brew maintainers. Any idea how to solve this? Because I fear that this will end with the brew maintainers patching in this completion on their own. |
c0e9459
to
09307f8
Compare
This patch adds support for autocompleting clang flags, by using its new --autocomplete API to get all possible flags and their values dynamically.
Works with clang 5.0 released in August 23 and will fall back to the default completion for older clang versions.
(The fish maintainers are also using this new API here: fish-shell/fish-shell#4174 )