Skip to content
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

Support clang's new shell autocompletion feature #4174

Closed
Teemperor opened this issue Jul 1, 2017 · 7 comments
Closed

Support clang's new shell autocompletion feature #4174

Teemperor opened this issue Jul 1, 2017 · 7 comments

Comments

@Teemperor
Copy link

The next version of clang will ship with a --autocomplete flag that provides a backend for shells to complete all flags that the current clang binary supports. The general idea is that the shell passes the flag it currently wants to complete to clang viaclang --autocomplete=-fsyntax and then clang prints a list of flags that this completes to:

$./bin/clang --autocomplete=-std
-std-default= -std: -std= -stdlib=

This also works with all the values that the flags take, e.g. in this case return all the stdlibs clang supports in its -stdlib=libc++ flag:

$ ./bin/clang --autocomplete=-stdlib=,
libc++ libstdc++ platform

For a better overview, see the clang's bash completion script that illustrates the way the --autocomplete flag works: bash-completions.sh.

The big advantage is that this allows to have a single shell completion that works with all flags of all future clang versions (even the custom vendor versions from Apple or a self-build clang from trunk!), as the flag just queries the internal option parsing logic of the selected clang binary.

It would be very useful if fish would use this feature and also start calling this flag when trying to complete a clang flag.

@faho
Copy link
Member

faho commented Jul 2, 2017

I'd be willing to do this once I have that version on my machine. Any idea when it's going to be released?

Some notes for later:

  • It looks like this doesn't use the bash completion variables ($COMP_LINE and such), which is nice. If it does, there's already an implementation in the npm completions.

  • This seems to expect splitting on space - so it needs a string split " ".

  • I'm not sure about the --autocomplete=-stdlib=, one - it should ultimately result in -stdlib=libc++ being offered. It might need some transformations - if the token ends with a "=", append a "," to the "--autocomplete=" argument and then print "$token$reply".

@faho faho self-assigned this Jul 2, 2017
@Teemperor
Copy link
Author

LLVM/clang 5 will release on 23rd of August, but it's really up to your OS-vendor when it arrives on your machine :). If there are some API changes with the --autocomplete flag that can't be worked around from the fish complete script, then we can still influence the API for the script until the 19th of July when clang 5 is tagged (but no promises on this).

@faho
Copy link
Member

faho commented Jul 2, 2017

For fish, this would ideally receive the full clang commandline up to the cursor. I.e. we'd just call clang --autocomplete=(commandline -pc) ("-p" only selects the current "process" without any other parts in a pipe and such, "-c" only prints up to the cursor). That way it would also have the full context - which other options have already been specified and such.

Also, it would delimit different candidates with newlines, not spaces - this shouldn't break anything for bash either.

Also, it would use --autocomplete=-stdlib= to suggest options for "-stdlib=" (without the trailing ","), and it would print the entire thing - "-stdlib=libc++", not just the "libc++" part.

Support for descriptions would be nice to have - something like -stdlib=\tSelect a standard library, but that would interact badly with other shells.

None of these are dealbreakers, though.

@zanchey
Copy link
Member

zanchey commented Jul 2, 2017

Perhaps the clang maintainers would consider shipping the fish completion themselves - it might be worth trying to get it written before the release.

@Teemperor
Copy link
Author

The autocomplete support has landed in clang 5.0 which will release in two weeks and is currently in testing and available in the LLVM apt repository. We added support for flag descriptions in the format fish wants (separated by \t) and moved to newlines as separators. The ,'s between the values however stayed, sorry :).

The clang unit tests should help with explaining the API. Also ping for @yamaguchi1024 who is the code owner of this cool feature, she maybe has time to answer your questions.

@zanchey Shipping this is up to the distribution maintainers, which is quite a large and diverse group of people we would have to get on board for this to work out :/

@faho
Copy link
Member

faho commented Aug 10, 2017

We added support for flag descriptions in the format fish wants (separated by \t) and moved to newlines as separators.

Nice!

The ,'s between the values however stayed, sorry :).

There's probably a reason for them that I don't get. Anyway, we can deal with that.

mqudsi added a commit to mqudsi/fish-shell that referenced this issue Oct 23, 2018
Use clang/clang++'s own autocompletion support to complete arguments. It
is rather convoluted as clang generates autocompletions for a portion of
the current token rather than the entire token, e.g. while `--st` will
autocomplete to `--std=` (which is fine by fish), `--std=g` will
autocomplete to `gnu...` without the leading `--std=` which breaks fish'
support for the completion.

Additionally, on systems where clang/clang++ is the system compiler
(such as FreeBSD), it is very often for users to invoke a newer version
of clang/clang++ installed as clang[++]-NN instead of clang. Using a
monkey-patched version of `complete -p` to support that without breaking
(future) completions for commands like `clang-format`.

Closes fish-shell#4174.
@mqudsi mqudsi closed this as completed in 5ad2923 Oct 25, 2018
@zanchey zanchey modified the milestones: fish-future, fish-3.0 Oct 25, 2018
ridiculousfish pushed a commit to ridiculousfish/fish-shell that referenced this issue Nov 24, 2018
Use clang/clang++'s own autocompletion support to complete arguments. It
is rather convoluted as clang generates autocompletions for a portion of
the current token rather than the entire token, e.g. while `--st` will
autocomplete to `--std=` (which is fine by fish), `--std=g` will
autocomplete to `gnu...` without the leading `--std=` which breaks fish'
support for the completion.

Additionally, on systems where clang/clang++ is the system compiler
(such as FreeBSD), it is very often for users to invoke a newer version
of clang/clang++ installed as clang[++]-NN instead of clang. Using a
monkey-patched version of `complete -p` to support that without breaking
(future) completions for commands like `clang-format`.

Closes fish-shell#4174.
@floam
Copy link
Member

floam commented Nov 26, 2018

I wonder if there is a good way to try to reasonably dampen down these completions a bit.

Type clang - and hit tab - the pager can look like a real eye chart. Just one of these would probably be fine:
--save-stats --save-stats= -save-stats -save-stats= (Save llvm statistics.)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants