Skip to content

Commit

Permalink
feat(complete): Add autocomplete for visible_alias to elvish
Browse files Browse the repository at this point in the history
  • Loading branch information
pzmarzly committed Apr 29, 2024
1 parent f1be587 commit cf5a411
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
35 changes: 22 additions & 13 deletions clap_complete/src/shells/elvish.rs
Expand Up @@ -67,10 +67,13 @@ fn escape_help<T: ToString>(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();
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions clap_complete/tests/snapshots/sub_subcommands.elvish
Expand Up @@ -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'= {
Expand All @@ -45,13 +46,28 @@ 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'')'
cand --help 'Print help (see more with ''--help'')'
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)'
Expand All @@ -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'
Expand Down

0 comments on commit cf5a411

Please sign in to comment.