From b38538d7c4c69d746a45da242041bb13f0f441e4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 19 Aug 2024 10:37:51 -0500 Subject: [PATCH] fix(complete)!: Rename dynamic to engine This serves as a more specific name. --- clap_complete/src/command/mod.rs | 6 +- clap_complete/src/command/shells.rs | 10 +- .../src/{dynamic => engine}/candidate.rs | 2 +- .../src/{dynamic => engine}/complete.rs | 0 .../src/{dynamic => engine}/custom.rs | 2 +- clap_complete/src/{dynamic => engine}/mod.rs | 0 clap_complete/src/env/mod.rs | 6 +- clap_complete/src/env/shells.rs | 10 +- clap_complete/src/lib.rs | 6 +- .../tests/testsuite/{dynamic.rs => engine.rs} | 228 +++++++++++++----- 10 files changed, 186 insertions(+), 84 deletions(-) rename clap_complete/src/{dynamic => engine}/candidate.rs (98%) rename clap_complete/src/{dynamic => engine}/complete.rs (100%) rename clap_complete/src/{dynamic => engine}/custom.rs (96%) rename clap_complete/src/{dynamic => engine}/mod.rs (100%) rename clap_complete/tests/testsuite/{dynamic.rs => engine.rs} (81%) diff --git a/clap_complete/src/command/mod.rs b/clap_complete/src/command/mod.rs index 97c9845b933..6f9d805496c 100644 --- a/clap_complete/src/command/mod.rs +++ b/clap_complete/src/command/mod.rs @@ -216,7 +216,7 @@ impl CompleteArgs { /// /// This will generally be called by [`CompleteCommand`] or [`CompleteArgs`]. /// -/// This handles adapting between the shell and [`completer`][crate::dynamic::complete()]. +/// This handles adapting between the shell and [`completer`][crate::engine::complete()]. /// A `CommandCompleter` can choose how much of that lives within the registration script and or /// lives in [`CommandCompleter::write_complete`]. pub trait CommandCompleter { @@ -238,9 +238,9 @@ pub trait CommandCompleter { /// Complete the given command /// /// Adapt information from arguments and [`CommandCompleter::write_registration`]-defined env - /// variables to what is needed for [`completer`][crate::dynamic::complete()]. + /// variables to what is needed for [`completer`][crate::engine::complete()]. /// - /// Write out the [`CompletionCandidate`][crate::dynamic::CompletionCandidate]s in a way the shell will understand. + /// Write out the [`CompletionCandidate`][crate::engine::CompletionCandidate]s in a way the shell will understand. fn write_complete( &self, cmd: &mut clap::Command, diff --git a/clap_complete/src/command/shells.rs b/clap_complete/src/command/shells.rs index e057c001f69..acadb16d554 100644 --- a/clap_complete/src/command/shells.rs +++ b/clap_complete/src/command/shells.rs @@ -241,7 +241,7 @@ fi .ok() .and_then(|i| i.parse().ok()); let ifs: Option = std::env::var("IFS").ok().and_then(|i| i.parse().ok()); - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for (i, candidate) in completions.iter().enumerate() { if i != 0 { @@ -335,7 +335,7 @@ set edit:completion:arg-completer[BIN] = { |@words| .and_then(|i| i.parse().ok()) .unwrap_or_default(); let ifs: Option = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok()); - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for (i, candidate) in completions.iter().enumerate() { if i != 0 { @@ -376,7 +376,7 @@ impl CommandCompleter for Fish { buf: &mut dyn std::io::Write, ) -> Result<(), std::io::Error> { let index = args.len() - 1; - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { write!(buf, "{}", candidate.get_content().to_string_lossy())?; @@ -442,7 +442,7 @@ Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ buf: &mut dyn std::io::Write, ) -> Result<(), std::io::Error> { let index = args.len() - 1; - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { write!(buf, "{}", candidate.get_content().to_string_lossy())?; @@ -516,7 +516,7 @@ compdef _clap_dynamic_completer BIN"# if args.len() == index { args.push("".into()); } - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for (i, candidate) in completions.iter().enumerate() { if i != 0 { diff --git a/clap_complete/src/dynamic/candidate.rs b/clap_complete/src/engine/candidate.rs similarity index 98% rename from clap_complete/src/dynamic/candidate.rs rename to clap_complete/src/engine/candidate.rs index ca1e695f71c..50f8b33a4aa 100644 --- a/clap_complete/src/dynamic/candidate.rs +++ b/clap_complete/src/engine/candidate.rs @@ -37,7 +37,7 @@ impl CompletionCandidate { /// Add a prefix to the content of completion candidate /// - /// This is generally used for post-process by [`complete`][crate::dynamic::complete()] for + /// This is generally used for post-process by [`complete`][crate::engine::complete()] for /// things like pre-pending flags, merging delimiter-separated values, etc. pub fn add_prefix(mut self, prefix: impl Into) -> Self { let suffix = self.content; diff --git a/clap_complete/src/dynamic/complete.rs b/clap_complete/src/engine/complete.rs similarity index 100% rename from clap_complete/src/dynamic/complete.rs rename to clap_complete/src/engine/complete.rs diff --git a/clap_complete/src/dynamic/custom.rs b/clap_complete/src/engine/custom.rs similarity index 96% rename from clap_complete/src/dynamic/custom.rs rename to clap_complete/src/engine/custom.rs index 6a2e22fd758..7813773bb75 100644 --- a/clap_complete/src/dynamic/custom.rs +++ b/clap_complete/src/engine/custom.rs @@ -11,7 +11,7 @@ use super::CompletionCandidate; /// /// ```rust /// use clap::Parser; -/// use clap_complete::dynamic::{ArgValueCompleter, CompletionCandidate}; +/// use clap_complete::engine::{ArgValueCompleter, CompletionCandidate}; /// /// #[derive(Debug, Parser)] /// struct Cli { diff --git a/clap_complete/src/dynamic/mod.rs b/clap_complete/src/engine/mod.rs similarity index 100% rename from clap_complete/src/dynamic/mod.rs rename to clap_complete/src/engine/mod.rs diff --git a/clap_complete/src/env/mod.rs b/clap_complete/src/env/mod.rs index 093ac67f4c9..30894a7c73a 100644 --- a/clap_complete/src/env/mod.rs +++ b/clap_complete/src/env/mod.rs @@ -273,7 +273,7 @@ impl<'s> Shells<'s> { /// /// This will generally be called by [`CompleteEnv`]. /// -/// This handles adapting between the shell and [`completer`][crate::dynamic::complete()]. +/// This handles adapting between the shell and [`completer`][crate::engine::complete()]. /// A `EnvCompleter` can choose how much of that lives within the registration script or /// lives in [`EnvCompleter::write_complete`]. pub trait EnvCompleter { @@ -308,9 +308,9 @@ pub trait EnvCompleter { /// Complete the given command /// /// Adapt information from arguments and [`EnvCompleter::write_registration`]-defined env - /// variables to what is needed for [`completer`][crate::dynamic::complete()]. + /// variables to what is needed for [`completer`][crate::engine::complete()]. /// - /// Write out the [`CompletionCandidate`][crate::dynamic::CompletionCandidate]s in a way the shell will understand. + /// Write out the [`CompletionCandidate`][crate::engine::CompletionCandidate]s in a way the shell will understand. fn write_complete( &self, cmd: &mut clap::Command, diff --git a/clap_complete/src/env/shells.rs b/clap_complete/src/env/shells.rs index 5b8579f9700..eee9eee4d66 100644 --- a/clap_complete/src/env/shells.rs +++ b/clap_complete/src/env/shells.rs @@ -86,7 +86,7 @@ fi .ok() .and_then(|i| i.parse().ok()); let ifs: Option = std::env::var("IFS").ok().and_then(|i| i.parse().ok()); - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for (i, candidate) in completions.iter().enumerate() { if i != 0 { @@ -189,7 +189,7 @@ set edit:completion:arg-completer[BIN] = { |@words| .and_then(|i| i.parse().ok()) .unwrap_or_default(); let ifs: Option = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok()); - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for (i, candidate) in completions.iter().enumerate() { if i != 0 { @@ -237,7 +237,7 @@ impl EnvCompleter for Fish { buf: &mut dyn std::io::Write, ) -> Result<(), std::io::Error> { let index = args.len() - 1; - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { write!(buf, "{}", candidate.get_content().to_string_lossy())?; @@ -310,7 +310,7 @@ Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ buf: &mut dyn std::io::Write, ) -> Result<(), std::io::Error> { let index = args.len() - 1; - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { write!(buf, "{}", candidate.get_content().to_string_lossy())?; @@ -393,7 +393,7 @@ compdef _clap_dynamic_completer BIN"# if args.len() == index { args.push("".into()); } - let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; + let completions = crate::engine::complete(cmd, args, index, current_dir)?; for (i, candidate) in completions.iter().enumerate() { if i != 0 { diff --git a/clap_complete/src/lib.rs b/clap_complete/src/lib.rs index 247532830d4..e5631c066ce 100644 --- a/clap_complete/src/lib.rs +++ b/clap_complete/src/lib.rs @@ -69,7 +69,7 @@ pub mod aot; #[cfg(feature = "unstable-command")] pub mod command; #[cfg(feature = "unstable-dynamic")] -pub mod dynamic; +pub mod engine; #[cfg(feature = "unstable-dynamic")] pub mod env; @@ -80,10 +80,10 @@ pub use command::CompleteArgs; pub use command::CompleteCommand; #[doc(inline)] #[cfg(feature = "unstable-dynamic")] -pub use dynamic::ArgValueCompleter; +pub use engine::ArgValueCompleter; #[doc(inline)] #[cfg(feature = "unstable-dynamic")] -pub use dynamic::CompletionCandidate; +pub use engine::CompletionCandidate; #[cfg(feature = "unstable-dynamic")] pub use env::CompleteEnv; diff --git a/clap_complete/tests/testsuite/dynamic.rs b/clap_complete/tests/testsuite/engine.rs similarity index 81% rename from clap_complete/tests/testsuite/dynamic.rs rename to clap_complete/tests/testsuite/engine.rs index bbde81688e1..fc991258e78 100644 --- a/clap_complete/tests/testsuite/dynamic.rs +++ b/clap_complete/tests/testsuite/engine.rs @@ -4,7 +4,7 @@ use std::fs; use std::path::Path; use clap::{builder::PossibleValue, Command}; -use clap_complete::dynamic::{ArgValueCompleter, CompletionCandidate, CustomCompleter}; +use clap_complete::engine::{ArgValueCompleter, CompletionCandidate, CustomCompleter}; use snapbox::assert_data_eq; macro_rules! complete { @@ -25,11 +25,14 @@ fn suggest_subcommand_subset() { .subcommand(Command::new("hello-moon")) .subcommand(Command::new("goodbye-world")); - assert_data_eq!(complete!(cmd, "he"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "he"), + snapbox::str![[r#" hello-moon hello-world help Print this message or the help of the given subcommand(s) -"#]],); +"#]], + ); } #[test] @@ -42,9 +45,15 @@ fn suggest_hidden_long_flags() { .hide(true), ); - assert_data_eq!(complete!(cmd, "--hello-world"), snapbox::str!["--hello-world-visible"]); + assert_data_eq!( + complete!(cmd, "--hello-world"), + snapbox::str!["--hello-world-visible"] + ); - assert_data_eq!(complete!(cmd, "--hello-world-h"), snapbox::str!["--hello-world-hidden"]); + assert_data_eq!( + complete!(cmd, "--hello-world-h"), + snapbox::str!["--hello-world-hidden"] + ); } #[test] @@ -62,18 +71,27 @@ fn suggest_hidden_subcommand_and_aliases() { .hide(true), ); - assert_data_eq!(complete!(cmd, "test"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "test"), + snapbox::str![[r#" test_visible test_visible-alias_visible -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "test_h"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "test_h"), + snapbox::str![[r#" test_hidden test_hidden-alias_hidden test_hidden-alias_visible -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "test_hidden-alias_h"), snapbox::str!["test_hidden-alias_hidden"]); + assert_data_eq!( + complete!(cmd, "test_hidden-alias_h"), + snapbox::str!["test_hidden-alias_hidden"] + ); } #[test] @@ -95,12 +113,15 @@ fn suggest_subcommand_aliases() { .alias("hidden-goodbye"), ); - assert_data_eq!(complete!(cmd, "hello"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "hello"), + snapbox::str![[r#" hello-moon hello-moon-foo hello-world hello-world-foo -"#]],); +"#]], + ); } #[test] @@ -114,9 +135,15 @@ fn suggest_hidden_possible_value() { ]), ); - assert_data_eq!(complete!(cmd, "--test=test"), snapbox::str!["--test=test-visible Say hello to the world"]); + assert_data_eq!( + complete!(cmd, "--test=test"), + snapbox::str!["--test=test-visible Say hello to the world"] + ); - assert_data_eq!(complete!(cmd, "--test=test-h"), snapbox::str!["--test=test-hidden Say hello to the moon"]); + assert_data_eq!( + complete!(cmd, "--test=test-h"), + snapbox::str!["--test=test-hidden Say hello to the moon"] + ); } #[test] @@ -136,20 +163,32 @@ fn suggest_hidden_long_flag_aliases() { .hide(true), ); - assert_data_eq!(complete!(cmd, "--test"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--test"), + snapbox::str![[r#" --test_visible --test_visible-alias_visible -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "--test_h"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--test_h"), + snapbox::str![[r#" --test_hidden --test_hidden-alias_visible --test_hidden-alias_hidden -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "--test_visible-alias_h"), snapbox::str!["--test_visible-alias_hidden"]); + assert_data_eq!( + complete!(cmd, "--test_visible-alias_h"), + snapbox::str!["--test_visible-alias_hidden"] + ); - assert_data_eq!(complete!(cmd, "--test_hidden-alias_h"), snapbox::str!["--test_hidden-alias_hidden"]); + assert_data_eq!( + complete!(cmd, "--test_hidden-alias_h"), + snapbox::str!["--test_hidden-alias_hidden"] + ); } #[test] @@ -171,11 +210,14 @@ fn suggest_long_flag_subset() { .action(clap::ArgAction::Count), ); - assert_data_eq!(complete!(cmd, "--he"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--he"), + snapbox::str![[r#" --hello-world --hello-moon --help Print help -"#]],); +"#]], + ); } #[test] @@ -187,10 +229,13 @@ fn suggest_possible_value_subset() { "goodbye-world".into(), ])); - assert_data_eq!(complete!(cmd, "hello"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "hello"), + snapbox::str![[r#" hello-world Say hello to the world hello-moon -"#]],); +"#]], + ); } #[test] @@ -212,12 +257,15 @@ fn suggest_additional_short_flags() { .action(clap::ArgAction::Count), ); - assert_data_eq!(complete!(cmd, "-a"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-a"), + snapbox::str![[r#" -aa -ab -ac -ah Print help -"#]],); +"#]], + ); } #[test] @@ -230,13 +278,16 @@ fn suggest_subcommand_positional() { ]), )); - assert_data_eq!(complete!(cmd, "hello-world [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "hello-world [TAB]"), + snapbox::str![[r#" --help Print help (see more with '--help') -h Print help (see more with '--help') hello-world Say hello to the world hello-moon goodbye-world -"#]],); +"#]], + ); } #[test] @@ -301,17 +352,23 @@ d_dir/ snapbox::str!["b_file"], ); - assert_data_eq!(complete!(cmd, "--format [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--format [TAB]"), + snapbox::str![[r#" json yaml toml -"#]],); +"#]], + ); - assert_data_eq!(complete!(cmd, "-F [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-F [TAB]"), + snapbox::str![[r#" json yaml toml -"#]],); +"#]], + ); assert_data_eq!(complete!(cmd, "--format j[TAB]"), snapbox::str!["json"],); @@ -321,13 +378,18 @@ toml assert_data_eq!(complete!(cmd, "-F t[TAB]"), snapbox::str!["toml"],); - assert_data_eq!(complete!(cmd, "-cccF [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-cccF [TAB]"), + snapbox::str![[r#" json yaml toml -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "--input a_file [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--input a_file [TAB]"), + snapbox::str![[r#" --input --format --count @@ -339,7 +401,8 @@ toml pos_a pos_b pos_c -"#]]); +"#]] + ); assert_data_eq!( complete!(cmd, "-ci[TAB]", current_dir = Some(testdir_path)), @@ -395,17 +458,23 @@ fn suggest_argument_multi_values() { .num_args(1..=3), ); - assert_data_eq!(complete!(cmd, "--certain-num [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--certain-num [TAB]"), + snapbox::str![[r#" val1 val2 val3 -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "--certain-num val1 [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--certain-num val1 [TAB]"), + snapbox::str![[r#" val1 val2 val3 -"#]]); +"#]] + ); assert_data_eq!( complete!(cmd, "--certain-num val1 val2 val3 [TAB]"), @@ -419,11 +488,14 @@ val3 "#]] ); - assert_data_eq!(complete!(cmd, "--uncertain-num [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--uncertain-num [TAB]"), + snapbox::str![[r#" val1 val2 val3 -"#]]); +"#]] + ); assert_data_eq!( complete!(cmd, "--uncertain-num val1 [TAB]"), @@ -452,34 +524,48 @@ val3 "#]] ); - assert_data_eq!(complete!(cmd, "-Y [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-Y [TAB]"), + snapbox::str![[r#" val1 val2 val3 -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "-Y val1 [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-Y val1 [TAB]"), + snapbox::str![[r#" val1 val2 val3 -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "-Y val1 val2 val3 [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-Y val1 val2 val3 [TAB]"), + snapbox::str![[r#" --certain-num --uncertain-num --help Print help -Y -N -h Print help -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "-N [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-N [TAB]"), + snapbox::str![[r#" val1 val2 val3 -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "-N val1 [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-N val1 [TAB]"), + snapbox::str![[r#" val1 val2 val3 @@ -489,16 +575,20 @@ val3 -Y -N -h Print help -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "-N val1 val2 val3 [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "-N val1 val2 val3 [TAB]"), + snapbox::str![[r#" --certain-num --uncertain-num --help Print help -Y -N -h Print help -"#]]); +"#]] + ); } #[test] @@ -522,11 +612,14 @@ fn suggest_custom_arg_value() { .add::(ArgValueCompleter::new(MyCustomCompleter {})), ); - assert_data_eq!(complete!(cmd, "--custom [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--custom [TAB]"), + snapbox::str![[r#" custom1 custom2 custom3 -"#]],); +"#]], + ); } #[test] @@ -550,19 +643,27 @@ fn suggest_multi_positional() { .value_parser(["json", "yaml", "toml"]), ); - assert_data_eq!(complete!(cmd, "pos_1 pos_a [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "pos_1 pos_a [TAB]"), + snapbox::str![[r#" pos_a pos_b pos_c -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "pos_1 pos_a pos_b [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "pos_1 pos_a pos_b [TAB]"), + snapbox::str![[r#" pos_a pos_b pos_c -"#]]); +"#]] + ); - assert_data_eq!(complete!(cmd, "--format json pos_1 [TAB]"), snapbox::str![[r#" + assert_data_eq!( + complete!(cmd, "--format json pos_1 [TAB]"), + snapbox::str![[r#" --format --help Print help -F @@ -570,7 +671,8 @@ pos_c pos_a pos_b pos_c -"#]]); +"#]] + ); assert_data_eq!( complete!(cmd, "--format json pos_1 pos_a [TAB]"), @@ -794,7 +896,7 @@ fn complete(cmd: &mut Command, args: impl AsRef, current_dir: Option<&Path> arg_index = args.len() - 1; } - clap_complete::dynamic::complete(cmd, args, arg_index, current_dir) + clap_complete::engine::complete(cmd, args, arg_index, current_dir) .unwrap() .into_iter() .map(|candidate| {