Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(completion): Enhance argument parsing for improved user experience
Maximilian Roos198d95b2b878
diff --git a/ISSUE.md b/ISSUE.mdnew file mode 100644index 000000000..783a5b3d3--- /dev/null+++ b/ISSUE.md@@ -0,0 +1,624 @@+# Shell Completion System: Refactoring and Remaining Optimization Opportunities++## Executive Summary++We successfully refactored the shell completion system to use clap introspection instead of manual string parsing, reducing the codebase by 305 lines (from 426 to 121 net lines). However, we've identified a remaining function (`should_complete_positional_arg`) that still uses manual parsing and has known bugs. This report documents the refactoring, the remaining issues, and options for further improvement.++## Background: Why Custom Completion?++The codebase uses a **custom completion implementation** rather than clap's `unstable-dynamic` feature for several reasons (from `src/commands/completion.rs:1-11`):++1. **unstable-dynamic is an unstable API** that may change between versions+2. **Need conditional completion logic** (e.g., don't complete branches when `--create` is present)+3. **Need runtime-fetched values** (git branches) with context-aware filtering+4. **Need precise control** over positional argument state tracking with flags++This approach uses stable clap APIs and handles edge cases that clap's completion system isn't designed for.++## What We've Accomplished++### Original Problem++Tab completion for `--base=<value>` format wasn't working:++```bash+wt switch --create --execute=claude --base=<tab> # No completion+wt switch --create --execute=claude --base <tab> # Worked fine+```++The code only handled space-separated format (`--base <value>`), not equals format (`--base=value`).++### Initial "Hacky" Solution++The original fix added equals-format detection to manual parsing logic:++```rust+// Check if we're completing a flag value+// Handle both formats: --base <value> and --base=<value>++// First, check if the last argument is --base=... or -b=...+if let Some(last_arg) = args.last()+ && (last_arg.starts_with("--base=") || last_arg.starts_with("-b="))+{+ return CompletionContext::BaseFlag;+}++// Then check if the previous argument was --base or -b (space-separated format)+if args.len() >= 3 {+ let prev_arg = &args[args.len() - 2];+ if prev_arg == "--base" || prev_arg == "-b" {+ return CompletionContext::BaseFlag;+ }+}+```++This worked but was **brittle and hardcoded**.++### Comprehensive Refactoring++We replaced the entire manual parsing system with clap-based introspection:++#### Before (Old Approach)++```rust+#[derive(Debug, PartialEq)]+enum CompletionContext {+ SwitchBranch, // Hardcoded for switch command+ PushTarget, // Hardcoded for push command+ MergeTarget, // Hardcoded for merge command+ RemoveBranch, // Hardcoded for remove command+ BaseFlag, // Hardcoded for --base flag+ Unknown,+}++fn parse_completion_context(args: &[String]) -> CompletionContext {+ // 70+ lines of manual parsing:+ // - Find subcommand by skipping global flags+ // - Check if last arg is --base=...+ // - Check if prev arg is --base+ // - Check subcommand name and return hardcoded enum variant+}++pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {+ let context = parse_completion_context(&args);++ match context {+ CompletionContext::SwitchBranch+ | CompletionContext::PushTarget+ | CompletionContext::MergeTarget+ | CompletionContext::RemoveBranch+ | CompletionContext::BaseFlag => {+ // Fetch and print branches+ }+ CompletionContext::Unknown => {+ // Use clap_fallback for ValueEnums+ }+ }+}+```++#### After (New Approach)++```rust+/// Represents what we're trying to complete+#[derive(Debug)]+enum CompletionTarget<'a> {+ /// Completing a value for an option flag (e.g., --base <value> or --base=<value>)+ Option(&'a Arg, String), // (clap Arg, prefix to complete)+ /// Completing a positional branch argument (switch/push/merge/remove commands)+ PositionalBranch(String), // prefix to complete+ /// No special completion needed+ Unknown,+}++fn detect_completion_target<'a>(args: &[String], cmd: &'a Command) -> CompletionTarget<'a> {+ // Walk command tree to find active subcommand+ let mut cur = cmd;+ let mut subcommand_name = None;+ // ... (walk logic)++ // Check for --arg=value format+ if let Some(equals_pos) = last.find('=') {+ let flag_part = &last[..equals_pos];+ let value_part = &last[equals_pos + 1..];++ if let Some(long) = flag_part.strip_prefix("--")+ && let Some(arg) = cur.get_opts().find(|a| a.get_long().is_some_and(|l| l == long))+ {+ return CompletionTarget::Option(arg, value_part.to_string());+ }+ // ... (short form check)+ }++ // Check for --arg value format (space-separated)+ if let Some(p) = prev {+ if let Some(long) = p.strip_prefix("--")+ && let Some(arg) = cur.get_opts().find(|a| a.get_long().is_some_and(|l| l == long))+ {+ return CompletionTarget::Option(arg, last.to_string());+ }+ // ... (short form check)+ }++ // Check positional branch arguments using subcommand name+ if let Some(subcmd) = subcommand_name {+ match subcmd {+ "switch" => {+ let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");+ if !has_create && should_complete_positional_arg(args, i) {+ return CompletionTarget::PositionalBranch(last.to_string());+ }+ }+ "push" | "merge" | "remove" => {+ if should_complete_positional_arg(args, i) {+ return CompletionTarget::PositionalBranch(last.to_string());+ }+ }+ _ => {}+ }+ }++ CompletionTarget::Unknown+}++pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {+ let mut cmd = crate::cli::Cli::command();+ cmd.build(); // Required for introspection++ let target = detect_completion_target(&args, &cmd);++ match target {+ CompletionTarget::Option(arg, prefix) => {+ // Check if this is the "base" option that needs branch completion+ if arg.get_long() == Some("base") {+ // Complete with all branches (runtime-fetched values)+ let branches = get_branches_for_completion(|| Repository::current().all_branches());+ for branch in branches {+ println!("{}", branch);+ }+ } else {+ // Use the arg's declared possible_values (ValueEnum types)+ let items = items_from_arg(arg, &prefix);+ if !items.is_empty() {+ print_items(items);+ }+ }+ }+ 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);+ }+ }+ CompletionTarget::Unknown => {+ // Check for positionals with ValueEnum possible_values+ // ... (handles init <Shell>, beta run-hook <HookType>)+ }+ }++ Ok(())+}+```++### Benefits of Refactoring++1. **Automatic `=` format support for ALL flags** - not just `--base`, but any future flags with values+2. **No hardcoded command names** - uses clap's knowledge of command structure+3. **Returns clap `Arg` objects** - can query metadata, possible_values, etc.+4. **More maintainable** - adding new flags with custom completion is easier+5. **Reduced code size** - 305 lines removed (426 → 121 net lines)++### Test Results++All tests pass after refactoring:++```bash+$ cargo test --test integration completion --quiet+running 29 tests+.............................+test result: ok. 29 passed; 0 failed; 0 ignored; 0 measured; 265 filtered out++$ cargo test --quiet 2>&1 | tail -5+test result: ok. 294 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out++$ pre-commit run --all-files+fix end of files.........................................................Passed+check yaml...............................................................Passed+mixed line ending........................................................Passed+trim trailing whitespace.................................................Passed+typos....................................................................Passed+Check for formatting with cargo fmt......................................Passed+Check for linting issues with cargo clippy...............................Passed+markdown-link-check......................................................Passed+no-dbg...................................................................Passed+```++## Remaining Issue: `should_complete_positional_arg`++### The Problem++One function still uses manual parsing and has **known bugs**:++```rust+/// Check if a positional argument should be completed+/// Returns true if we're still completing the first positional arg+/// Returns false if the positional arg has been provided and we've moved past it+fn should_complete_positional_arg(args: &[String], start_index: usize) -> bool {+ let mut i = start_index;++ while i < args.len() {+ let arg = &args[i];++ if arg == "--base" || arg == "-b" {+ // Skip flag and its value+ i += 2;+ } else if arg.starts_with("--") || (arg.starts_with('-') && arg.len() > 1) {+ // Skip other flags+ i += 1;+ } else if !arg.is_empty() {+ // Found a positional argument+ // Only continue completing if it's at the last position+ return i >= args.len() - 1;+ } else {+ // Empty string (cursor position)+ i += 1;+ }+ }++ // No positional arg found yet - should complete+ true+}+```++### Current Hardcoded Checks++```bash+$ rg '== "--' src/commands/completion.rs+if arg == "--base" || arg == "-b" {+ if tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose" {+ let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");+ if tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose" {+```++### Bug: Incomplete Flag Handling++The function hardcodes `--base` and `-b` but **misses `--execute`**, which also takes a value:++```rust+// From src/cli.rs - switch command flags that take values+base: Option<String>, // Detected ✅+execute: Option<String>, // MISSING ❌+```++**Reproduction scenario:**++```bash+wt switch --execute "code ." <tab>+# BUG: "code" is treated as the branch positional argument+# Should skip "code" because --execute takes a value+# Result: Might not complete branches correctly+```++### Additional Hardcoded Patterns++1. **Global flags** (lines 125, 247):+ ```rust+ if tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose"+ ```+ Could query `cmd.get_global_opts()` instead++2. **`--create` flag detection** (line 193):+ ```rust+ let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");+ ```+ Could check if `cur.get_opts().find(|a| a.get_long() == Some("create"))`++### Why This Function Exists++The function attempts to answer: **"Has the user already provided the positional argument?"**++This is needed because:+- Branch completion should only happen when completing the first positional+- Flags can appear anywhere: `wt switch --base main feature` or `wt switch feature --base main`+- Need to distinguish: `wt switch feat<tab>` (complete) vs `wt switch feature <tab>` (don't complete)++Example scenarios:++```bash+wt switch <tab> # should_complete = true+wt switch feat<tab> # should_complete = true (still completing first arg)+wt switch feature <tab> # should_complete = false (already provided branch)+wt switch --base main <tab> # should_complete = true (no branch yet)+wt switch --base main feat<tab> # should_complete = true (still completing branch)+wt switch --base main feature <tab> # should_complete = false (branch provided)+```++The challenge is **parsing without clap's state machine** - we need to manually skip flags and their values.++## Options for Further Improvement++### Option A: Use Clap Introspection for Flag Detection++Pass the clap `Command` to `should_complete_positional_arg` and query which flags take values:++```rust+fn should_complete_positional_arg(args: &[String], start_index: usize, cmd: &Command) -> bool {+ let mut i = start_index;++ while i < args.len() {+ let arg = &args[i];++ // Check if this is a flag that takes a value+ if let Some(stripped) = arg.strip_prefix("--") {+ // Long form+ if let Some(opt) = cmd.get_opts().find(|o| o.get_long() == Some(stripped)) {+ if opt.get_num_args().map_or(false, |n| n.max_values() > Some(0)) {+ // This flag takes a value, skip it+ i += 2;+ continue;+ }+ }+ i += 1;+ } else if arg.starts_with('-') && arg.len() > 1 {+ // Short form or combined flags+ // More complex: could be -abc (three flags) or -b value+ // Would need to check each character+ i += 1;+ } else if !arg.is_empty() {+ return i >= args.len() - 1;+ } else {+ i += 1;+ }+ }++ true+}+```++**Pros:**+- No hardcoded flag names+- Automatically handles new flags+- More robust++**Cons:**+- More complex+- Handling short flags is tricky (`-b value` vs `-abc`)+- Handling `--flag=value` format needs special care++### Option B: Simplify by Counting Non-Flag Args++Instead of trying to be precise about which flags take values, just count non-flag arguments:++```rust+fn should_complete_positional_arg(args: &[String], start_index: usize) -> bool {+ let mut positional_count = 0;++ for arg in &args[start_index..] {+ // Skip flags and their potential values+ if arg.starts_with('-') {+ continue;+ }++ // Skip empty strings (cursor position)+ if arg.is_empty() {+ continue;+ }++ // Count as positional+ positional_count += 1;+ }++ // We want to complete if we haven't found a positional yet,+ // or if the last arg is a partial positional (at cursor)+ positional_count <= 1+}+```++**Pros:**+- Much simpler+- No need to know which flags take values+- Handles `--flag=value` automatically++**Cons:**+- Less precise - might treat flag values as positionals+- Could have edge cases like: `wt switch --execute "my branch name" <tab>`+ (would count "my branch name" as a positional)++### Option C: Use Clap's Parser++The most robust approach would be to use clap's actual parser to understand what's been consumed:++```rust+use clap::Parser;++fn should_complete_positional_arg(args: &[String], cmd: &Command) -> bool {+ // Try parsing with clap up to the current position+ // Check if the positional has been filled+ // This would require more invasive changes to use clap's internal parser state+}+```++**Pros:**+- Most accurate+- Handles all edge cases+- Uses clap's own logic++**Cons:**+- Requires understanding clap's internal parser+- More invasive changes+- Might require partial parsing (non-standard use of clap)++### Option D: Keep Current Implementation, Document Limitations++Accept the current limitations and document them:++```rust+/// Check if a positional argument should be completed+///+/// Returns true if we're still completing the first positional arg+/// Returns false if the positional arg has been provided and we've moved past it+///+/// LIMITATIONS:+/// - Only handles --base/-b flag (missing --execute/-x)+/// - Doesn't handle --flag=value format+/// - Assumes flags with values are consumed as two args+fn should_complete_positional_arg(args: &[String], start_index: usize) -> bool {+ // Current implementation...+}+```++Add `--execute` to the hardcoded list:++```rust+if arg == "--base" || arg == "-b" || arg == "--execute" || arg == "-x" {+ // Skip flag and its value+ i += 2;+}+```++**Pros:**+- Minimal changes+- Keeps working code++**Cons:**+- Still brittle+- Requires manual updates for new flags+- Doesn't handle `--flag=value` format++## Open Questions and Research Needs++### 1. How do other shell completion systems handle this?++**Question:** How do bash-completion, zsh-completion, and fish-completion handle the problem of "has this positional been provided yet" when flags can appear anywhere?++**Research needed:**+- Look at bash-completion source code+- Check how git's completion handles: `git checkout <flags> branch` vs `git checkout branch <flags>`+- See if there are standard patterns for this++**Why it matters:** We might be reinventing the wheel. There could be established patterns.++### 2. Can we use clap's partial parsing?++**Question:** Does clap provide any APIs for partial parsing that could help here?++**Research needed:**+- Check clap documentation for partial parsing+- Look for examples of using clap for completion in other projects+- Check if clap_complete has utilities we're missing++**Why it matters:** We might be able to leverage clap's parser instead of manual parsing.++### 3. What completion behavior do users expect?++**Question:** What happens in practice when flags take values that look like branch names?++**Examples:**+```bash+wt switch --execute "checkout feature-branch" <tab>+# Should this complete? The string "checkout feature-branch" is a valid branch name+# But it's the value of --execute, not a positional+```++**Research needed:**+- Test how git completion handles: `git checkout -b "master" <tab>`+- Check bash-completion best practices+- Look at other CLI tools with similar patterns++**Why it matters:** Edge cases might be acceptable if they're rare in practice.++### 4. Performance impact of clap introspection?++**Question:** Does calling `cmd.get_opts()` and iterating on every keystroke have performance implications?++**Assumptions carrying load:**+- We assume clap introspection is fast enough for completion+- We assume calling `.build()` on every completion is acceptable++**Research needed:**+- Benchmark: `time wt complete wt switch --base <args>`+- Compare manual parsing vs clap introspection performance+- Check if we should cache the built command++**Why it matters:** Completion needs to be fast (<100ms ideally).++### 5. Should we handle `--flag=value` in positional detection?++**Question:** Does `should_complete_positional_arg` need to handle `--flag=value` format, or is the current behavior acceptable?++**Current behavior:**+```bash+wt switch --base=main feat<tab> # Works - completes branches+wt switch --execute="code ." feat<tab> # Might work? Need to test+```++**Research needed:**+- Test actual behavior with `--execute="value"` format+- Check if shells split `--flag=value` before passing to completion+- Verify if this is a real problem or theoretical++**Why it matters:** Might already work due to shell argument parsing.++## Recommendations++Based on the analysis, I recommend **Option A (Use Clap Introspection)** because:++1. **Consistency** - Matches the pattern we established in the refactoring+2. **Maintainability** - No hardcoded flag names+3. **Correctness** - Handles all flags that take values, not just `--base`++The implementation would:+- Query `cmd.get_opts()` to find flags that take values+- Handle `--flag=value` format by detecting `=` before checking+- Keep the existing logic for counting positionals++Before implementing, I suggest:+1. **Research** shell completion patterns (Question 1)+2. **Test** edge cases with `--execute` to confirm the bug (Question 5)+3. **Benchmark** to ensure performance is acceptable (Question 4)++## Code Context: Switch Command Definition++For reference, here's the `switch` command definition from `src/cli.rs:376-399`:++```rust+Switch {+ /// Branch name, worktree path, '@' for current HEAD, or '-' for previous branch+ branch: String,++ /// Create a new branch+ #[arg(short = 'c', long)]+ create: bool,++ /// Base branch to create from (only with --create). Use '@' for current HEAD+ #[arg(short = 'b', long)]+ base: Option<String>,++ /// Execute command after switching+ #[arg(short = 'x', long)]+ execute: Option<String>,++ /// Auto-approve project commands without saving approvals.+ #[arg(short = 'f', long)]+ force: bool,++ /// Skip all project hooks: post-create and post-start.+ #[arg(long)]+ no_verify: bool,+}+```++Flags that take values:+- `--base` / `-b` → `Option<String>` ✅ Currently handled+- `--execute` / `-x` → `Option<String>` ❌ **Missing from should_complete_positional_arg**++## Conclusion++The refactoring successfully eliminated 305 lines of brittle manual parsing by using clap introspection. The remaining `should_complete_positional_arg` function has a similar opportunity for improvement:++- **Current state:** Hardcoded checks for `--base`, missing `--execute`+- **Goal:** Use clap to query which flags take values+- **Benefit:** Automatic handling of all current and future flags++The key challenge is balancing **precision** (correctly identifying when a positional has been provided) with **simplicity** (not reimplementing clap's parser). Option A provides the best balance by using clap's metadata while keeping the existing positional-counting logic.diff --git a/src/commands/completion.rs b/src/commands/completion.rsindex 3254b3a83..00c8b7a21 100644--- a/src/commands/completion.rs+++ b/src/commands/completion.rs@@ -21,6 +21,17 @@ struct Item {help: Option<String>,}+/// Represents what we're trying to complete+#[derive(Debug)]+enum CompletionTarget<'a> {+ /// Completing a value for an option flag (e.g., `--base <value>` or `--base=<value>`)+ Option(&'a Arg, String), // (clap Arg, prefix to complete)+ /// Completing a positional branch argument (switch/push/merge/remove commands)+ PositionalBranch(String), // prefix to complete+ /// No special completion needed+ Unknown,+}+/// Print completion items in fish-friendly format (name\thelp)/// Other shells ignore the tab separator and just use the namefn print_items(items: impl IntoIterator<Item = Item>) {@@ -33,16 +44,6 @@ fn print_items(items: impl IntoIterator<Item = Item>) {}}-#[derive(Debug, PartialEq)]-enum CompletionContext {- SwitchBranch,- PushTarget,- MergeTarget,- RemoveBranch,- BaseFlag,- Unknown,-}-/// Check if a positional argument should be completed/// Returns true if we're still completing the first positional arg/// Returns false if the positional arg has been provided and we've moved past it@@ -72,79 +73,6 @@ fn should_complete_positional_arg(args: &[String], start_index: usize) -> bool {true}-/// Find the subcommand position by skipping global flags-///-/// Note: `--source` is handled by the shell wrapper (templates/*.sh) and stripped before-/// reaching the main Rust binary, but the completion function passes COMP_WORDS directly-/// to `wt complete`, so completion sees the raw command line with `--source` still present.-fn find_subcommand_index(args: &[String]) -> Option<usize> {- let mut i = 1; // Start after "wt"- while i < args.len() {- let arg = &args[i];- // Skip global flags (--source is shell-only, others are defined in cli.rs)- if arg == "--source" || arg == "--internal" || arg == "-v" || arg == "--verbose" {- i += 1;- } else if !arg.starts_with('-') {- // Found the subcommand- return Some(i);- } else {- // Unknown flag, stop searching (fail-safe behavior)- return None;- }- }- None-}--fn parse_completion_context(args: &[String]) -> CompletionContext {- // args format: ["wt", "switch", "partial"]- // or: ["wt", "--source", "switch", "partial"]- // or: ["wt", "switch", "--create", "new", "--base", "partial"]- // or: ["wt", "beta", "run-hook", "partial"]-- if args.len() < 2 {- return CompletionContext::Unknown;- }-- let subcommand_index = match find_subcommand_index(args) {- Some(idx) => idx,- None => return CompletionContext::Unknown,- };-- let subcommand = &args[subcommand_index];-- // Check if the previous argument was a flag that expects a value- // If so, we're completing that flag's value- if args.len() >= 3 {- let prev_arg = &args[args.len() - 2];- if prev_arg == "--base" || prev_arg == "-b" {- return CompletionContext::BaseFlag;- }- }-- // Special handling for switch --create: don't complete new branch names- if subcommand == "switch" {- let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");- if has_create {- return CompletionContext::Unknown;- }- }-- // For commands with positional branch arguments, check if we should complete- let context = match subcommand.as_str() {- "switch" => CompletionContext::SwitchBranch,- "push" => CompletionContext::PushTarget,- "merge" => CompletionContext::MergeTarget,- "remove" => CompletionContext::RemoveBranch,- _ => return CompletionContext::Unknown,- };-- if should_complete_positional_arg(args, subcommand_index + 1) {- context- } else {- CompletionContext::Unknown- }-}-fn get_branches_for_completion<F>(get_branches_fn: F) -> Vec<String>whereF: FnOnce() -> Result<Vec<String>, GitError>,@@ -180,24 +108,26 @@ fn items_from_arg(arg: &Arg, prefix: &str) -> Vec<Item> {.collect()}-/// Generic fallback that reflects on clap's Command tree to find possible values-/// This automatically handles all ValueEnum types without manual registration-fn clap_fallback(args: &[String]) -> Vec<Item> {- // args look like: ["wt", "<subcmds...>", "<partial or prev>", [partial]?]- let mut cmd = crate::cli::Cli::command();- cmd.build(); // Required for introspection (completions/help)+/// Detect what we're trying to complete using clap introspection+/// Handles both --arg value and --arg=value formats+fn detect_completion_target<'a>(args: &[String], cmd: &'a Command) -> CompletionTarget<'a> {+ if args.len() < 2 {+ return CompletionTarget::Unknown;+ }// Find the active subcommand frame by walking the command treelet mut i = 1; // Skip binary name- let mut cur: &Command = &cmd;+ let mut cur = cmd;+ let mut subcommand_name = None;while i < args.len() {let tok = &args[i];- // Skip global flags (same as find_subcommand_index)+ // Skip global flagsif tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose" {i += 1;continue;}if let Some(sc) = cur.find_subcommand(tok) {+ subcommand_name = Some(sc.get_name());cur = sc;i += 1;} else {@@ -205,17 +135,35 @@ fn clap_fallback(args: &[String]) -> Vec<Item> {}}- // Use the last two args to determine what we're completing- // If we consumed all args as subcommands, we're completing a positional (empty prefix)- // Otherwise, the last arg is the partial completion text- let last = if i >= args.len() {- ""- } else {- args.last().map(String::as_str).unwrap_or("")- };+ let last = args.last().map(String::as_str).unwrap_or("");let prev = args.iter().rev().nth(1).map(|s| s.as_str());- // 1) If we are completing a value for an option (prev was a flag), use that option's possible values+ // Check for --arg=value format in last argument+ if let Some(equals_pos) = last.find('=') {+ let flag_part = &last[..equals_pos];+ let value_part = &last[equals_pos + 1..];++ // Long form: --name=value+ if let Some(long) = flag_part.strip_prefix("--")+ && let Some(arg) = cur+ .get_opts()+ .find(|a| a.get_long().is_some_and(|l| l == long))+ {+ return CompletionTarget::Option(arg, value_part.to_string());+ }++ // Short form: -n=value+ if let Some(short) = flag_part+ .strip_prefix('-')+ .filter(|s| s.len() == 1)+ .and_then(|s| s.chars().next())+ && let Some(arg) = cur.get_opts().find(|a| a.get_short() == Some(short))+ {+ return CompletionTarget::Option(arg, value_part.to_string());+ }+ }++ // Check for --arg value format (space-separated)if let Some(p) = prev {// Long form: --nameif let Some(long) = p.strip_prefix("--")@@ -223,8 +171,9 @@ fn clap_fallback(args: &[String]) -> Vec<Item> {.get_opts().find(|a| a.get_long().is_some_and(|l| l == long)){- return items_from_arg(arg, last);+ return CompletionTarget::Option(arg, last.to_string());}+// Short form: -nif let Some(short) = p.strip_prefix('-')@@ -232,349 +181,95 @@ fn clap_fallback(args: &[String]) -> Vec<Item> {.and_then(|s| s.chars().next())&& let Some(arg) = cur.get_opts().find(|a| a.get_short() == Some(short)){- return items_from_arg(arg, last);+ return CompletionTarget::Option(arg, last.to_string());}}- // 2) Otherwise, we're likely on a positional; try the "next" positional that has fixed values- // Heuristic: first positional with possible_values- if let Some(arg) = cur- .get_positionals()- .find(|a| !a.get_possible_values().is_empty())- {- return items_from_arg(arg, last);+ // Check if we're completing a positional branch argument+ // Special handling for switch --create: don't complete when creating new branches+ if let Some(subcmd) = subcommand_name {+ match subcmd {+ "switch" => {+ let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");+ if !has_create && should_complete_positional_arg(args, i) {+ return CompletionTarget::PositionalBranch(last.to_string());+ }+ }+ "push" | "merge" | "remove" => {+ if should_complete_positional_arg(args, i) {+ return CompletionTarget::PositionalBranch(last.to_string());+ }+ }+ _ => {}+ }}- // 3) Nothing to contribute- Vec::new()+ CompletionTarget::Unknown}pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {- let context = parse_completion_context(&args);-- match context {- CompletionContext::SwitchBranch- | CompletionContext::PushTarget- | CompletionContext::MergeTarget- | CompletionContext::RemoveBranch- | CompletionContext::BaseFlag => {+ let mut cmd = crate::cli::Cli::command();+ cmd.build(); // Required for introspection++ let target = detect_completion_target(&args, &cmd);++ match target {+ CompletionTarget::Option(arg, prefix) => {+ // Check if this is the "base" option that needs branch completion+ if arg.get_long() == Some("base") {+ // Complete with all branches (runtime-fetched values)+ let branches = get_branches_for_completion(|| Repository::current().all_branches());+ for branch in branches {+ println!("{}", branch);+ }+ } else {+ // Use the arg's declared possible_values (ValueEnum types)+ let items = items_from_arg(arg, &prefix);+ if !items.is_empty() {+ print_items(items);+ }+ }+ }+ 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);}}- CompletionContext::Unknown => {- // Use clap's reflection to find possible values for ValueEnum types- // This automatically handles init <Shell>, config shell --shell <Shell>,- // and any future ValueEnum arguments without manual registration- let items = clap_fallback(&args);- if !items.is_empty() {- print_items(items);+ CompletionTarget::Unknown => {+ // Check for positionals with ValueEnum possible_values (e.g., init <Shell>, beta run-hook <HookType>)+ // Walk the command tree to find the active subcommand+ let mut i = 1;+ let mut cur = &cmd;+ while i < args.len() {+ let tok = &args[i];+ if tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose" {+ i += 1;+ continue;+ }+ if let Some(sc) = cur.find_subcommand(tok) {+ cur = sc;+ i += 1;+ } else {+ break;+ }+ }++ let last = args.last().map(String::as_str).unwrap_or("");++ // Check if there's a positional with possible_values+ if let Some(arg) = cur+ .get_positionals()+ .find(|a| !a.get_possible_values().is_empty())+ {+ let items = items_from_arg(arg, last);+ if !items.is_empty() {+ print_items(items);+ }}- // else: intentionally print nothing}}Ok(())}--#[cfg(test)]-mod tests {- use super::*;-- #[test]- fn test_find_subcommand_index() {- let args = vec!["wt".to_string(), "switch".to_string()];- assert_eq!(find_subcommand_index(&args), Some(1));- }-- #[test]- fn test_find_subcommand_index_with_source() {- let args = vec![- "wt".to_string(),- "--source".to_string(),- "switch".to_string(),- ];- assert_eq!(find_subcommand_index(&args), Some(2));- }-- #[test]- fn test_find_subcommand_index_with_verbose() {- let args = vec!["wt".to_string(), "-v".to_string(), "switch".to_string()];- assert_eq!(find_subcommand_index(&args), Some(2));- }-- #[test]- fn test_find_subcommand_index_with_multiple_flags() {- let args = vec![- "wt".to_string(),- "--source".to_string(),- "-v".to_string(),- "switch".to_string(),- ];- assert_eq!(find_subcommand_index(&args), Some(3));- }-- #[test]- fn test_find_subcommand_index_no_subcommand() {- let args = vec!["wt".to_string()];- assert_eq!(find_subcommand_index(&args), None);- }-- #[test]- fn test_parse_completion_context_switch() {- let args = vec!["wt".to_string(), "switch".to_string(), "feat".to_string()];- assert_eq!(- parse_completion_context(&args),- CompletionContext::SwitchBranch- );- }-- #[test]- fn test_parse_completion_context_switch_with_source() {- let args = vec![- "wt".to_string(),- "--source".to_string(),- "switch".to_string(),- "feat".to_string(),- ];- assert_eq!(- parse_completion_context(&args),- CompletionContext::SwitchBranch- );- }-- #[test]- fn test_parse_completion_context_push() {- let args = vec!["wt".to_string(), "push".to_string(), "ma".to_string()];- assert_eq!(- parse_completion_context(&args),- CompletionContext::PushTarget- );- }-- #[test]- fn test_parse_completion_context_merge() {- let args = vec!["wt".to_string(), "merge".to_string(), "de".to_string()];- assert_eq!(- parse_completion_context(&args),- CompletionContext::MergeTarget- );- }-- #[test]- fn test_parse_completion_context_remove() {- let args = vec!["wt".to_string(), "remove".to_string(), "feat".to_string()];- assert_eq!(- parse_completion_context(&args),- CompletionContext::RemoveBranch- );- }-- #[test]- fn test_parse_completion_context_base_flag() {- let args = vec![- "wt".to_string(),- "switch".to_string(),- "--create".to_string(),- "new".to_string(),- "--base".to_string(),- "dev".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);- }-- #[test]- fn test_parse_completion_context_unknown() {- let args = vec!["wt".to_string()];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_base_flag_short() {- let args = vec![- "wt".to_string(),- "switch".to_string(),- "--create".to_string(),- "new".to_string(),- "-b".to_string(),- "dev".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);- }-- #[test]- fn test_parse_completion_context_base_at_end() {- // --base at the end with empty string (what shell sends when completing)- let args = vec![- "wt".to_string(),- "switch".to_string(),- "--create".to_string(),- "new".to_string(),- "--base".to_string(),- "".to_string(), // Shell sends empty string for cursor position- ];- // Should detect BaseFlag context- assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);- }-- #[test]- fn test_parse_completion_context_multiple_base_flags() {- // Multiple --base flags (last one wins)- let args = vec![- "wt".to_string(),- "switch".to_string(),- "--create".to_string(),- "new".to_string(),- "--base".to_string(),- "main".to_string(),- "--base".to_string(),- "develop".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);- }-- #[test]- fn test_parse_completion_context_empty_args() {- let args = vec![];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_switch_only() {- // Just "wt switch" with no other args- let args = vec!["wt".to_string(), "switch".to_string()];- assert_eq!(- parse_completion_context(&args),- CompletionContext::SwitchBranch- );- }-- #[test]- fn test_parse_completion_context_dev_run_hook() {- // "wt beta run-hook <cursor>"- // Now handled by clap fallback via Unknown context- let args = vec!["wt".to_string(), "beta".to_string(), "run-hook".to_string()];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_dev_run_hook_partial() {- // "wt beta run-hook po<cursor>"- // Now handled by clap fallback via Unknown context- let args = vec![- "wt".to_string(),- "beta".to_string(),- "run-hook".to_string(),- "po".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_dev_only() {- // "wt beta <cursor>" - should not complete- let args = vec!["wt".to_string(), "beta".to_string()];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_base_flag_with_source() {- let args = vec![- "wt".to_string(),- "--source".to_string(),- "switch".to_string(),- "--create".to_string(),- "new".to_string(),- "--base".to_string(),- "dev".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::BaseFlag);- }-- #[test]- fn test_parse_completion_context_beta_run_hook_with_source() {- // Now handled by clap fallback via Unknown context- let args = vec![- "wt".to_string(),- "--source".to_string(),- "beta".to_string(),- "run-hook".to_string(),- "po".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_merge_with_verbose_and_source() {- let args = vec![- "wt".to_string(),- "-v".to_string(),- "--source".to_string(),- "merge".to_string(),- "de".to_string(),- ];- assert_eq!(- parse_completion_context(&args),- CompletionContext::MergeTarget- );- }-- #[test]- fn test_find_subcommand_index_unknown_flag() {- // Unknown flags cause completion to bail out (fail-safe behavior)- let args = vec!["wt".to_string(), "--typo".to_string(), "switch".to_string()];- assert_eq!(find_subcommand_index(&args), None);- }-- #[test]- fn test_find_subcommand_index_empty_after_flag() {- // Empty string after flag (cursor immediately after --source with no subcommand yet)- // Empty string doesn't start with '-', so it's treated as the subcommand position- let args = vec!["wt".to_string(), "--source".to_string(), "".to_string()];- assert_eq!(find_subcommand_index(&args), Some(2));- }-- #[test]- fn test_parse_completion_context_init() {- // "wt init <cursor>"- // Now handled by clap fallback via Unknown context- let args = vec!["wt".to_string(), "init".to_string()];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_init_partial() {- // "wt init fi<cursor>"- // Now handled by clap fallback via Unknown context- let args = vec!["wt".to_string(), "init".to_string(), "fi".to_string()];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_init_with_source() {- // "wt --source init fi<cursor>"- // Now handled by clap fallback via Unknown context- let args = vec![- "wt".to_string(),- "--source".to_string(),- "init".to_string(),- "fi".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-- #[test]- fn test_parse_completion_context_init_with_verbose() {- // "wt -v init ba<cursor>"- // Now handled by clap fallback via Unknown context- let args = vec![- "wt".to_string(),- "-v".to_string(),- "init".to_string(),- "ba".to_string(),- ];- assert_eq!(parse_completion_context(&args), CompletionContext::Unknown);- }-}diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex 5e333fcca..128798294 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -179,6 +179,90 @@ fn test_complete_base_flag_shows_all_branches() {assert!(branches.iter().any(|b| b.contains("develop")));}+#[test]+fn test_complete_base_flag_with_equals() {+ let temp = TestRepo::new();+ temp.commit("initial");++ // Create some branches+ Command::new("git")+ .args(["branch", "develop"])+ .current_dir(temp.root_path())+ .output()+ .unwrap();++ Command::new("git")+ .args(["branch", "feature/existing"])+ .current_dir(temp.root_path())+ .output()+ .unwrap();++ // Test completion for --base= format (equals sign, no space)+ let mut cmd = wt_command();+ temp.clean_cli_env(&mut cmd);+ let output = cmd+ .current_dir(temp.root_path())+ .args([+ "complete",+ "wt",+ "switch",+ "--create",+ "new-branch",+ "--base=",+ ])+ .output()+ .unwrap();++ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);+ let branches: Vec<&str> = stdout.lines().collect();++ // Should show all branches as potential base+ assert!(branches.iter().any(|b| b.contains("develop")));+ assert!(branches.iter().any(|b| b.contains("feature/existing")));+ assert!(branches.iter().any(|b| b.contains("main")));++ // Test completion for --base=m (equals sign with partial value)+ let mut cmd = wt_command();+ temp.clean_cli_env(&mut cmd);+ let output = cmd+ .current_dir(temp.root_path())+ .args([+ "complete",+ "wt",+ "switch",+ "--create",+ "new-branch",+ "--base=m",+ ])+ .output()+ .unwrap();++ assert!(output.status.success());+ 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")));++ // Test completion for -b= format (short form with equals)+ let mut cmd = wt_command();+ temp.clean_cli_env(&mut cmd);+ let output = cmd+ .current_dir(temp.root_path())+ .args(["complete", "wt", "switch", "--create", "new-branch", "-b="])+ .output()+ .unwrap();++ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);+ let branches: Vec<&str> = stdout.lines().collect();++ // Should show all branches (short form with equals works too)+ assert!(branches.iter().any(|b| b.contains("develop")));+ assert!(branches.iter().any(|b| b.contains("feature/existing")));+}+#[test]fn test_complete_outside_git_repo() {let temp = tempfile::tempdir().unwrap();