From 097482cdd8b3c06b0b976fccac6f05debbd794e4 Mon Sep 17 00:00:00 2001 From: shanmu <shanmu1901@gmail.com> Date: Fri, 14 Jun 2024 18:02:20 +0800 Subject: [PATCH] fix(dynamic): add more completion support for the ci test --- clap_complete/src/dynamic/completer.rs | 24 +++++++++++++++++++++--- clap_complete/tests/testsuite/dynamic.rs | 3 ++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/clap_complete/src/dynamic/completer.rs b/clap_complete/src/dynamic/completer.rs index 573d448d4e1d..0ee66d7c42b0 100644 --- a/clap_complete/src/dynamic/completer.rs +++ b/clap_complete/src/dynamic/completer.rs @@ -145,7 +145,7 @@ pub fn complete( } else if arg.is_short() { if let Some(short) = arg.to_short() { let mut short = short.clone(); - // HACK: Not consider `-fhg` now. During parsing, we assume that ShortFlags are in the format of `-fbar` or `-f=bar`. + // HACK: During completion parsing, we assume that ShortFlags are in the format of `-fbar` or `-f=bar`. let opt = short.next_flag(); state = if let Some(opt) = opt { if let Ok(opt) = opt { @@ -243,7 +243,7 @@ fn complete_arg( ); } else if arg.is_negative_number() { } else if arg.is_short() { - // HACK: Assuming knowledge of -f<TAB>` and `-f=<TAB>` to complete the value of `-f` + // HACK: Assuming knowledge of -f<TAB>` and `-f=<TAB>` to complete the value of `-f`, and `-f<TAB>` to complete another short flag of cmd. if let Some(short) = arg.to_short() { let mut short = short.clone(); let opt = short.next_flag(); @@ -274,6 +274,13 @@ fn complete_arg( } } } + + completions.extend( + shorts_and_visible_aliases(cmd) + .into_iter() + .map(|(f, help)| (format!("{}{}", arg.to_value_os().to_string_lossy(), f).into(), help)), + ); + } } else if arg.is_stdio() { // HACK: Assuming knowledge of is_stdio @@ -289,7 +296,18 @@ fn complete_arg( .map(|(f, help)| (format!("-{}", f).into(), help)), ); } else if arg.is_empty() { - // NOTE: Do nothing for empty arg. + // Complete all the long and short flag of current command. + completions.extend( + longs_and_visible_aliases(cmd) + .into_iter() + .map(|(f, help)| (format!("--{f}").into(), help)), + ); + + completions.extend( + shorts_and_visible_aliases(cmd) + .into_iter() + .map(|(f, help)| (format!("-{}", f).into(), help)), + ); } } diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/dynamic.rs index 196d0d7e1d00..91214439f2e8 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/dynamic.rs @@ -97,7 +97,7 @@ fn suggest_additional_short_flags() { .short('c') .action(clap::ArgAction::Count), ); - + println!("{}", complete!(cmd, "-")); snapbox::assert_eq( snapbox::str![ "-aa @@ -119,6 +119,7 @@ fn suggest_subcommand_positional() { ]), )); + println!("{}", complete!(cmd, "hello-world [TAB]")); snapbox::assert_eq( snapbox::str![ "--help\tPrint help (see more with '--help')