From 8af051c4391ac022e2c6126f12cce4480d021ae7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 20 Sep 2024 13:36:32 -0500 Subject: [PATCH] fix(complete): When completing -[TAB], prioritize shorts --- clap_complete/src/engine/complete.rs | 18 ++++++++++++++++-- clap_complete/tests/testsuite/engine.rs | 8 ++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/clap_complete/src/engine/complete.rs b/clap_complete/src/engine/complete.rs index bfedca52..64cdea89 100644 --- a/clap_complete/src/engine/complete.rs +++ b/clap_complete/src/engine/complete.rs @@ -157,8 +157,7 @@ fn complete_arg( completions.extend(complete_arg_value(arg.to_value(), positional, current_dir)); } - if arg.is_empty() || arg.is_stdio() { - // HACK: Assuming knowledge of is_stdio + if arg.is_empty() { completions.extend(longs_and_visible_aliases(cmd)); completions.extend(hidden_longs_aliases(cmd)); @@ -172,6 +171,21 @@ fn complete_arg( .into_iter() .map(|comp| comp.add_prefix(dash_or_arg.to_string())), ); + } else if arg.is_stdio() { + // HACK: Assuming knowledge of is_stdio + let dash_or_arg = if arg.is_empty() { + "-".into() + } else { + arg.to_value_os().to_string_lossy() + }; + completions.extend( + shorts_and_visible_aliases(cmd) + .into_iter() + .map(|comp| comp.add_prefix(dash_or_arg.to_string())), + ); + + completions.extend(longs_and_visible_aliases(cmd)); + completions.extend(hidden_longs_aliases(cmd)); } else if arg.is_escape() { // HACK: Assuming knowledge of is_escape completions.extend(longs_and_visible_aliases(cmd)); diff --git a/clap_complete/tests/testsuite/engine.rs b/clap_complete/tests/testsuite/engine.rs index de15b674..50ce9247 100644 --- a/clap_complete/tests/testsuite/engine.rs +++ b/clap_complete/tests/testsuite/engine.rs @@ -1114,11 +1114,11 @@ pos-c assert_data_eq!( complete!(cmd, "-[TAB]"), snapbox::str![[r#" ---required-flag ---optional-flag ---long-flag ---help Print help +-r +-o -s +-h Print help +--long-flag "#]] ); assert_data_eq!(