Skip to content

Commit

Permalink
fix(complete): Fix zsh.rs subcommand deduplication
Browse files Browse the repository at this point in the history
Fixing the iteration over all_subcommands in zsh.rs. We deduplicate
values on (sc_name, bin_name) keys, but then only iterate on bin_name.
This doesn't cause problems now, since all bin names seem to be unique.
However, without fixing this, the next commit would have started
generating duplicated functions with same names.

For example, with an #[long = "foo", visible_alias = "bar"] subcommand,
we'll end up with 2 pairs: [("foo", "foo"), ("bar", "foo")]. Before this
commit, we would have ended up generating _my-app__foo_commands()
functions. These functions should have identical content, so it is not
an error, just an inefficiency that we can fix.
  • Loading branch information
pzmarzly committed May 17, 2024
1 parent 5be548d commit be15bd5
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 248 deletions.
11 changes: 7 additions & 4 deletions clap_complete/src/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ _{bin_name_underscore}_commands() {{
ret.push(parent_text);

// Next we start looping through all the children, grandchildren, etc.
let mut all_subcommands = utils::all_subcommands(p);
let mut all_subcommand_bins: Vec<_> = utils::all_subcommands(p)
.into_iter()
.map(|(_sc_name, bin_name)| bin_name)
.collect();

all_subcommands.sort();
all_subcommands.dedup();
all_subcommand_bins.sort();
all_subcommand_bins.dedup();

for (_, ref bin_name) in &all_subcommands {
for bin_name in &all_subcommand_bins {
debug!("subcommand_details:iter: bin_name={bin_name}");

ret.push(format!(
Expand Down
Loading

0 comments on commit be15bd5

Please sign in to comment.