Skip to content

refactor(_command_offset): save Bash's $2 passed to completion functions #814

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

Merged
merged 4 commits into from
Oct 30, 2022

Conversation

akinomyoga
Copy link
Collaborator

Fixes #790

This is a fix alternative to #791. Instead of imitating the arguments passed by Bash, we may save the original arguments in _init_completion (renamed to _comp_initialize). However, this involves not only the change of _init_completion itself but also the interface change of functions that call _init_completion internally---such as _filedir_xspec, _longopt, _minimal, _known_hosts, etc. Also, this doesn't work when _comp_command_offset is used without calling _init_completion (some cases are found in external completions). We need to request external completion writers carefully update their code.

@akinomyoga akinomyoga marked this pull request as draft September 11, 2022 06:00
@akinomyoga akinomyoga changed the title refactor(_command_offset): save Bash's $2 passed to completion functions [Need discussion (cf #791)] refactor(_command_offset): save Bash's $2 passed to completion functions Sep 12, 2022
@akinomyoga akinomyoga marked this pull request as ready for review September 12, 2022 13:04
@scop
Copy link
Owner

scop commented Sep 18, 2022

Even though this PR is huge and there is an interface change involved, I feel the code here is much easier to understand than in #791; from that point of view I have a better feeling about this one.

If I understand correctly, the API change is such that it will mostly degrade gracefully, i.e. if the original arguments are not available, we'll basically do what we used to do before this change? Or will this flat out break some scenarios if not adjusted by affected completions?

External completion authors will likely be using _init_completion for a long time still. Luckily the current implementation (before this change) should be fine if it receives positional arguments, so I suppose externals can switch basically to

    local cur prev words cword split comp_args
    _init_completion -- "$@" || return

...in order to support earlier bash completion versions and to take advantage of the new stuff.

@akinomyoga
Copy link
Collaborator Author

Even though this PR is huge and there is an interface change involved, I feel the code here is much easier to understand than in #791; from that point of view I have a better feeling about this one.

OK! Then, let us consider this direction!

If I understand correctly, the API change is such that it will mostly degrade gracefully, i.e. if the original arguments are not available, we'll basically do what we used to do before this change?

Yes, it is basically right. Actually, it slightly improves the original behavior. Originally, we passed the entire word ${COMP_WORDS[COMP_CWORD]} as the second argument, but now we are passing the part of the word after the current cursor position, i.e., $cur, as Bash does for its built-in programmable completion.

External completion authors will likely be using _init_completion for a long time still. Luckily the current implementation (before this change) should be fine if it receives positional arguments, so I suppose externals can switch basically to

    local cur prev words cword split comp_args
    _init_completion -- "$@" || return

...in order to support earlier bash completion versions and to take advantage of the new stuff.

Ah, this is a good point. I hadn't noticed that point by myself. Thank you.

@akinomyoga akinomyoga changed the title [Need discussion (cf #791)] refactor(_command_offset): save Bash's $2 passed to completion functions refactor(_command_offset): save Bash's $2 passed to completion functions Sep 18, 2022
Now `_comp_initialize` receives in its arguments the original
arguments passed to the completion function. Arguments of other
functions such as `_minimal`, `_longopt`, `_filedir_xspec`,
`_known_hosts`, and `_user_at_host` are also changed.
@akinomyoga akinomyoga force-pushed the fix-command_offset-cur-B branch from dde3a00 to fc4af85 Compare October 2, 2022 22:32
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.

Ok, let's go with this. Thanks!

@scop scop merged commit b55ca9b into scop:master Oct 30, 2022
@akinomyoga akinomyoga deleted the fix-command_offset-cur-B branch October 30, 2022 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bash command completion has a problem with sudo command
2 participants