|
| 1 | +--- |
| 2 | +applyTo: completions/gtr.bash, completions/_gtr, completions/gtr.fish |
| 3 | +--- |
| 4 | + |
| 5 | +# Completions Instructions |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Shell completions provide tab-completion for `gtr` commands, flags, branches, and adapter names across Bash, Zsh, and Fish shells. |
| 10 | + |
| 11 | +## When to Update Completions |
| 12 | + |
| 13 | +**Always update all three completion files** when: |
| 14 | + |
| 15 | +- Adding new commands (e.g., `gtr new-command`) |
| 16 | +- Adding new flags to existing commands (e.g., `--new-flag`) |
| 17 | +- Adding editor or AI adapters (completion must list available adapters) |
| 18 | +- Changing command names or flag names |
| 19 | + |
| 20 | +## File Responsibilities |
| 21 | + |
| 22 | +- **`completions/gtr.bash`** - Bash completion (requires bash-completion v2+) |
| 23 | +- **`completions/_gtr`** - Zsh completion (uses Zsh completion system) |
| 24 | +- **`completions/gtr.fish`** - Fish shell completion |
| 25 | + |
| 26 | +## Implementation Pattern |
| 27 | + |
| 28 | +Each completion file implements: |
| 29 | + |
| 30 | +1. **Command completion** - Top-level commands (`new`, `rm`, `open`, `ai`, `list`, etc.) |
| 31 | +2. **Flag completion** - Command-specific flags (e.g., `--from`, `--force`, `--editor`) |
| 32 | +3. **Branch completion** - Dynamic completion of existing worktree branches (via `gtr list --porcelain`) |
| 33 | +4. **Adapter completion** - Editor names (`cursor`, `vscode`, `zed`) and AI tool names (`aider`, `claude`, `codex`) |
| 34 | + |
| 35 | +## Testing Completions |
| 36 | + |
| 37 | +**Manual testing** (no automated tests): |
| 38 | + |
| 39 | +```bash |
| 40 | +# Bash - source the completion file |
| 41 | +source completions/gtr.bash |
| 42 | +gtr <TAB> # Should show commands |
| 43 | +gtr new <TAB> # Should show flags |
| 44 | +gtr open <TAB> # Should show branches |
| 45 | +gtr open --editor <TAB> # Should show editor names |
| 46 | + |
| 47 | +# Zsh - fpath must include completions directory |
| 48 | +fpath=(completions $fpath) |
| 49 | +autoload -U compinit && compinit |
| 50 | +gtr <TAB> |
| 51 | + |
| 52 | +# Fish - symlink to ~/.config/fish/completions/ |
| 53 | +ln -s "$(pwd)/completions/gtr.fish" ~/.config/fish/completions/ |
| 54 | +gtr <TAB> |
| 55 | +``` |
| 56 | + |
| 57 | +## Branch Completion Logic |
| 58 | + |
| 59 | +All three completions dynamically fetch current worktree branches: |
| 60 | + |
| 61 | +- Parse output of `gtr list --porcelain` (tab-separated: `path\tbranch\tstatus`) |
| 62 | +- Extract branch column (second field) |
| 63 | +- Exclude the special ID `1` (main repo) if needed |
| 64 | + |
| 65 | +## Adapter Name Updates |
| 66 | + |
| 67 | +When adding an editor or AI adapter: |
| 68 | + |
| 69 | +**Bash** (`completions/gtr.bash`): |
| 70 | + |
| 71 | +- Update `_gtr_editors` array or case statement |
| 72 | +- Update flag completion for `--editor` in `open` command |
| 73 | + |
| 74 | +**Zsh** (`completions/_gtr`): |
| 75 | + |
| 76 | +- Update `_arguments` completion specs for `--editor` or `--ai` |
| 77 | +- Use `_values` or `_alternative` for adapter names |
| 78 | + |
| 79 | +**Fish** (`completions/gtr.fish`): |
| 80 | + |
| 81 | +- Update `complete -c gtr` lines for editor/AI flags |
| 82 | +- List adapter names explicitly or parse from `gtr adapter` output |
| 83 | + |
| 84 | +## Keep in Sync |
| 85 | + |
| 86 | +The three completion files must stay synchronized: |
| 87 | + |
| 88 | +- Same commands supported |
| 89 | +- Same flags for each command |
| 90 | +- Same adapter names |
| 91 | +- Same branch completion behavior |
| 92 | + |
| 93 | +## Examples |
| 94 | + |
| 95 | +**Adding a new command `gtr status`**: |
| 96 | + |
| 97 | +1. Add `status` to main command list in all three files |
| 98 | +2. Add flag completion if the command has flags |
| 99 | +3. Test tab completion works |
| 100 | + |
| 101 | +**Adding a new editor `sublime`**: |
| 102 | + |
| 103 | +1. Create `adapters/editor/sublime.sh` with contract functions |
| 104 | +2. Add `sublime` to editor list in all three completion files |
| 105 | +3. Update help text in `bin/gtr` (`cmd_help` function) |
| 106 | +4. Update README with installation instructions |
| 107 | +5. Test `gtr open --editor s<TAB>` completes to `sublime` |
| 108 | + |
| 109 | +## Common Pitfalls |
| 110 | + |
| 111 | +- **Forgetting to update all three files** - Always update Bash, Zsh, AND Fish |
| 112 | +- **Hardcoding adapter names** - Keep adapter lists in sync with actual files in `adapters/{editor,ai}/` |
| 113 | +- **Not testing** - Source/reload completions and test with `<TAB>` key |
| 114 | +- **Case sensitivity** - Command and flag names must match exactly (case-sensitive) |
| 115 | + |
| 116 | +## Bash-Specific Notes |
| 117 | + |
| 118 | +- Requires `bash-completion` v2+ package |
| 119 | +- Use `COMPREPLY` array to return completions |
| 120 | +- Use `compgen` to filter based on current word (`$cur`) |
| 121 | +- Check `$COMP_CWORD` for argument position |
| 122 | + |
| 123 | +## Zsh-Specific Notes |
| 124 | + |
| 125 | +- Uses `_arguments` completion framework |
| 126 | +- Supports more sophisticated completion logic (descriptions, grouping) |
| 127 | +- Use `_describe` for simple lists, `_arguments` for complex commands |
| 128 | + |
| 129 | +## Fish-Specific Notes |
| 130 | + |
| 131 | +- Uses declarative `complete -c gtr` syntax |
| 132 | +- Conditions can check previous arguments with `__fish_seen_subcommand_from` |
| 133 | +- Can call external commands for dynamic completion |
0 commit comments