Skip to content

Commit

Permalink
fix(complete): Use existing display order for Arg/Command
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 20, 2024
1 parent c6b5d62 commit 40532e9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
8 changes: 4 additions & 4 deletions clap_complete/src/engine/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,10 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<CompletionCandid
value
);

let mut scs = subcommands(cmd)
subcommands(cmd)
.into_iter()
.filter(|x| x.get_value().starts_with(value))
.collect::<Vec<_>>();
scs.sort();
scs
.collect()
}

/// Gets all the long options, their visible aliases and flags of a [`clap::Command`] with formatted `--` prefix.
Expand Down Expand Up @@ -487,6 +485,7 @@ fn populate_arg_candidate(candidate: CompletionCandidate, arg: &clap::Arg) -> Co
.to_owned()
.into(),
))
.display_order(Some(arg.get_display_order()))
.hide(arg.is_hide_set())
}

Expand Down Expand Up @@ -535,6 +534,7 @@ fn populate_command_candidate(
.to_owned()
.into(),
))
.display_order(Some(subcommand.get_display_order()))
.hide(subcommand.is_hide_set())
}
/// Parse the short flags and find the first `takes_values` option.
Expand Down
10 changes: 5 additions & 5 deletions clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
%
action help last quote --global --help
alias hint pacman value --generate --version
action value last hint --global --help
quote pacman alias help --generate --version
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand All @@ -275,9 +275,9 @@ fn complete_dynamic_env_quoted_help() {
let input = "exhaustive quote \t\t";
let expected = snapbox::str![[r#"
%
cmd-backslash cmd-double-quotes escape-help --double-quotes --brackets --global
cmd-backticks cmd-expansions help --backticks --expansions --help
cmd-brackets cmd-single-quotes --single-quotes --backslash --choice --version
cmd-single-quotes cmd-backslash escape-help --global --backslash --choice
cmd-double-quotes cmd-brackets help --double-quotes --brackets --help
cmd-backticks cmd-expansions --single-quotes --backticks --expansions --version
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down
10 changes: 5 additions & 5 deletions clap_complete/tests/testsuite/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn suggest_subcommand_subset() {
assert_data_eq!(
complete!(cmd, "he"),
snapbox::str![[r#"
hello-moon
hello-world
hello-moon
help Print this message or the help of the given subcommand(s)
"#]],
);
Expand Down Expand Up @@ -105,8 +105,8 @@ fn suggest_subcommand_aliases() {
assert_data_eq!(
complete!(cmd, "hello"),
snapbox::str![[r#"
hello-moon
hello-world
hello-moon
"#]],
);
}
Expand Down Expand Up @@ -1099,26 +1099,26 @@ fn sort_and_filter() {
assert_data_eq!(
complete!(cmd, " [TAB]"),
snapbox::str![[r#"
help Print this message or the help of the given subcommand(s)
sub
help Print this message or the help of the given subcommand(s)
pos-a
pos-b
pos-c
--required-flag
--optional-flag
--long-flag
--help Print help
-s
--help Print help
"#]]
);
assert_data_eq!(
complete!(cmd, "-[TAB]"),
snapbox::str![[r#"
-r --required-flag
-o --optional-flag
--long-flag
-s
-h Print help
--long-flag
"#]]
);
assert_data_eq!(
Expand Down
15 changes: 7 additions & 8 deletions clap_complete/tests/testsuite/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,9 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
% exhaustive action
action last --global (everywhere)
alias pacman --generate (generate)
help (Print this message or the help of the given subcommand(s)) quote --help (Print help)
hint value --version (Print version)
action pacman hint --generate (generate)
quote last help (Print this message or the help of the given subcommand(s)) --help (Print help)
value alias --global (everywhere) --version (Print version)
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand All @@ -214,22 +213,22 @@ fn complete_dynamic_env_quoted_help() {
let input = "exhaustive quote \t\t";
let expected = snapbox::str![[r#"
% exhaustive quote
cmd-backslash (Avoid '/n')
cmd-single-quotes (Can be 'always', 'auto', or 'never')
cmd-double-quotes (Can be "always", "auto", or "never")
cmd-backticks (For more information see `echo test`)
cmd-backslash (Avoid '/n')
cmd-brackets (List packages [filter])
cmd-double-quotes (Can be "always", "auto", or "never")
cmd-expansions (Execute the shell command with $SHELL)
cmd-single-quotes (Can be 'always', 'auto', or 'never')
escape-help (/tab "')
help (Print this message or the help of the given subcommand(s))
--single-quotes (Can be 'always', 'auto', or 'never')
--global (everywhere)
--double-quotes (Can be "always", "auto", or "never")
--backticks (For more information see `echo test`)
--backslash (Avoid '/n')
--brackets (List packages [filter])
--expansions (Execute the shell command with $SHELL)
--choice
--global (everywhere)
--help (Print help (see more with '--help'))
--version (Print version)
"#]];
Expand Down
10 changes: 5 additions & 5 deletions clap_complete/tests/testsuite/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
% exhaustive
--generate --help action help last quote
--global --version alias hint pacman value
action --generate --help hint pacman value
alias --global help last quote --version
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand All @@ -205,9 +205,9 @@ fn complete_dynamic_env_quoted_help() {
let input = "exhaustive quote \t\t";
let expected = snapbox::str![[r#"
% exhaustive quote
--backslash --choice --global --version cmd-brackets cmd-single-quotes
--backticks --double-quotes --help cmd-backslash cmd-double-quotes escape-help
--brackets --expansions --single-quotes cmd-backticks cmd-expansions help
--backslash --choice cmd-brackets cmd-single-quotes --expansions help
--backticks cmd-backslash cmd-double-quotes --double-quotes --global --single-quotes
--brackets cmd-backticks cmd-expansions escape-help --help --version
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down

0 comments on commit 40532e9

Please sign in to comment.