refactor(complete): Remove low-value w macro

This commit is contained in:
Ed Page 2024-08-16 10:10:26 -05:00
parent 17d6d24232
commit 6842ed96da
6 changed files with 36 additions and 48 deletions

View file

@ -1,12 +1,3 @@
macro_rules! w {
($buf:expr, $to_w:expr) => {
match $buf.write_all($to_w) {
Ok(..) => (),
Err(..) => panic!("Failed to write to generated file"),
}
};
}
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
macro_rules! debug { macro_rules! debug {
($($arg:tt)*) => { ($($arg:tt)*) => {

View file

@ -20,10 +20,9 @@ impl Generator for Bash {
let fn_name = bin_name.replace('-', "__"); let fn_name = bin_name.replace('-', "__");
w!( write!(
buf, buf,
format!( "_{name}() {{
"_{name}() {{
local i cur prev opts cmd local i cur prev opts cmd
COMPREPLY=() COMPREPLY=()
cur=\"${{COMP_WORDS[COMP_CWORD]}}\" cur=\"${{COMP_WORDS[COMP_CWORD]}}\"
@ -66,15 +65,13 @@ else
complete -F _{name} -o bashdefault -o default {name} complete -F _{name} -o bashdefault -o default {name}
fi fi
", ",
name = bin_name, name = bin_name,
cmd = fn_name, cmd = fn_name,
name_opts = all_options_for_path(cmd, bin_name), name_opts = all_options_for_path(cmd, bin_name),
name_opts_details = option_details_for_path(cmd, bin_name), name_opts_details = option_details_for_path(cmd, bin_name),
subcmds = all_subcommands(cmd, &fn_name), subcmds = all_subcommands(cmd, &fn_name),
subcmd_details = subcommand_details(cmd) subcmd_details = subcommand_details(cmd)
) ).expect("failed to write completion file");
.as_bytes()
);
} }
} }
@ -274,22 +271,23 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String {
let mut opts = String::new(); let mut opts = String::new();
for short in utils::shorts_and_visible_aliases(p) { for short in utils::shorts_and_visible_aliases(p) {
write!(&mut opts, "-{short} ").unwrap(); write!(&mut opts, "-{short} ").expect("writing to String is infallible");
} }
for long in utils::longs_and_visible_aliases(p) { for long in utils::longs_and_visible_aliases(p) {
write!(&mut opts, "--{long} ").unwrap(); write!(&mut opts, "--{long} ").expect("writing to String is infallible");
} }
for pos in p.get_positionals() { for pos in p.get_positionals() {
if let Some(vals) = utils::possible_values(pos) { if let Some(vals) = utils::possible_values(pos) {
for value in vals { for value in vals {
write!(&mut opts, "{} ", value.get_name()).unwrap(); write!(&mut opts, "{} ", value.get_name())
.expect("writing to String is infallible");
} }
} else { } else {
write!(&mut opts, "{pos} ").unwrap(); write!(&mut opts, "{pos} ").expect("writing to String is infallible");
} }
} }
for (sc, _) in utils::subcommands(p) { for (sc, _) in utils::subcommands(p) {
write!(&mut opts, "{sc} ").unwrap(); write!(&mut opts, "{sc} ").expect("writing to String is infallible");
} }
opts.pop(); opts.pop();

View file

@ -22,7 +22,8 @@ impl Generator for Elvish {
let subcommands_cases = generate_inner(cmd, ""); let subcommands_cases = generate_inner(cmd, "");
let result = format!( write!(
buf,
r#" r#"
use builtin; use builtin;
use str; use str;
@ -46,9 +47,8 @@ set edit:completion:arg-completer[{bin_name}] = {{|@words|
$completions[$command] $completions[$command]
}} }}
"#, "#,
); )
.expect("failed to write completion file");
w!(buf, result.as_bytes());
} }
} }

View file

@ -42,7 +42,7 @@ impl Generator for Fish {
needs_fn_name, needs_fn_name,
using_fn_name, using_fn_name,
); );
w!(buf, buffer.as_bytes()); write!(buf, "{buffer}").expect("failed to write completion file");
} }
} }
@ -240,7 +240,9 @@ fn gen_subcommand_helpers(
} }
} }
let optspecs_fn_name = format!("__fish_{bin_name}_global_optspecs"); let optspecs_fn_name = format!("__fish_{bin_name}_global_optspecs");
let template = format!("\ write!(
buf,
"\
# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.\n\ # Print an optspec for argparse to handle cmd's options that are independent of any subcommand.\n\
function {optspecs_fn_name}\n\ function {optspecs_fn_name}\n\
\tstring join \\n{optspecs}\n\ \tstring join \\n{optspecs}\n\
@ -264,8 +266,7 @@ fn gen_subcommand_helpers(
\tand return 1\n\ \tand return 1\n\
\tcontains -- $cmd[1] $argv\n\ \tcontains -- $cmd[1] $argv\n\
end\n\n\ end\n\n\
"); ").expect("failed to write completion file");
w!(buf, template.as_bytes());
} }
fn value_completion(option: &Arg) -> String { fn value_completion(option: &Arg) -> String {

View file

@ -22,7 +22,8 @@ impl Generator for PowerShell {
let subcommands_cases = generate_inner(cmd, ""); let subcommands_cases = generate_inner(cmd, "");
let result = format!( write!(
buf,
r#" r#"
using namespace System.Management.Automation using namespace System.Management.Automation
using namespace System.Management.Automation.Language using namespace System.Management.Automation.Language
@ -51,9 +52,8 @@ Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{
Sort-Object -Property ListItemText Sort-Object -Property ListItemText
}} }}
"# "#
); )
.expect("failed to write completion file");
w!(buf, result.as_bytes());
} }
} }

View file

@ -19,10 +19,9 @@ impl Generator for Zsh {
.get_bin_name() .get_bin_name()
.expect("crate::generate should have set the bin_name"); .expect("crate::generate should have set the bin_name");
w!( write!(
buf, buf,
format!( "#compdef {name}
"#compdef {name}
autoload -U is-at-least autoload -U is-at-least
@ -49,13 +48,12 @@ else
compdef _{name} {name} compdef _{name} {name}
fi fi
", ",
name = bin_name, name = bin_name,
initial_args = get_args_of(cmd, None), initial_args = get_args_of(cmd, None),
subcommands = get_subcommands_of(cmd), subcommands = get_subcommands_of(cmd),
subcommand_details = subcommand_details(cmd) subcommand_details = subcommand_details(cmd)
) )
.as_bytes() .expect("failed to write completion file");
);
} }
} }