Skip to content

Dynamic completion of worktree names for wt use, wt rm, and wt session rm #18

@ukanga

Description

@ukanga

What to build

Build on the static completion shipped in #17 by adding dynamic completion of worktree names at three specific positions:

  • wt use <TAB> — completes from the live worktree list.
  • wt rm <TAB> — same source as use.
  • wt session rm <TAB> — completes from worktrees attached to the active tmux session (panes mode) or tracked windows-mode sessions (~/.wt/sessions.json), respecting --mode if passed.

Mechanism: a hidden helper subcommand prints candidates one per line; each shell's completion script invokes the helper at the relevant positional positions.

End-to-end behaviour after this lands:

$ wt use <TAB><TAB>
feature/auth   feature/payments   bugfix/header

$ wt rm fea<TAB>
feature/auth   feature/payments

$ wt session rm <TAB>
feature/auth   feature/payments

Subcommand and flag completion from #17 continues to work unchanged; this issue only adds value-level completion for the three positionals named above.

Implementation notes

1. Hidden helper subcommand. Add a hidden wt __complete <kind> subcommand (#[command(hide = true)]) that prints completion candidates to stdout, one per line, with no decoration:

  • wt __complete worktrees — wraps the existing WorktreeManager::list_worktrees(), filters non-empty task_id (mirroring the existing filter used by the interactive picker), and prints task_id per line.
  • wt __complete session-worktrees [--mode panes|windows] — reads the active session state. For panes mode, lists worktrees with a window in the wt tmux session; for windows mode, reads from ~/.wt/sessions.json. When --mode is omitted, fall back to the resolved default from Config::load_for_repo.

The helper must be silent on errors (exit 0 with empty output) so that a transient failure (no git repo, no tmux, missing session file) never breaks the user's prompt.

2. Per-shell wrappers. For each shell, augment the static completion script from #17 so that the three positional positions delegate to the helper:

  • bash: in the _wt completion function, branch on the detected subcommand (use, rm, session rm) and call compgen -W "$(wt __complete worktrees 2>/dev/null)" -- "$cur" for that positional.
  • zsh: in the generated _wt function, replace the value spec for the affected positionals with ':worktree:_wt_worktrees', where _wt_worktrees is a small helper that runs wt __complete worktrees and feeds _describe.
  • fish: append complete -c wt -n "__fish_seen_subcommand_from use" -f -a "(wt __complete worktrees)" (and analogous lines for rm and session rm) to the generated fish file.
  • elvish: register a custom completer via edit:completion:arg-completer that delegates to the static map for known cases and calls the helper for the three target positions.

The implementer may choose to (a) post-process clap_complete output in Rust before emitting, or (b) emit a separate "dynamic addendum" file per shell that's sourced after the static one. Either is acceptable as long as #17's static behaviour is preserved and the acceptance criteria pass.

3. install.sh integration. No new install paths — the dynamic addendum lands in the same shell completion location as #17. If the addendum is a separate file/snippet, ensure idempotency on repeat runs (mirror the existing grep -q guards).

Acceptance criteria

  • wt __complete worktrees (hidden) prints one worktree name per line; output is empty (exit 0) when no worktrees exist or when run outside a git repo.
  • wt __complete session-worktrees [--mode panes|windows] (hidden) prints one worktree name per line for the resolved session mode; honours --mode override; exits 0 with empty output when no session exists.
  • Both helper subcommands are absent from wt --help and from wt completions <shell> output.
  • In a fresh bash shell, wt use <TAB> and wt rm <TAB> complete to the live worktree list. wt session rm <TAB> completes to the active session's worktrees.
  • Same end-to-end behaviour verified in fresh zsh, fish, and elvish shells.
  • Subcommand- and flag-level completion from Add shell auto-completion for the wt command #17 is unaffected (regression check: wt session --mode <TAB> still lists panes and windows).
  • Helper failures (e.g. tmux not installed for session-worktrees panes mode) do not produce visible error output during tab completion.
  • Integration test invokes the helper subcommand against a repository with two synthetic worktrees and asserts both names appear in stdout, one per line, sorted deterministically.
  • PR description records manual verification transcripts for each of the four shells.

Blocked by

Out of scope

  • Dynamic completion for wt new <name> (free-form; could complete from local branch names but warrants a separate discussion).
  • Completing base branches for -b <branch> from git branch output (separate follow-up).
  • PowerShell.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions