Skip to content

Commit

Permalink
fix(complete)!: Put CompleteCommand behind unstable-command
Browse files Browse the repository at this point in the history
Avoid the cost of the deps when not needed.
  • Loading branch information
epage committed Aug 11, 2024
1 parent 2b2b2be commit b94fce3
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 20 deletions.
7 changes: 4 additions & 3 deletions clap_complete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions clap_complete/examples/exhaustive.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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;
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion clap_complete/src/dynamic/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/src/dynamic/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions clap_complete/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions clap_complete/src/dynamic/shells/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions clap_complete/src/dynamic/shells/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down
2 changes: 2 additions & 0 deletions clap_complete/tests/testsuite/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/testsuite/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/testsuite/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/testsuite/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b94fce3

Please sign in to comment.