Skip to content

fix: completion generation inside $() #1403

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

richardkmichael
Copy link

Closes #630

  • python -m pytest test/t/unit has two failing tests and two skipped tests in test_unit_compgen_filedir.py. This is the same as on main, so I do not believe they are related to this patch.

  • python -m mypy . and python -m ruff chec pass

  • I am unsure what to make of the runLint output, but it has exit status 0, so I presume it is OK

Command substitution completion was incorrectly setting the `prev`
variable due to the `_comp_initialize` function reassigning it from
the original completion context after processing the substitution.

The fix ensures that `words` and `cword` variables are also updated
to match the command substitution context, preventing the later
reassignment from overriding the correct `prev` value.

Unit test added to validate command substitution correctly sets
`COMP_LINE`, `COMP_CWORD`, `cur`, and `prev` variables for both single
commands and commands with arguments.
Copy link
Collaborator

@yedayak yedayak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In main echo $(ip <TAB> completes all files (the default completion, it should complete sub-commands), but with this PR it doesn't complete anything. Maybe you can use _comp_command_offset for this?

# Replace the completion variables with ones that reflect the inner context.
COMP_LINE=$inner_line
read -ra COMP_WORDS <<<"$inner_line"
COMP_CWORD=$((${#COMP_WORDS[@]} - 1))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can become negative if there isn't anything inside the $():

echo $(<TAB>bash: COMP_WORDS: bad array subscript

@richardkmichael
Copy link
Author

@yedayak Thanks for the quick review. I'll investigate.

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-completion fails to generate path completions with $(
2 participants