Skip to content

refactor(_command_offset): refactor the adjustment of COMP_WORDS #797

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 2 commits into from
Sep 9, 2022

Conversation

akinomyoga
Copy link
Collaborator

@akinomyoga akinomyoga commented Sep 3, 2022

Extracted from #790 #791. This is also actually independent.

  • d2c0b4e simplify logic: The original code tries to manually perform a string manipulation that can be achieved by a single parameter expansion. Also, it tries to manually perform the array compaction that can be achieved by a single parameter expansion. The behaviors of the new codes slightly differ from the original ones, but I believe the behaviors of the new codes are better:
    • In the original code, COMP_LINE becomes empty when ${COMP_WORDS[i]} is not contained in $COMP_LINE, though I don't think that happens as far as these variables are modified by external codes. In the new code, such a word ${COMP_WORDS[i]} is just ignored.
    • In the original code, the words in COMP_WORDS after COMP_CWORD is not modified, but just the word at COMP_CWORD and the words preceding it are shifted. For example, COMP_WORDS=([0]=sudo [1]=A [2]=B [3]=C [4]=D) with COMP_CWORD=2 becomes COMP_WORDS=([0]=A [1]=B [3]=C [4]=D) instead of COMP_WORDS=([0]=A [1]=B [2]=C [3]=D). In the new code, all the words are shifted.
  • 995db59 localize changes to COMP_*: The original implementation of _comp_offset breaks the state of COMP_* after the completion of _comp_offset. Currently, it doesn't cause a problem because all the completion functions in bash-completion that use _comp_offset seem to return from the function immediately after the call of _comp_offset. But I still think we should localize the changes to COMP_* within _command_offset and functions called from it.
    • This change might possibly break existing external completion functions if there are completion functions that use the modified values of COMP_* after calling _comp_offset.
    • The test seems to ignore also other variables cur, words, and cword, and they do not seem to be modified by the current implementation of _command_offset.

The description is the same as was in #791.

Note: Previously, the words after COMP_CWORD have been kept at the
original positions in the array COMP_WORDS, but we shift all the words
in the new implementation.
@akinomyoga akinomyoga force-pushed the _command_offset-comp_words branch from 012966c to 4f9678e Compare September 4, 2022 03:29
@scop scop merged commit 575da51 into scop:master Sep 9, 2022
@scop
Copy link
Owner

scop commented Sep 9, 2022

Thanks!

@akinomyoga akinomyoga deleted the _command_offset-comp_words branch September 10, 2022 12:46
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.

2 participants