From cf5a41155e0f2e78f19b952632654b5f28a9dfbc Mon Sep 17 00:00:00 2001 From: Pawel Zmarzly Date: Mon, 29 Apr 2024 23:14:04 +0200 Subject: [PATCH] feat(complete): Add autocomplete for visible_alias to elvish --- clap_complete/src/shells/elvish.rs | 35 ++++++++++++------- .../tests/snapshots/sub_subcommands.elvish | 24 +++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/clap_complete/src/shells/elvish.rs b/clap_complete/src/shells/elvish.rs index 082cb3df5e0..da20deb37ec 100644 --- a/clap_complete/src/shells/elvish.rs +++ b/clap_complete/src/shells/elvish.rs @@ -67,10 +67,13 @@ fn escape_help(help: Option<&StyledStr>, data: T) -> String { fn generate_inner(p: &Command, previous_command_name: &str) -> String { debug!("generate_inner"); - let command_name = if previous_command_name.is_empty() { - p.get_bin_name().expect(INTERNAL_ERROR_MSG).to_string() + let command_names = if previous_command_name.is_empty() { + vec![p.get_bin_name().expect(INTERNAL_ERROR_MSG).to_string()] } else { - format!("{};{}", previous_command_name, &p.get_name()) + p.get_name_and_visible_aliases() + .into_iter() + .map(|name| format!("{};{}", previous_command_name, name)) + .collect() }; let mut completions = String::new(); @@ -113,23 +116,29 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { } for subcommand in p.get_subcommands() { - let data = &subcommand.get_name(); - let tooltip = escape_help(subcommand.get_about(), data); + for name in subcommand.get_name_and_visible_aliases() { + let tooltip = escape_help(subcommand.get_about(), name); - completions.push_str(&preamble); - completions.push_str(format!("{data} '{tooltip}'").as_str()); + completions.push_str(&preamble); + completions.push_str(format!("{name} '{tooltip}'").as_str()); + } } - let mut subcommands_cases = format!( - r" + let mut subcommands_cases = String::new(); + for command_name in &command_names { + subcommands_cases.push_str(&format!( + r" &'{}'= {{{} }}", - &command_name, completions - ); + &command_name, completions + )); + } for subcommand in p.get_subcommands() { - let subcommand_subcommands_cases = generate_inner(subcommand, &command_name); - subcommands_cases.push_str(&subcommand_subcommands_cases); + for command_name in &command_names { + let subcommand_subcommands_cases = generate_inner(subcommand, command_name); + subcommands_cases.push_str(&subcommand_subcommands_cases); + } } subcommands_cases diff --git a/clap_complete/tests/snapshots/sub_subcommands.elvish b/clap_complete/tests/snapshots/sub_subcommands.elvish index d42e344c495..68b72f66f20 100644 --- a/clap_complete/tests/snapshots/sub_subcommands.elvish +++ b/clap_complete/tests/snapshots/sub_subcommands.elvish @@ -28,6 +28,7 @@ set edit:completion:arg-completer[my-app] = {|@words| cand --version 'Print version' cand test 'tests things' cand some_cmd 'top level subcommand' + cand some_cmd_alias 'top level subcommand' cand help 'Print this message or the help of the given subcommand(s)' } &'my-app;test'= { @@ -45,6 +46,14 @@ set edit:completion:arg-completer[my-app] = {|@words| cand sub_cmd 'sub-subcommand' cand help 'Print this message or the help of the given subcommand(s)' } + &'my-app;some_cmd_alias'= { + cand -h 'Print help' + cand --help 'Print help' + cand -V 'Print version' + cand --version 'Print version' + cand sub_cmd 'sub-subcommand' + cand help 'Print this message or the help of the given subcommand(s)' + } &'my-app;some_cmd;sub_cmd'= { cand --config 'the other case to test' cand -h 'Print help (see more with ''--help'')' @@ -52,6 +61,13 @@ set edit:completion:arg-completer[my-app] = {|@words| cand -V 'Print version' cand --version 'Print version' } + &'my-app;some_cmd_alias;sub_cmd'= { + cand --config 'the other case to test' + cand -h 'Print help (see more with ''--help'')' + cand --help 'Print help (see more with ''--help'')' + cand -V 'Print version' + cand --version 'Print version' + } &'my-app;some_cmd;help'= { cand sub_cmd 'sub-subcommand' cand help 'Print this message or the help of the given subcommand(s)' @@ -60,6 +76,14 @@ set edit:completion:arg-completer[my-app] = {|@words| } &'my-app;some_cmd;help;help'= { } + &'my-app;some_cmd_alias;help'= { + cand sub_cmd 'sub-subcommand' + cand help 'Print this message or the help of the given subcommand(s)' + } + &'my-app;some_cmd_alias;help;sub_cmd'= { + } + &'my-app;some_cmd_alias;help;help'= { + } &'my-app;help'= { cand test 'tests things' cand some_cmd 'top level subcommand'