Skip to content

Commit

Permalink
fix: Nested subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Aug 19, 2023
1 parent 60d8b3f commit 7aa4801
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 20 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ Things to do:
- Port darwin and degroff parsers
- Test the type 4 parser
- Find samples of type 4, Darwin, and Degroff to test
- Ensure nested subcommands work
- Add .gz files to the tests folder
- Test excluding/including commands and directories
- Figure out why fish only seems to use man1, man6, and man8
7 changes: 1 addition & 6 deletions src/gen/nu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ fn generate_cmd(
out.writeln("]");

for sub_cmd in &cmd.subcommands {
generate_cmd(
&format!("{} {}", cmd.name, sub_cmd.name),
sub_cmd,
out,
false,
);
generate_cmd(&format!("{cmd_name} {}", sub_cmd.name), sub_cmd, out, false);
}
}
15 changes: 4 additions & 11 deletions src/gen/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,18 @@ pub fn generate(cmd: &CommandInfo, out_dir: &Path) -> Result<()> {
let comp_name = format!("_{}", cmd.name);
let mut res = Output::new(String::from("\t"));
res.writeln(format!("#compdef {comp_name} {}", cmd.name));
generate_fn(cmd, &mut res, 0, &comp_name);
generate_fn(cmd, &mut res, &comp_name);
fs::write(out_dir.join(format!("{comp_name}.zsh")), res.text())?;
Ok(())
}

/// Generate a completion function for a command/subcommand
///
/// ## Arguments
/// * `pos` - If this is a top-level command, 0. Otherwise, if this is a
/// subcommand, which argument number the subcommand is (how deep it is)
/// * `fn` - What to name the completion function. If you have a command `foo`
/// with subcommand `bar`, the completion function for `foo bar` would be
/// named `_foo_bar`
fn generate_fn(cmd: &CommandInfo, out: &mut Output, pos: usize, fn_name: &str) {
fn generate_fn(cmd: &CommandInfo, out: &mut Output, fn_name: &str) {
out.writeln("");
out.writeln(format!("function {fn_name} {{"));
out.indent();
Expand Down Expand Up @@ -97,7 +95,7 @@ fn generate_fn(cmd: &CommandInfo, out: &mut Output, pos: usize, fn_name: &str) {
out.writeln("'*::arg:->args'");
out.dedent();

out.writeln(format!("case $line[{}] in", pos + 1));
out.writeln("case $line[1] in");
out.indent();
for sub_cmd in &cmd.subcommands {
out.writeln(format!("{}) {fn_name}_{};;", sub_cmd.name, sub_cmd.name));
Expand All @@ -110,11 +108,6 @@ fn generate_fn(cmd: &CommandInfo, out: &mut Output, pos: usize, fn_name: &str) {
out.writeln("}");

for sub_cmd in &cmd.subcommands {
generate_fn(
sub_cmd,
out,
pos + 1,
&format!("{fn_name}_{}", sub_cmd.name),
);
generate_fn(sub_cmd, out, &format!("{fn_name}_{}", sub_cmd.name));
}
}
11 changes: 10 additions & 1 deletion tests/resources/expected/_test1.bash

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion tests/resources/expected/_test1.zsh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/resources/expected/test1.nu

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/resources/in/man1/test1-sub1-nested.1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7aa4801

Please sign in to comment.