Commit Graph

9 Commits

Author SHA1 Message Date
ridiculousfish
e88eb508d0 Rework variable assignments during tab completion
Prior to this change, tab completing with a variable assignment like
`VAR=val cmd<tab>` would parse out and apply VAR=val, then recursively
invoke completions. This caused some awkwardness around the wrap chain -
if a wrapped command had a variable completion we risked infinite
recursion. A secondary problem is that we would run any command
substitutions inside variable assignment, which the user does not expect
to run until pressing enter.

With this change, we explicitly track variable assignments encountered
during tab completion, including both those explicitly given on the
command line and those found during wrap chain walk. We then apply them
while suppressing command substitutions.
2020-09-26 18:39:38 -07:00
ridiculousfish
cc07716dc1 Separate out variable assignments when completing
In preparation for applying variable assignments (VAR=VAL cmd), separate
them out from the command when performing completions. This includes both
those that the user typed, and any that come about through
completion --wraps.
2020-09-26 17:30:25 -07:00
ridiculousfish
45b85d28bb Completion wrap chain visited set to store only wrapped command
The "wrap chain" refers to a sequence of commands which wrap other
commands, for completion purposes. One possibility is that a wrap chain
will produce a combinatorial explosion or even an infinite loop, so there
needs to be logic to prevent that. Part of that logic is encapsulated in a
visited set (wrap_chain_visited_set_t) to prevent exploring the same item
twice.

Prior to this change, we stored pairs (command, wrapped_command). But we
only really need to store the wrapped command. Switch to that.

One consequence is that if a command wraps another command in more than
one way, we won't explore both ways. This seems unlikely in practice.
2020-09-26 17:24:59 -07:00
Johannes Altmanninger
45e7c709f4 Consolidate complete cycle detection and always report error on cycle
Detect recursive calls to builtin complete and the internal completion in
the same place.

In 0a0149cc2 (Prevent infinite recursion when completion wraps variable assignment)
we don't print an error when completing certain aliases like:

	alias vim "A=B vim"

But we also gave no completions.
We could make this case work, but I think that trying to salvage situations
like this one is way too complex. Instead, let the user know by printing an
error. Not sure if the style of the error fits.

We could add some heuristic to alias to not add --wraps in some cyclic cases.
2020-09-26 14:56:03 +02:00
Johannes Altmanninger
0a0149cc2a Prevent infinite recursion when completion wraps variable assignment
Closes #7344

Apply a targeted fix to the place where complete() is called to handle nested
variable assignments.  Sadly, reporting an error is probably not okay here,
because people might legitimately use aliases like:

	alias vim "A=B command vim"

This is all a bit ugly, and I hope to find a cleaner solution.  Supporting
completions on commandlines like `x=$PWD cd $x/ ` is a nice feature but it
comes with some complexity.
2020-09-22 01:11:18 +02:00
Jason
3cf6ebc0e1 Amend typos and grammar errors 2019-11-25 13:07:15 +01:00
Johannes Altmanninger
e167714899 Raise the recursion limit of complete
Users should generally prefer to use complete --wraps but this
corrects some unexpected behavior.

Fixes #3474
2019-10-03 21:25:19 +02:00
Johannes Altmanninger
aa011f70a8 Print an error when complete reaches its recursion limit 2019-10-03 21:25:19 +02:00
ridiculousfish
5f64972908 Do not try the same (command, wraps) pair more than once when completing
This prevents runaway wrap chains. Fixes #5638.
2019-09-21 14:09:38 -07:00