Skip to content

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

feat(completion): Add dynamic completions for all subcommands

Maximilian Roosbd3d1871cefd
diff --git a/src/shell.rs b/src/shell.rs
index f04b97a30..30d528a12 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -222,7 +222,10 @@ impl Shell {
/// Generate dynamic completion registrations for Fish shell by introspecting CLI structure
///
-/// This generates completion registrations for all subcommands with positional arguments.
+/// This generates completion registrations for all subcommands:
+/// - Subcommands with positional arguments get dynamic completions via `__wt_complete`
+/// - Subcommands without positional arguments get `-f` to prevent file completion fallback
+///
/// 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.
///
@@ -244,22 +247,22 @@ fn generate_fish_dynamic_completions(cli_cmd: &clap::Command, cmd_prefix: &str)
// Check if this subcommand has positional arguments
let has_positional = subcmd.get_positionals().next().is_some();
- if has_positional {
- // Generate condition based on command chain
- let condition = if current_chain.len() == 1 {
- // Top-level: just check for the subcommand
- format!("__fish_seen_subcommand_from {}", name)
- } else {
- // Nested: check for parent chain in sequence
- // e.g., for "dev run-hook": "__fish_seen_subcommand_from dev; and __fish_seen_subcommand_from run-hook"
- current_chain
- .iter()
- .map(|part| format!("__fish_seen_subcommand_from {}", part))
- .collect::<Vec<_>>()
- .join("; and ")
- };
+ // Generate condition based on command chain
+ let condition = if current_chain.len() == 1 {
+ // Top-level: just check for the subcommand
+ format!("__fish_seen_subcommand_from {}", name)
+ } else {
+ // Nested: check for parent chain in sequence
+ // e.g., for "beta commit": "__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from commit"
+ current_chain
+ .iter()
+ .map(|part| format!("__fish_seen_subcommand_from {}", part))
+ .collect::<Vec<_>>()
+ .join("; and ")
+ };
- // Get description from the first positional argument's help text
+ if has_positional {
+ // Subcommand with positional arguments - register dynamic completion
let desc = subcmd
.get_positionals()
.next()
@@ -271,6 +274,12 @@ fn generate_fish_dynamic_completions(cli_cmd: &clap::Command, cmd_prefix: &str)
" complete -c {} -n '{}' -f -a '(__{}_complete)' -d '{}'",
cmd_prefix, condition, cmd_prefix, desc
));
+ } else {
+ // Subcommand without positional arguments - prevent file completion fallback
+ completions.push(format!(
+ " complete -c {} -n '{}' -f",
+ cmd_prefix, condition
+ ));
}
// Recurse into nested subcommands
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index a955a0be1..f4466b02f 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -329,6 +329,29 @@ fn test_complete_unknown_command() {
});
}
+#[test]
+fn test_complete_beta_commit_no_positionals() {
+ let repo = TestRepo::new();
+ repo.commit("initial");
+ let mut settings = Settings::clone_current();
+ settings.set_snapshot_path("../snapshots");
+
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ repo.clean_cli_env(&mut cmd);
+ cmd.current_dir(repo.root_path())
+ .args(["complete", "wt", "beta", "commit", ""]);
+
+ assert_cmd_snapshot!(cmd, @r"
+ success: true
+ exit_code: 0
+ ----- stdout -----
+
+ ----- stderr -----
+ ");
+ });
+}
+
#[test]
fn test_complete_list_command() {
let repo = TestRepo::new();
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index c5b56ffbb..31663d788 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpfehyR7/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp3NkPx3/test-config.toml
---
success: true
exit_code: 0
@@ -158,10 +158,21 @@ if type -q wt; or set -q WORKTRUNK_BIN
# Register dynamic completions
complete -c wt -n '__fish_seen_subcommand_from init' -f -a '(__wt_complete)' -d 'Shell to generate code for'
+ complete -c wt -n '__fish_seen_subcommand_from config' -f
+ complete -c wt -n '__fish_seen_subcommand_from config; and __fish_seen_subcommand_from init' -f
+ complete -c wt -n '__fish_seen_subcommand_from config; and __fish_seen_subcommand_from list' -f
+ complete -c wt -n '__fish_seen_subcommand_from config; and __fish_seen_subcommand_from refresh-cache' -f
+ complete -c wt -n '__fish_seen_subcommand_from config; and __fish_seen_subcommand_from shell' -f
+ complete -c wt -n '__fish_seen_subcommand_from beta' -f
complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from run-hook' -f -a '(__wt_complete)' -d 'Hook type to run'
+ complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from commit' -f
complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from squash' -f -a '(__wt_complete)' -d 'Target branch to squash against (defaults to default branch)'
complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from push' -f -a '(__wt_complete)' -d 'Target branch (defaults to default branch)'
complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from rebase' -f -a '(__wt_complete)' -d 'Target branch to rebase onto (defaults to default branch)'
+ complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from ask-approvals' -f
+ complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from clear-approvals' -f
+ complete -c wt -n '__fish_seen_subcommand_from beta; and __fish_seen_subcommand_from select' -f
+ complete -c wt -n '__fish_seen_subcommand_from list' -f
complete -c wt -n '__fish_seen_subcommand_from switch' -f -a '(__wt_complete)' -d 'Branch name, worktree path, '@' for current HEAD, or '-' for previous branch'
complete -c wt -n '__fish_seen_subcommand_from remove' -f -a '(__wt_complete)' -d 'Worktree names or branches to remove (use '@' for current, defaults to current if none specified)'
complete -c wt -n '__fish_seen_subcommand_from merge' -f -a '(__wt_complete)' -d 'Target branch to merge into (defaults to default branch)'