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

refactor: refactor _comp_{get_first_word,count_args} #1036

Merged
merged 7 commits into from
Sep 12, 2023

Conversation

akinomyoga
Copy link
Collaborator

@akinomyoga akinomyoga commented Aug 13, 2023

Waiting for #1033 and #1034


There are many handwritten for-loops in the codebase that can be replaced by these functions with small adjustments. This is the first step:

  • 0f14cc0 ad4472f Change the interface to accept detailed specs through options rather than through positional parameters.
  • 0384bd5 Add an interface to get a position of the first argument (instead of its content)
  • 521d2bb 29398ef 3127703 Change behaviors (considering <>& and skipping cword/words re-assembling that seems redundant). These need discussions.

@akinomyoga akinomyoga changed the title [Waiting for #1033 and #1034] refactor: refactor _get_first_word and _count_args [Waiting for #1033 and #1034] refactor: refactor _comp_{get_first_word,count_args} Aug 13, 2023
@akinomyoga akinomyoga changed the title [Waiting for #1033 and #1034] refactor: refactor _comp_{get_first_word,count_args} [Waiting for #1033] refactor: refactor _comp_{get_first_word,count_args} Aug 26, 2023
@akinomyoga akinomyoga force-pushed the refactor-api-5 branch 2 times, most recently from 7f818a2 to 83ba88f Compare September 2, 2023 20:07
In the current implementation, to count the number of arguments, we
reassemble `cword` and `words` by specified exclude chars (i.e.,
characters that should not be considered word breaks even though they
are in COMP_WORDBREAKS).  The reassembly by the top-level
`_comp_initialize` always specify `<>&` as exclude chars, but the
current implementaion of `_comp_count_args` do not specify them.  This
patch specifies `<>&` for `_comp_count_args` in accordance with
`_comp_initialize`.
In most of the contexts calling `_comp_count_args` in the current
codebase, `cword` and `words` are already initialized with the same
set of exclude chars (-n chars) by _comp_initialize, so there seems to
be no need to again reassemble `cword` and `words`.  This patch
removes the redundant initialization of `cword` and `words`.

* When `_comp_initialize` is called without `-s` or `-n chars` and
  `_comp_count_args` is called with an empty $1, they are compatible
  so `cword` and `words` outside can be directly used.

* `7z` and `nslookup` specify the same set of exclude chars as
  `_comp_initialize`, so existing `cword` and `words` are compatible.

Nevertheless, we need to reinitialize `cword` and `words` when the
specified exclude chars are different from those specified to
_comp_initialize.

* The `ssh` completion calls `_comp_count_args` with excluding `=`
  while it calls `_comp_initialize` with `-n :`.

* The `chown` completion calls `_comp_count_args` with excluding `:`
  while it calls `_comp_initialize` with `-sn :`.

* The `nc` completions calls `_comp_count_args` without exclude chars
  while it calls `_comp_initialize` with `-n :`.

* The completions for `chmod`, `cryptsetup`, `hcitool`, `mkinitrd`,
  `patch`, `quota`, and `zopflipng` does not specify exclude chars to
  `_comp_count_args` while they specify `-s` to `_comp_initialize`.

It is not clear whether these discrepancies are intended ones are not,
but this patch tries to keep the current behavior by explicitly
specifying the original exclude chars.
These completions induce reassembling `cword` and `words` inside
`_comp_count_args`, but they are likely to be intended to be the same
as outside `cword` and `words`.  We just skip the reassembling by
dropping `-n ""` or `-n :`.
@akinomyoga akinomyoga changed the title [Waiting for #1033] refactor: refactor _comp_{get_first_word,count_args} refactor: refactor _comp_{get_first_word,count_args} Sep 2, 2023
@akinomyoga akinomyoga marked this pull request as ready for review September 9, 2023 04:22
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.

Nice 👍

@scop scop merged commit 0661fb5 into scop:master Sep 12, 2023
7 checks passed
@akinomyoga akinomyoga deleted the refactor-api-5 branch September 17, 2023 17:50
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