worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Support fuzzy shell completion for `switch`

Open benjaminbauer opened this issue 8 months ago • 5 comments

Problem

Fish shell supports substring matching for completions — typing auth<TAB> matches feature/user-auth. However, wt switch completions only support prefix matching because the binary filters candidates before returning them to the shell.

git checkout handles this correctly because git's fish completions return all branch candidates and let fish apply its own matching.

[!NOTE] To me this is different from #915 (fuzzy matching in the command itself) — this is about shell tab completion candidates being pre-filtered, preventing the shell's own substring matching from working.

[!NOTE] The below is vibed with love and tested

Reproducer

# Setup
git init /tmp/wt-repro && cd /tmp/wt-repro
git commit --allow-empty -m "init"
git branch feature/user-auth
git branch bugfix/user-auth-timeout
git branch release/2024-q1

# All candidates with empty token — works:
COMPLETE=fish command wt -- wt switch ''
# main                      + now
# feature/user-auth         / now
# bugfix/user-auth-timeout  / now
# release/2024-q1           / now

# Prefix "auth" — returns nothing:
COMPLETE=fish command wt -- wt switch auth
# (empty)

# Prefix "2024" — returns nothing:
COMPLETE=fish command wt -- wt switch 2024
# (empty)

# Compare: git lets fish do substring matching:
fish -c 'complete -C "git checkout auth"'
# feature/user-auth          Local Branch
# bugfix/user-auth-timeout   Local Branch

Root cause

The BranchCompleter (and clap's ValueCompleter::complete()) filters candidates by prefix before returning them to the shell. Fish never sees the non-matching candidates, so it can't apply its own substring matching.

Workaround

In wt.fish, replacing (commandline --current-token) with '' so the binary always returns all candidates and fish handles the filtering:

# Before (binary filters):
complete --keep-order --exclusive --command wt \
  --arguments "(... COMPLETE=fish $WORKTRUNK_BIN -- \
  (commandline --current-process --tokenize --cut-at-cursor) \
  (commandline --current-token))"

# After (fish filters):
complete --keep-order --exclusive --command wt \
  --arguments "(... COMPLETE=fish $WORKTRUNK_BIN -- \
  (commandline --current-process --tokenize --cut-at-cursor) '')"

Proposed fixes

Option A: Skip prefix filtering in COMPLETE mode

When the binary runs in completion mode (COMPLETE=fish/zsh/bash), return all candidates for positional arguments (branches, worktree names) instead of prefix-filtering them. The shell already applies its own matching to the returned candidates — prefix filtering in the binary just prevents the shell from using substring/fuzzy matching. Flag and subcommand completions can keep prefix filtering since shells expect exact prefix matching there anyway. This is the more robust fix — it would also benefit zsh's fuzzy matching and any future shell integrations.

Option B: Change the fish completion script only

Pass an empty string as the current token to the binary, so it returns all candidates and fish applies its own substring matching. This is the minimal change (single character in wt.fish) and works well in practice, but means the binary still does redundant prefix filtering for other shells, and each shell's completion script needs the same workaround independently.

benjaminbauer avatar Mar 12 '26 17:03 benjaminbauer

Thanks for the detailed report and analysis! I confirmed that the behavior you describe matches the code: BranchCompleter::complete() filters candidates with starts_with() before returning them to the shell, which prevents fish's substring matching from working.

The same prefix-filtering pattern is also used by HookCommandCompleter (line 196), so a fix targeting the completers would benefit both.

Your Option A (skip prefix filtering in COMPLETE mode for positional args) seems like the more robust approach — it would benefit all shells and keep the filtering responsibility where it belongs (in the shell). The COMPLETE env var is already available in maybe_handle_env_completion(), so the completers could check that to decide whether to skip prefix filtering.

I'll leave it for a maintainer to evaluate and prioritize.

worktrunk-bot avatar Mar 12 '26 17:03 worktrunk-bot

thanks you for the suggestion @benjaminbauer ! I hadn't realized this, v helpful to point it out

We just merged this to avoid showing "2000 options available", at least in zsh & bash.

@worktrunk-bot

  • do bash & zsh also benefit from us avoiding prefix-matching?
  • With prefix-matching, could we show remote branches which do match, even if there are hundreds of options which don't?
  • what do other tools do?

max-sixty avatar Mar 12 '26 19:03 max-sixty

Good questions — I dug into each one:

Do bash & zsh also benefit from skipping prefix filtering?

Zsh: yes. Zsh supports fuzzy/substring matching via its matcher-list zstyle (e.g., zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' 'l:|=* r:|=*'). Many users configure this. Like fish, zsh can only apply its matching to candidates it actually receives — prefix-filtering in the binary prevents it from working. So Option A would benefit zsh users who have fuzzy matching configured.

Bash: marginal. Bash's readline completion is strictly prefix-based by default. There's no built-in substring matching. Some users configure show-all-if-ambiguous or use fzf-based completion overrides, but standard bash completions are prefix-only. So bash wouldn't benefit much from this change in practice — but it also wouldn't be harmed by it.

With prefix matching, could we show remote branches that do match, even with hundreds of non-matching options?

Yes — and this is a nice insight. Right now the >100 threshold from #1442 applies before prefix filtering, so remote branches are excluded even if only a few would match the typed prefix. The flow is:

  1. complete_branches() gets all branches, sees >100 total, drops all remote-only branches
  2. BranchCompleter::complete() then prefix-filters the remaining (local + worktree) branches

If we moved to Option A (return all candidates, let the shell filter), we'd lose the ability to do this — the binary wouldn't know what the user has typed, so it can't selectively include matching remotes.

But there's a middle ground: keep the >100 remote exclusion as a pre-filter on the full candidate set, but after that, skip prefix filtering and return all surviving candidates to the shell. This preserves the "don't overwhelm with 2000 remote branches" behavior from #1442 while letting shells apply their own matching. The shells would see local branches + worktree branches (typically well under 100), which is fine for substring/fuzzy matching.

If we wanted to go further and include matching remote branches even when the total exceeds 100, that would require the binary to know the current token — which conflicts with Option A. That said, this seems like a niche scenario: users rarely tab-complete remote-only branch names (they'd typically use wt switch interactive picker or type the full name). So the simpler approach — skip prefix filtering but keep the remote exclusion threshold — seems like the right balance.

What do other tools do?

  • git — Git's fish completions (__fish_git_branches) return all branch candidates and let fish filter. Same for zsh (_git) and bash (__git_complete_refs). The key difference: git's completion is implemented in the shell scripts themselves (calling git for-each-ref directly), not via a binary filter. So the shell always has full control over matching.

  • cargo — Uses clap_complete's dynamic completion (same as worktrunk). Cargo's completers also prefix-filter before returning candidates. So cargo has the same limitation.

  • gh (GitHub CLI) — Uses Cobra's completion system, which returns all candidates and lets the shell filter. Similar approach to git.

  • docker — Returns all candidates, shell handles filtering.

The pattern among tools with many completable values (git, gh, docker) is to return all candidates and let the shell handle matching. Tools using clap_complete (cargo, worktrunk) tend to prefix-filter because clap's ValueCompleter trait passes current: &OsStr and examples show filtering with it — but clap doesn't require filtering.

Summary

Option A (skip prefix filtering for positional args in COMPLETE mode) seems like the right approach. It aligns with how git/gh/docker work, benefits both fish and zsh, and the >100 remote exclusion from #1442 can stay as-is — it's a pre-filter to avoid overwhelming the shell, independent of prefix matching.

worktrunk-bot avatar Mar 12 '26 19:03 worktrunk-bot

But there's a middle ground: keep the >100 remote exclusion as a pre-filter on the full candidate set, but after that, skip prefix filtering and return all surviving candidates to the shell. This preserves the "don't overwhelm with 2000 remote branches" behavior from #1442 while letting shells apply their own matching. The shells would see local branches + worktree branches (typically well under 100), which is fine for substring/fuzzy matching.

ok great, let's draft this change

max-sixty avatar Mar 12 '26 21:03 max-sixty

Drafted in #1471 — the change removes prefix filtering from BranchCompleter::complete() so all branch candidates (after the >100 remote exclusion from #1442) are returned to the shell. Fish and zsh can then apply their native substring/fuzzy matching.

The change is small — just the BranchCompleter impl in src/completion.rs. HookCommandCompleter keeps its prefix filtering since those are subcommand-style completions where prefix matching is expected.

CI is green on all three platforms.

worktrunk-bot avatar Mar 12 '26 21:03 worktrunk-bot