-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(complete): Handle COMP_WORDBREAKS in Bash completion #6264
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
AndreasBackx
wants to merge
2
commits into
clap-rs:master
Choose a base branch
from
AndreasBackx:feat/comp-wordbreaks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,15 @@ _clap_complete_NAME() { | |
| elif [[ $_CLAP_COMPLETE_SPACE == false ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then | ||
| compopt -o nospace | ||
| fi | ||
| if [[ -n ${COMP_WORDBREAKS+x} ]]; then | ||
| # If the current word contains a word break character, we need to strip | ||
| # the prefix up to that character from each completion | ||
| local prefix | ||
| prefix="${2%"${2##*[${COMP_WORDBREAKS}]}"}" | ||
| if [[ -n "$prefix" ]]; then | ||
| COMPREPLY=("${COMPREPLY[@]#"$prefix"}") | ||
| fi | ||
| fi | ||
| } | ||
| if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then | ||
| complete -o nospace -o bashdefault -o nosort -F _clap_complete_NAME BIN | ||
|
|
@@ -463,13 +472,40 @@ impl Zsh { | |
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use snapbox::assert_data_eq; | ||
|
|
||
| #[test] | ||
| #[cfg(feature = "unstable-dynamic")] | ||
| fn bash_registration_handles_wordbreaks() { | ||
| let mut buf = Vec::new(); | ||
| let bash = Bash; | ||
| bash.write_registration("COMPLETE", "my-app", "my-app", "my-app", &mut buf) | ||
| .expect("write_registration failed"); | ||
| let script = String::from_utf8(buf).expect("Invalid UTF-8"); | ||
|
|
||
| // The registration script must check for COMP_WORDBREAKS to handle shells | ||
| // where `=` and `:` are word-break characters. Without this, completing | ||
| // `--flag=value` would produce `--flag=--flag=value` because Bash splits | ||
| // at `=` and only replaces the part after it. | ||
| assert!( | ||
| script.contains("COMP_WORDBREAKS"), | ||
| "registration script must reference COMP_WORDBREAKS to handle word-break characters" | ||
| ); | ||
|
|
||
| // The script must strip the prefix up to the word-break character from | ||
| // each completion entry using the `${COMPREPLY[@]#"$prefix"}` pattern. | ||
| // This is the shell-side fix that prevents double-prefixing. | ||
| assert!( | ||
| script.contains(r#"COMPREPLY=("${COMPREPLY[@]#"$prefix"}")"#), | ||
| "registration script must strip word-break prefix from COMPREPLY entries" | ||
| ); | ||
| } | ||
|
|
||
| // This test verifies that fish shell path quoting works with or without spaces in the path. | ||
| #[test] | ||
| #[cfg(all(unix, feature = "unstable-dynamic"))] | ||
| #[cfg(feature = "unstable-shell-tests")] | ||
| fn fish_env_completer_path_quoting_works() { | ||
| use snapbox::assert_data_eq; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this being added in this commit |
||
| // Returns the dynamic registration line for the fish shell, for example: | ||
| // complete --keep-order --exclusive --command my-bin --arguments "(COMPLETE=fish /path/to/my-bin ... )" | ||
| let get_fish_registration = |completer_bin: &str| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please test behavior, not implementation