Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Feat: Generate dynamic Fish completions from CLI structure

Maximilian Roos80060b789d02
diff --git a/src/commands/init.rs b/src/commands/init.rs
index cc0846a1c..70a6ae86f 100644
--- a/src/commands/init.rs
+++ b/src/commands/init.rs
@@ -10,7 +10,7 @@ pub fn handle_init(shell_name: &str, cmd_name: &str, cli_cmd: &mut Command) -> R
// Generate shell integration code
let integration_output = init
- .generate()
+ .generate(cli_cmd)
.map_err(|e| format!("Failed to generate shell code: {}", e))?;
println!("{}", integration_output);
diff --git a/src/shell.rs b/src/shell.rs
index 80c0f8811..6c7ac9e16 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -253,6 +253,49 @@ impl Shell {
}
}
+/// Generate dynamic completion registrations for Fish shell by introspecting CLI structure
+///
+/// This generates completion registrations for all subcommands with positional arguments.
+/// The actual filtering of what completions to show is handled by the `wt complete` command,
+/// which checks the command context and only returns relevant completions.
+///
+/// This approach ensures that when new commands are added with positional arguments,
+/// they automatically get registered for completion - you only need to update the
+/// completion logic in `src/commands/completion.rs` to define what completions to show.
+fn generate_fish_dynamic_completions(cli_cmd: &clap::Command, cmd_prefix: &str) -> String {
+ cli_cmd
+ .get_subcommands()
+ .filter_map(|subcmd| {
+ let name = subcmd.get_name();
+
+ // Skip hidden commands (completion, complete)
+ if subcmd.is_hide_set() {
+ return None;
+ }
+
+ // Check if this subcommand has positional arguments
+ let has_positional = subcmd.get_positionals().next().is_some();
+ if !has_positional {
+ return None;
+ }
+
+ // Get description from the first positional argument's help text
+ let desc = subcmd
+ .get_positionals()
+ .next()
+ .and_then(|arg| arg.get_help())
+ .map(|h| h.to_string())
+ .unwrap_or_else(|| format!("{} argument", name));
+
+ Some(format!(
+ " complete -c {} -n '__fish_seen_subcommand_from {}' -f -a '(__{}_complete)' -d '{}'",
+ cmd_prefix, name, cmd_prefix, desc
+ ))
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+}
+
/// Shell integration configuration
pub struct ShellInit {
pub shell: Shell,
@@ -265,7 +308,7 @@ impl ShellInit {
}
/// Generate shell integration code
- pub fn generate(&self) -> Result<String, askama::Error> {
+ pub fn generate(&self, cli_cmd: &clap::Command) -> Result<String, askama::Error> {
match self.shell {
Shell::Bash | Shell::Zsh | Shell::Oil => {
let template = BashTemplate {
@@ -275,8 +318,11 @@ impl ShellInit {
template.render()
}
Shell::Fish => {
+ let dynamic_completions =
+ generate_fish_dynamic_completions(cli_cmd, &self.cmd_prefix);
let template = FishTemplate {
cmd_prefix: &self.cmd_prefix,
+ dynamic_completions,
};
template.render()
}
@@ -321,6 +367,7 @@ struct BashTemplate<'a> {
#[template(path = "fish.fish", escape = "none")]
struct FishTemplate<'a> {
cmd_prefix: &'a str,
+ dynamic_completions: String,
}
/// Nushell shell template
diff --git a/templates/fish.fish b/templates/fish.fish
index 64e0b723b..47808d58d 100644
--- a/templates/fish.fish
+++ b/templates/fish.fish
@@ -110,8 +110,5 @@ if type -q {{ cmd_prefix }}
end
# Register dynamic completions
- complete -c {{ cmd_prefix }} -n '__fish_seen_subcommand_from switch' -f -a '(__{{ cmd_prefix }}_complete)' -d 'Branch'
- complete -c {{ cmd_prefix }} -n '__fish_seen_subcommand_from push' -f -a '(__{{ cmd_prefix }}_complete)' -d 'Target branch'
- complete -c {{ cmd_prefix }} -n '__fish_seen_subcommand_from merge' -f -a '(__{{ cmd_prefix }}_complete)' -d 'Target branch'
- complete -c {{ cmd_prefix }} -n '__fish_seen_subcommand_from remove' -f -a '(__{{ cmd_prefix }}_complete)' -d 'Branch to remove'
+{{ dynamic_completions }}
end
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index c1b950720..6140870c4 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -131,10 +131,11 @@ if type -q wt
end
# Register dynamic completions
- complete -c wt -n '__fish_seen_subcommand_from switch' -f -a '(__wt_complete)' -d 'Branch'
- complete -c wt -n '__fish_seen_subcommand_from push' -f -a '(__wt_complete)' -d 'Target branch'
- complete -c wt -n '__fish_seen_subcommand_from merge' -f -a '(__wt_complete)' -d 'Target branch'
- complete -c wt -n '__fish_seen_subcommand_from remove' -f -a '(__wt_complete)' -d 'Branch to remove'
+ complete -c wt -n '__fish_seen_subcommand_from init' -f -a '(__wt_complete)' -d 'Shell to generate code for (bash, fish, zsh)'
+ complete -c wt -n '__fish_seen_subcommand_from switch' -f -a '(__wt_complete)' -d 'Branch name or worktree path'
+ complete -c wt -n '__fish_seen_subcommand_from remove' -f -a '(__wt_complete)' -d 'Worktree name or branch to remove (defaults to current worktree)'
+ complete -c wt -n '__fish_seen_subcommand_from push' -f -a '(__wt_complete)' -d 'Target branch (defaults to default branch)'
+ complete -c wt -n '__fish_seen_subcommand_from merge' -f -a '(__wt_complete)' -d 'Target branch to merge into (defaults to default branch)'
end
# Static completions (commands and flags)