fix(complete)!: Put CompleteCommand behind unstable-command

Avoid the cost of the deps when not needed.
This commit is contained in:
Ed Page 2024-08-10 20:41:01 -05:00
parent 2b2b2be610
commit b94fce3242
12 changed files with 30 additions and 20 deletions

View file

@ -52,12 +52,13 @@ automod = "1.0.14"
[[example]]
name = "dynamic"
required-features = ["unstable-dynamic"]
required-features = ["unstable-dynamic", "unstable-command"]
[features]
default = []
unstable-doc = ["unstable-dynamic"] # for docs.rs
unstable-dynamic = ["dep:clap_lex", "dep:shlex", "dep:unicode-xid", "clap/derive", "dep:is_executable", "dep:pathdiff", "clap/unstable-ext"]
unstable-doc = ["unstable-dynamic", "unstable-command"] # for docs.rs
unstable-dynamic = ["dep:clap_lex", "dep:is_executable", "dep:pathdiff", "clap/unstable-ext"]
unstable-command = ["unstable-dynamic", "dep:shlex", "dep:unicode-xid", "clap/derive", "dep:is_executable", "dep:pathdiff", "clap/unstable-ext"]
debug = ["clap/debug"]
[lints]

View file

@ -1,5 +1,5 @@
use clap::builder::PossibleValue;
#[cfg(feature = "unstable-dynamic")]
#[cfg(feature = "unstable-command")]
use clap::{FromArgMatches, Subcommand};
use clap_complete::{generate, Generator, Shell};
@ -12,7 +12,7 @@ fn main() {
return;
}
#[cfg(feature = "unstable-dynamic")]
#[cfg(feature = "unstable-command")]
if let Ok(completions) = clap_complete::dynamic::CompleteCommand::from_arg_matches(&matches) {
completions.complete(&mut cli());
return;
@ -195,7 +195,7 @@ fn cli() -> clap::Command {
.value_hint(clap::ValueHint::EmailAddress),
]),
]);
#[cfg(feature = "unstable-dynamic")]
#[cfg(feature = "unstable-command")]
let cli = clap_complete::dynamic::CompleteCommand::augment_subcommands(cli);
cli
}

View file

@ -5,7 +5,7 @@ use clap::builder::StyledStr;
/// A shell-agnostic completion candidate
///
/// [`ShellCompleter`][crate::dynamic::shells::ShellCompleter] will adapt what it can to the
/// [`Shell`][crate::dynamic::shells::Shell] will adapt what it can to the
/// current shell.
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CompletionCandidate {

View file

@ -10,7 +10,7 @@ use super::CompletionCandidate;
///
/// For integration with clap and shells, see [`CompleteCommand`][crate::dynamic::shells::CompleteCommand].
///
/// This is generally called by a [`ShellCompleter`][crate::dynamic::shells::ShellCompleter] which
/// This is generally called by a [`Shell`][crate::dynamic::shells::Shell] which
/// handles the shell-specific logic.
pub fn complete(
cmd: &mut clap::Command,

View file

@ -19,5 +19,7 @@ pub use custom::ArgValueCompleter;
pub use custom::CustomCompleter;
// These live in `shells` because they are tightly coupled with the `ShellCompleter`s
#[cfg(feature = "unstable-command")]
pub use shells::CompleteArgs;
#[cfg(feature = "unstable-command")]
pub use shells::CompleteCommand;

View file

@ -76,6 +76,7 @@ use super::Shell;
#[allow(missing_docs)]
#[derive(Clone, Debug)]
#[command(about = None, long_about = None)]
#[cfg(feature = "unstable-command")]
pub enum CompleteCommand {
/// Register shell completions for this program
#[command(hide = true)]
@ -173,6 +174,7 @@ impl CompleteCommand {
/// ```
#[derive(clap::Args, Clone, Debug)]
#[command(about = None, long_about = None)]
#[cfg(feature = "unstable-command")]
pub struct CompleteArgs {
/// Specify shell to complete for
#[arg(value_name = "NAME")]
@ -231,6 +233,7 @@ impl CompleteArgs {
/// This handles adapting between the shell and [`completer`][crate::dynamic::complete()].
/// A `CommandCompleter` can choose how much of that lives within the registration script and or
/// lives in [`CommandCompleter::write_complete`].
#[cfg(feature = "unstable-command")]
pub trait CommandCompleter {
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String;

View file

@ -1,7 +1,9 @@
//! Shell completion support, see [`CompleteCommand`] for more details
#[cfg(feature = "unstable-command")]
mod command;
#[cfg(feature = "unstable-command")]
pub use command::*;
use std::fmt::Display;
@ -132,22 +134,22 @@ impl ValueEnum for Shell {
}
}
/// A [`CommandCompleter`] for Bash
/// Bash completion adapter
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Bash;
/// A [`CommandCompleter`] for Elvish
/// Elvish completion adapter
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Elvish;
/// A [`CommandCompleter`] for Fish
/// Fish completion adapter
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Fish;
/// A [`CommandCompleter`] for Powershell
/// Powershell completion adapter
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Powershell;
/// A [`CommandCompleter`] for zsh
/// Zsh completion adapter
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Zsh;

View file

@ -111,7 +111,7 @@ fn value_terminator() {
);
}
#[cfg(feature = "unstable-dynamic")]
#[cfg(feature = "unstable-command")]
#[test]
fn register_minimal() {
use clap_complete::dynamic::shells::CommandCompleter as _;

View file

@ -321,6 +321,7 @@ pub(crate) fn register_example<R: completest::RuntimeBuilder>(context: &str, nam
// Unconditionally include to avoid completion file tests failing based on the how
// `cargo test` is invoked
"--features=unstable-dynamic",
"--features=unstable-command",
],
)
.unwrap();
@ -379,6 +380,7 @@ where
// Unconditionally include to avoid completion file tests failing based on the how
// `cargo test` is invoked
"--features=unstable-dynamic",
"--features=unstable-command",
],
)
.unwrap();

View file

@ -175,14 +175,14 @@ value value
assert_data_eq!(actual, expected);
}
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(all(unix, feature = "unstable-command"))]
#[test]
fn register_dynamic() {
common::register_example::<completest_pty::ElvishRuntimeBuilder>("dynamic", "exhaustive");
}
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(all(unix, feature = "unstable-command"))]
fn complete_dynamic() {
if !common::has_command("elvish") {
return;

View file

@ -165,14 +165,14 @@ bash (bash (shell)) fish (fish shell) zsh (zsh shell)"#;
assert_data_eq!(actual, expected);
}
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(all(unix, feature = "unstable-command"))]
#[test]
fn register_dynamic() {
common::register_example::<completest_pty::FishRuntimeBuilder>("dynamic", "exhaustive");
}
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(all(unix, feature = "unstable-command"))]
fn complete_dynamic() {
if !common::has_command("fish") {
return;

View file

@ -163,14 +163,14 @@ pacman action alias value quote hint last --
assert_data_eq!(actual, expected);
}
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(all(unix, feature = "unstable-command"))]
#[test]
fn register_dynamic() {
common::register_example::<completest_pty::ZshRuntimeBuilder>("dynamic", "exhaustive");
}
#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(all(unix, feature = "unstable-command"))]
fn complete_dynamic() {
if !common::has_command("zsh") {
return;