Skip to content

Commit

Permalink
refactor(complete): Remove low-value w macro
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 16, 2024
1 parent 17d6d24 commit 6842ed9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 48 deletions.
9 changes: 0 additions & 9 deletions clap_complete/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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")]
macro_rules! debug {
($($arg:tt)*) => {
Expand Down
32 changes: 15 additions & 17 deletions clap_complete/src/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ impl Generator for Bash {

let fn_name = bin_name.replace('-', "__");

w!(
write!(
buf,
format!(
"_{name}() {{
"_{name}() {{
local i cur prev opts cmd
COMPREPLY=()
cur=\"${{COMP_WORDS[COMP_CWORD]}}\"
Expand Down Expand Up @@ -66,15 +65,13 @@ else
complete -F _{name} -o bashdefault -o default {name}
fi
",
name = bin_name,
cmd = fn_name,
name_opts = all_options_for_path(cmd, bin_name),
name_opts_details = option_details_for_path(cmd, bin_name),
subcmds = all_subcommands(cmd, &fn_name),
subcmd_details = subcommand_details(cmd)
)
.as_bytes()
);
name = bin_name,
cmd = fn_name,
name_opts = all_options_for_path(cmd, bin_name),
name_opts_details = option_details_for_path(cmd, bin_name),
subcmds = all_subcommands(cmd, &fn_name),
subcmd_details = subcommand_details(cmd)
).expect("failed to write completion file");
}
}

Expand Down Expand Up @@ -274,22 +271,23 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String {

let mut opts = String::new();
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) {
write!(&mut opts, "--{long} ").unwrap();
write!(&mut opts, "--{long} ").expect("writing to String is infallible");
}
for pos in p.get_positionals() {
if let Some(vals) = utils::possible_values(pos) {
for value in vals {
write!(&mut opts, "{} ", value.get_name()).unwrap();
write!(&mut opts, "{} ", value.get_name())
.expect("writing to String is infallible");
}
} else {
write!(&mut opts, "{pos} ").unwrap();
write!(&mut opts, "{pos} ").expect("writing to String is infallible");
}
}
for (sc, _) in utils::subcommands(p) {
write!(&mut opts, "{sc} ").unwrap();
write!(&mut opts, "{sc} ").expect("writing to String is infallible");
}
opts.pop();

Expand Down
8 changes: 4 additions & 4 deletions clap_complete/src/shells/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl Generator for Elvish {

let subcommands_cases = generate_inner(cmd, "");

let result = format!(
write!(
buf,
r#"
use builtin;
use str;
Expand All @@ -46,9 +47,8 @@ set edit:completion:arg-completer[{bin_name}] = {{|@words|
$completions[$command]
}}
"#,
);

w!(buf, result.as_bytes());
)
.expect("failed to write completion file");
}
}

Expand Down
9 changes: 5 additions & 4 deletions clap_complete/src/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Generator for Fish {
needs_fn_name,
using_fn_name,
);
w!(buf, buffer.as_bytes());
write!(buf, "{buffer}").expect("failed to write completion file");
}
}

Expand Down Expand Up @@ -240,7 +240,9 @@ fn gen_subcommand_helpers(
}
}
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\
function {optspecs_fn_name}\n\
\tstring join \\n{optspecs}\n\
Expand All @@ -264,8 +266,7 @@ fn gen_subcommand_helpers(
\tand return 1\n\
\tcontains -- $cmd[1] $argv\n\
end\n\n\
");
w!(buf, template.as_bytes());
").expect("failed to write completion file");
}

fn value_completion(option: &Arg) -> String {
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/src/shells/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl Generator for PowerShell {

let subcommands_cases = generate_inner(cmd, "");

let result = format!(
write!(
buf,
r#"
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Expand Down Expand Up @@ -51,9 +52,8 @@ Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{
Sort-Object -Property ListItemText
}}
"#
);

w!(buf, result.as_bytes());
)
.expect("failed to write completion file");
}
}

Expand Down
18 changes: 8 additions & 10 deletions clap_complete/src/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ impl Generator for Zsh {
.get_bin_name()
.expect("crate::generate should have set the bin_name");

w!(
write!(
buf,
format!(
"#compdef {name}
"#compdef {name}
autoload -U is-at-least
Expand All @@ -49,13 +48,12 @@ else
compdef _{name} {name}
fi
",
name = bin_name,
initial_args = get_args_of(cmd, None),
subcommands = get_subcommands_of(cmd),
subcommand_details = subcommand_details(cmd)
)
.as_bytes()
);
name = bin_name,
initial_args = get_args_of(cmd, None),
subcommands = get_subcommands_of(cmd),
subcommand_details = subcommand_details(cmd)
)
.expect("failed to write completion file");
}
}

Expand Down

0 comments on commit 6842ed9

Please sign in to comment.