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

Conversation

yamaguchi1024
Copy link

@yamaguchi1024 yamaguchi1024 commented Aug 16, 2017

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 )

A new autocompletion feature for clang, by using its new
--autocomplete API to get possible flags dynamically.
@@ -0,0 +1,81 @@
_clang_filedir()
{
Copy link
Owner

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.

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
Copy link
Owner

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.

cur="${COMP_WORDS[$cword]}"
fi

w1="${COMP_WORDS[$cword - 1]}"
Copy link
Owner

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?


# When clang does not emit any possible autocompletion, or user pushed tab after " ",
# just autocomplete files.
if [[ "$flags" == "$(echo -e '\n')" || "$arg" == "" ]]; then
Copy link
Owner

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'

[[ "${flags: -1}" == '=' ]] && compopt -o nospace 2> /dev/null
COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
fi
}
Copy link
Owner

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]}
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 [[ "$?" != 0 ]]; then
Copy link
Owner

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
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

@@ -0,0 +1,81 @@
_clang_filedir()
Copy link
Owner

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

Copy link
Owner

@scop scop left a 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

@yamaguchi1024
Copy link
Author

@scop
Since this clang --autocomplete API is implemented in clang 5.0, Ubuntu 14.04 doesn't support this newest clang version. What do you think I can do about this?

@Teemperor
Copy link

Travis has the LLVM repository setup, so I think we could get the most recent clang from there for completion testing.

@yamaguchi1024
Copy link
Author

@scop Could you review?

@scop
Copy link
Owner

scop commented Sep 17, 2017

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.

@yamaguchi1024
Copy link
Author

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

@scop
Copy link
Owner

scop commented Sep 17, 2017

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
https://github.com/scop/clang/commit/ceb95f6a46f5841bff97b961c54c0730270931e9.patch

@Teemperor
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants