Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat(completion): Improve positional argument detection

Maximilian Roos61e1aa242580
diff --git a/src/commands/completion.rs b/src/commands/completion.rs
index 00c8b7a21..d5494f55f 100644
--- a/src/commands/completion.rs
+++ b/src/commands/completion.rs
@@ -11,9 +11,22 @@
// isn't designed for. See the extensive test suite in tests/integration_tests/completion.rs
use clap::{Arg, Command, CommandFactory};
+use std::sync::OnceLock;
use worktrunk::git::{GitError, Repository};
use worktrunk::styling::{ERROR, ERROR_EMOJI, println};
+/// Cache the built command to avoid repeated construction cost
+static CMD: OnceLock<Command> = OnceLock::new();
+
+/// Get or build the cached command
+fn built_cmd() -> &'static Command {
+ CMD.get_or_init(|| {
+ let mut cmd = crate::cli::Cli::command();
+ cmd.build();
+ cmd
+ })
+}
+
/// Completion item with optional help text for fish shell descriptions
#[derive(Debug)]
struct Item {
@@ -44,32 +57,140 @@ fn print_items(items: impl IntoIterator<Item = Item>) {
}
}
-/// 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 {
+/// True if `arg` takes one or more values (vs. boolean/COUNT flags)
+fn takes_value(arg: &Arg) -> bool {
+ arg.get_num_args().is_some_and(|n| n.max_values() > 0)
+}
+
+/// Build a fast lookup of options (active subcommand + globals) -> takes_value
+fn build_opt_index<'a>(
+ active: &'a Command,
+ root: &'a Command,
+) -> (
+ std::collections::HashMap<String, bool>,
+ std::collections::HashMap<char, bool>,
+) {
+ use std::collections::HashMap;
+
+ let mut long_map: HashMap<String, bool> = HashMap::new();
+ let mut short_map: HashMap<char, bool> = HashMap::new();
+
+ // Local opts
+ for opt in active.get_opts() {
+ if let Some(l) = opt.get_long() {
+ long_map.insert(l.to_string(), takes_value(opt));
+ }
+ if let Some(s) = opt.get_short() {
+ short_map.insert(s, takes_value(opt));
+ }
+ }
+
+ // Global opts
+ for opt in root.get_opts() {
+ if opt.is_global_set() {
+ if let Some(l) = opt.get_long() {
+ long_map.entry(l.to_string()).or_insert(takes_value(opt));
+ }
+ if let Some(s) = opt.get_short() {
+ short_map.entry(s).or_insert(takes_value(opt));
+ }
+ }
+ }
+
+ (long_map, short_map)
+}
+
+/// Check if a positional argument should be completed using clap introspection
+///
+/// This function uses clap metadata to determine whether we're still completing the first
+/// positional argument. It handles:
+/// - Flags that take values (--flag value, --flag=value, -f value, -fvalue)
+/// - Short flag clusters (-abc where any flag might consume a value)
+/// - POSIX `--` end-of-options terminator
+/// - Global flags (--verbose, --source, --internal)
+///
+/// Returns true if we're still on the first positional argument (should complete branches).
+/// Returns false if a positional has been provided and we've moved past it.
+fn should_complete_positional_arg(
+ args: &[String],
+ start_index: usize,
+ active: &Command,
+ root: &Command,
+) -> bool {
+ let (longs, shorts) = build_opt_index(active, root);
+
let mut i = start_index;
+ let mut accept_opts = true;
- while i < args.len() {
- let arg = &args[i];
+ 'scan: while i < args.len() {
+ let tok = &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
+ if tok.is_empty() {
+ // Cursor placeholder; skip and keep scanning
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)
+ continue 'scan;
+ }
+
+ if accept_opts && tok == "--" {
+ accept_opts = false;
+ i += 1;
+ continue 'scan;
+ }
+
+ if accept_opts && tok.starts_with("--") {
+ let body = &tok[2..];
+ if let Some(_eq) = body.find('=') {
+ // --long=value
+ i += 1;
+ continue 'scan;
+ } else {
+ // --long [value?]
+ let name = body;
+ if longs.get(name).copied().unwrap_or(false) {
+ // Value is next arg unless we are currently typing it
+ if i == args.len() - 1 {
+ // Completing option value, not a positional
+ return false;
+ }
+ i += 2;
+ continue 'scan;
+ }
+ i += 1;
+ continue 'scan;
+ }
+ }
+
+ if accept_opts && tok.starts_with('-') && tok.len() > 1 {
+ // Short clusters: -abc (if some short takes a value, the remainder is its value)
+ let mut chars = tok[1..].chars().peekable();
+ while let Some(c) = chars.next() {
+ let takes = shorts.get(&c).copied().unwrap_or(false);
+ if takes {
+ // If anything remains in-cluster, it's the value: consume token only
+ if chars.peek().is_some() {
+ i += 1;
+ continue 'scan;
+ } else {
+ // Value is next token, unless we're currently typing it
+ if i == args.len() - 1 {
+ return false;
+ }
+ i += 2;
+ continue 'scan;
+ }
+ }
+ }
+ // No short in cluster takes a value -> just a bunch of booleans
i += 1;
+ continue 'scan;
}
+
+ // If we got here, `tok` is positional (or opts are terminated)
+ // Return true iff it's the last token (i.e., we are still completing it)
+ return i >= args.len() - 1;
}
- // No positional arg found yet - should complete
+ // No positional arg found yet -> still need to complete it
true
}
@@ -191,12 +312,12 @@ fn detect_completion_target<'a>(args: &[String], cmd: &'a Command) -> Completion
match subcmd {
"switch" => {
let has_create = args.iter().any(|arg| arg == "--create" || arg == "-c");
- if !has_create && should_complete_positional_arg(args, i) {
+ if !has_create && should_complete_positional_arg(args, i, cur, cmd) {
return CompletionTarget::PositionalBranch(last.to_string());
}
}
"push" | "merge" | "remove" => {
- if should_complete_positional_arg(args, i) {
+ if should_complete_positional_arg(args, i, cur, cmd) {
return CompletionTarget::PositionalBranch(last.to_string());
}
}
@@ -208,10 +329,9 @@ fn detect_completion_target<'a>(args: &[String], cmd: &'a Command) -> Completion
}
pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {
- let mut cmd = crate::cli::Cli::command();
- cmd.build(); // Required for introspection
+ let cmd = built_cmd();
- let target = detect_completion_target(&args, &cmd);
+ let target = detect_completion_target(&args, cmd);
match target {
CompletionTarget::Option(arg, prefix) => {
@@ -241,7 +361,7 @@ pub fn handle_complete(args: Vec<String>) -> Result<(), GitError> {
// 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;
+ let mut cur = cmd;
while i < args.len() {
let tok = &args[i];
if tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose" {
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index 128798294..a955a0be1 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -992,3 +992,197 @@ fn test_complete_list_format_flag() {
assert!(stdout.contains("table"));
assert!(stdout.contains("json"));
}
+
+#[test]
+fn test_complete_switch_with_execute_flag() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "develop"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt switch --execute "code ." <cursor>
+ // Should complete branches because --execute takes a value
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "switch", "--execute", "code .", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let branches: Vec<&str> = stdout.lines().collect();
+ assert!(branches.iter().any(|b| b.contains("develop")));
+ assert!(branches.iter().any(|b| b.contains("main")));
+}
+
+#[test]
+fn test_complete_switch_with_execute_equals_format() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "feature"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt switch --execute="code ." <cursor>
+ // Should complete branches
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "switch", "--execute=code .", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let branches: Vec<&str> = stdout.lines().collect();
+ assert!(branches.iter().any(|b| b.contains("feature")));
+ assert!(branches.iter().any(|b| b.contains("main")));
+}
+
+#[test]
+fn test_complete_switch_short_cluster_with_value() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "bugfix"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt switch -xcode <cursor>
+ // -x takes value "code" (fused), should complete branches for positional
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "switch", "-xcode", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let branches: Vec<&str> = stdout.lines().collect();
+ assert!(branches.iter().any(|b| b.contains("bugfix")));
+ assert!(branches.iter().any(|b| b.contains("main")));
+}
+
+#[test]
+fn test_complete_switch_with_double_dash_terminator() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "feature"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt switch -- <cursor>
+ // After --, everything is positional, should complete branches
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "switch", "--", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let branches: Vec<&str> = stdout.lines().collect();
+ assert!(branches.iter().any(|b| b.contains("feature")));
+ assert!(branches.iter().any(|b| b.contains("main")));
+}
+
+#[test]
+fn test_complete_switch_positional_already_provided() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "existing"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt switch existing <cursor>
+ // Positional already provided, should NOT complete branches
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "switch", "existing", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ // Should not suggest branches (stdout should be empty or not contain branches)
+ assert_eq!(stdout.trim(), "");
+}
+
+#[test]
+fn test_complete_switch_completing_execute_value() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "develop"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt switch --execute <cursor>
+ // Currently typing the value for --execute, should NOT complete branches
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "switch", "--execute", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ // Should not suggest branches when completing option value
+ assert_eq!(stdout.trim(), "");
+}
+
+#[test]
+fn test_complete_merge_with_flags() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ Command::new("git")
+ .args(["branch", "hotfix"])
+ .current_dir(temp.root_path())
+ .output()
+ .unwrap();
+
+ // Test: wt merge --no-remove --force <cursor>
+ // Should complete branches for positional (boolean flags don't consume arguments)
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "merge", "--no-remove", "--force", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let branches: Vec<&str> = stdout.lines().collect();
+ assert!(branches.iter().any(|b| b.contains("hotfix")));
+ assert!(branches.iter().any(|b| b.contains("main")));
+}