From 47cbc5f247d2aaa43edd0c7ff95b7668c4625f39 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 3 May 2023 21:02:30 -0400 Subject: [PATCH] chore: Deduplicate powershell code complete Makes powershell code more concise --- clap_complete/src/shells/powershell.rs | 79 ++++++++------------------ 1 file changed, 25 insertions(+), 54 deletions(-) diff --git a/clap_complete/src/shells/powershell.rs b/clap_complete/src/shells/powershell.rs index 92467d53..e449e2e3 100644 --- a/clap_complete/src/shells/powershell.rs +++ b/clap_complete/src/shells/powershell.rs @@ -84,63 +84,11 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { let preamble = String::from("\n [CompletionResult]::new("); for option in p.get_opts() { - if let Some(shorts) = option.get_short_and_visible_aliases() { - let tooltip = get_tooltip(option.get_help(), shorts[0]); - for short in shorts { - completions.push_str(&preamble); - completions.push_str( - format!( - "'-{}', '{}', {}, '{}')", - short, short, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } - } - - if let Some(longs) = option.get_long_and_visible_aliases() { - let tooltip = get_tooltip(option.get_help(), longs[0]); - for long in longs { - completions.push_str(&preamble); - completions.push_str( - format!( - "'--{}', '{}', {}, '{}')", - long, long, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } - } + generate_aliases(&mut completions, &preamble, option); } for flag in utils::flags(p) { - if let Some(shorts) = flag.get_short_and_visible_aliases() { - let tooltip = get_tooltip(flag.get_help(), shorts[0]); - for short in shorts { - completions.push_str(&preamble); - completions.push_str( - format!( - "'-{}', '{}', {}, '{}')", - short, short, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } - } - - if let Some(longs) = flag.get_long_and_visible_aliases() { - let tooltip = get_tooltip(flag.get_help(), longs[0]); - for long in longs { - completions.push_str(&preamble); - completions.push_str( - format!( - "'--{}', '{}', {}, '{}')", - long, long, "[CompletionResultType]::ParameterName", tooltip - ) - .as_str(), - ); - } - } + generate_aliases(&mut completions, &preamble, &flag); } for subcommand in p.get_subcommands() { @@ -172,3 +120,26 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { subcommands_cases } + +fn generate_aliases(completions: &mut String, preamble: &String, arg: &Arg) { + use std::fmt::Write as _; + + if let Some(aliases) = arg.get_short_and_visible_aliases() { + let tooltip = get_tooltip(arg.get_help(), aliases[0]); + for alias in aliases { + let _ = write!( + completions, + "{preamble}'-{alias}', '{alias}', [CompletionResultType]::ParameterName, '{tooltip}')" + ); + } + } + if let Some(aliases) = arg.get_long_and_visible_aliases() { + let tooltip = get_tooltip(arg.get_help(), aliases[0]); + for alias in aliases { + let _ = write!( + completions, + "{preamble}'--{alias}', '{alias}', [CompletionResultType]::ParameterName, '{tooltip}')" + ); + } + } +}