Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(completion): Implement filtering for branch completions
Maximilian Roose58173d2b116
diff --git a/src/commands/completion.rs b/src/commands/completion.rsindex 8dd783dbe..5c4b37c36 100644--- a/src/commands/completion.rs+++ b/src/commands/completion.rs@@ -313,12 +313,16 @@ fn detect_completion_target<'a>(args: &[String], cmd: &'a Command) -> Completion"switch" => {let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");if !has_create && should_complete_positional_arg(args, i, cur, cmd) {- return CompletionTarget::PositionalBranch(last.to_string());+ // If last arg is the subcommand itself, prefix is empty+ let prefix = if last == subcmd { "" } else { last };+ return CompletionTarget::PositionalBranch(prefix.to_string());}}"push" | "merge" | "remove" => {if should_complete_positional_arg(args, i, cur, cmd) {- return CompletionTarget::PositionalBranch(last.to_string());+ // If last arg is the subcommand itself, prefix is empty+ let prefix = if last == subcmd { "" } else { last };+ return CompletionTarget::PositionalBranch(prefix.to_string());}}_ => {}@@ -340,7 +344,9 @@ pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {// Complete with all branches (runtime-fetched values)let branches = get_branches_for_completion(|| Repository::current().all_branches());for branch in branches {- println!("{}", branch);+ if branch.starts_with(&prefix) {+ println!("{}", branch);+ }}} else {// Use the arg's declared possible_values (ValueEnum types)@@ -350,11 +356,13 @@ pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {}}}- CompletionTarget::PositionalBranch(_prefix) => {+ CompletionTarget::PositionalBranch(prefix) => {// Complete with all branches (runtime-fetched values)let branches = get_branches_for_completion(|| Repository::current().all_branches());for branch in branches {- println!("{}", branch);+ if branch.starts_with(&prefix) {+ println!("{}", branch);+ }}}CompletionTarget::Unknown => {diff --git a/src/commands/init.rs b/src/commands/init.rsindex 72cd94ad8..0f7bffe5d 100644--- a/src/commands/init.rs+++ b/src/commands/init.rs@@ -91,6 +91,15 @@ pub fn handle_init(line};+ // For Fish: Add -f (no file completion) to --base flag+ // The --base flag should only complete branches, not files+ let line = if matches!(shell, shell::Shell::Fish) && line.contains("-l base") {+ // Insert -f before -d (description) to disable file completion+ line.replace(" -d ", " -f -d ")+ } else {+ line+ };+// For Zsh: Guard the final compdef call to avoid errors when completion system isn't loaded// Not all users have compinit in their .zshrc, and --no-rcs mode never loads itlet line = if matches!(shell, shell::Shell::Zsh)diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex b93900bcf..2b98f48c4 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -242,8 +242,10 @@ fn test_complete_base_flag_with_equals() {let stdout = String::from_utf8_lossy(&output.stdout);let branches: Vec<&str> = stdout.lines().collect();- // Should show all branches (filtering happens in shell, not in our code)- assert!(branches.iter().any(|b| b.contains("main")));+ // Should only show branches that match the prefix "m"+ assert!(branches.iter().any(|b| b == &"main"));+ assert!(!branches.iter().any(|b| b.contains("develop")));+ assert!(!branches.iter().any(|b| b.contains("feature")));// Test completion for -b= format (short form with equals)let mut cmd = wt_command();@@ -412,8 +414,7 @@ fn test_complete_with_partial_prefix() {.output().unwrap();- // Complete with partial prefix - should return all branches- // (shell completion framework handles the prefix filtering)+ // Complete with partial prefix - should return only branches starting with "feat"let mut settings = Settings::clone_current();settings.set_snapshot_path("../snapshots");settings.bind(|| {@@ -427,8 +428,6 @@ fn test_complete_with_partial_prefix() {----- stdout -----feature/onefeature/two- hotfix/bug- main----- stderr -----");diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 31663d788..bf179fe7d 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: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp3NkPx3/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpgUNDX9/test-config.toml---success: trueexit_code: 0@@ -284,7 +284,7 @@ complete -c wt -n "__fish_wt_using_subcommand list" -l branches -d 'Include brancomplete -c wt -n "__fish_wt_using_subcommand list" -l full -d 'Show CI status, conflict detection, and complete diff statistics'complete -c wt -n "__fish_wt_using_subcommand list" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'complete -c wt -n "__fish_wt_using_subcommand list" -s h -l help -d 'Print help (see more with \'--help\')'-complete -c wt -n "__fish_wt_using_subcommand switch" -s b -l base -d 'Base branch to create from (only with --create). Use \'@\' for current HEAD' -r+complete -c wt -n "__fish_wt_using_subcommand switch" -s b -l base -f -d 'Base branch to create from (only with --create). Use \'@\' for current HEAD' -rcomplete -c wt -n "__fish_wt_using_subcommand switch" -s x -l execute -d 'Execute command after switching' -rcomplete -c wt -n "__fish_wt_using_subcommand switch" -s c -l create -d 'Create a new branch'complete -c wt -n "__fish_wt_using_subcommand switch" -s f -l force -d 'Auto-approve project commands without saving approvals'