Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(completion): complete command names for all hook types (#3070)
Worktrunk Botaa97ce94b13b
diff --git a/src/completion.rs b/src/completion.rsindex 84b6838f2..737706df6 100644--- a/src/completion.rs+++ b/src/completion.rs@@ -264,31 +264,7 @@ impl ValueCompleter for HookCommandCompleter {// bash-specific prefix filter in maybe_handle_env_completion() handles bash.// Get the hook type from the command line context- let hook_type = CONTEXT.with(|ctx| {- ctx.borrow().as_ref().and_then(|ctx| {- for hook in &[- "pre-start",- "post-start",- "pre-commit",- "post-commit",- "pre-merge",- "post-merge",- "pre-remove",- ] {- if ctx.contains(hook) {- return Some(*hook);- }- }- // Silent aliases still complete, mapped to the canonical name.- if ctx.contains("pre-create") {- return Some("pre-start");- }- if ctx.contains("post-create") {- return Some("post-start");- }- None- })- });+ let hook_type = CONTEXT.with(|ctx| ctx.borrow().as_ref().and_then(detect_hook_type));let Some(hook_type_str) = hook_type else {return Vec::new();@@ -417,6 +393,33 @@ impl CompletionContext {}}+/// Identify which hook type the user is completing a command name for by+/// scanning the command line for a hook-type argument. Returns the canonical+/// name, mapping the silent `pre-create`/`post-create` aliases to+/// `pre-start`/`post-start`.+///+/// The candidate list is derived from [`cli::HOOK_TYPE_NAMES`] rather than a+/// local copy, so a newly added hook type completes its command names without a+/// second edit here. `contains` is an exact match (not a substring test), so+/// iterating the full list has no shadowing hazard between names like+/// `pre-remove` and `post-remove`. `test_detect_hook_type_covers_all_names`+/// pins the coverage.+fn detect_hook_type(ctx: &CompletionContext) -> Option<&'static str> {+ for &hook in cli::HOOK_TYPE_NAMES {+ if ctx.contains(hook) {+ return Some(hook);+ }+ }+ // Silent aliases still complete, mapped to the canonical name.+ if ctx.contains("pre-create") {+ return Some("pre-start");+ }+ if ctx.contains("post-create") {+ return Some("post-start");+ }+ None+}+// Thread-local context tracking is required because clap's ValueCompleter::complete()// receives only the current argument being completed, not the full command line.// We need access to all arguments to detect `--create` / `-c` flags and suppress@@ -798,6 +801,52 @@ fn hide_non_positional_options_for_completion(cmd: Command) -> Command {mod tests {use super::*;+ /// Every canonical hook type must be detected so `wt hook <type> <Tab>`+ /// completes its configured command names. A hook type listed in+ /// HOOK_TYPE_NAMES but missing from `detect_hook_type`'s scan would+ /// silently return no completions — this previously dropped `pre-switch`,+ /// `post-switch`, and `post-remove`.+ #[test]+ fn test_detect_hook_type_covers_all_names() {+ for &name in cli::HOOK_TYPE_NAMES {+ let ctx = CompletionContext {+ args: vec!["hook".into(), name.into()],+ };+ assert_eq!(+ detect_hook_type(&ctx),+ Some(name),+ "hook type `{name}` should be detected for completion"+ );+ // The detected name must round-trip through HookType parsing,+ // mirroring complete()'s downstream `parse::<HookType>()`.+ assert!(+ name.parse::<HookType>().is_ok(),+ "hook type `{name}` should parse"+ );+ }+ }++ /// The silent `pre-create`/`post-create` aliases map to their canonical+ /// names so they still complete command names.+ #[test]+ fn test_detect_hook_type_maps_silent_aliases() {+ for (alias, canonical) in [("pre-create", "pre-start"), ("post-create", "post-start")] {+ let ctx = CompletionContext {+ args: vec!["hook".into(), alias.into()],+ };+ assert_eq!(detect_hook_type(&ctx), Some(canonical));+ }+ }++ /// No hook-type argument on the line means no hook-command completion.+ #[test]+ fn test_detect_hook_type_none_without_hook() {+ let ctx = CompletionContext {+ args: vec!["switch".into(), "main".into()],+ };+ assert_eq!(detect_hook_type(&ctx), None);+ }+#[test]fn test_truncate_template() {// Short template — returned as-is