Skip to content

Commit 4d8631d

Browse files
authored
feat: add nushell completions (#11311)
* feat: add nushell completions - `clap_complete` doesn't come with nushell completions built-in but they have another crate `clap_complete_nushell` that does. Sucks it isn't supported by default, see clap-rs/clap#5880. This PR implements a wrapper around the enum with nushell included to support nushell completions too. - moved `clap_complete` to workspace dependency to prevent conflicting versions between foundry-common and cast/anvil/forge. * chore: fix typo and clippy error * chore: move clap_complete_nushell to workspace dependencies
1 parent 386ec98 commit 4d8631d

File tree

12 files changed

+70
-7
lines changed

12 files changed

+70
-7
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ alloy-op-evm = "0.18.3"
274274
anstream = "0.6"
275275
anstyle = "1.0"
276276
terminal_size = "0.4"
277+
clap_complete = "4"
278+
clap_complete_nushell = "4"
277279

278280
# macros
279281
proc-macro2 = "1.0"

crates/anvil/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ clap = { version = "4", features = [
100100
"env",
101101
"wrap_help",
102102
], optional = true }
103-
clap_complete = { version = "4", optional = true }
103+
clap_complete = { workspace = true, optional = true}
104104
chrono.workspace = true
105105
ctrlc = { version = "3", optional = true }
106106
fdlimit = { version = "0.3", optional = true }

crates/anvil/src/args.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ mod tests {
7979
let args: Anvil = Anvil::parse_from(["anvil", "completions", "bash"]);
8080
assert!(matches!(
8181
args.cmd,
82-
Some(AnvilSubcommand::Completions { shell: clap_complete::Shell::Bash })
82+
Some(AnvilSubcommand::Completions {
83+
shell: foundry_common::clap::Shell::ClapCompleteShell(clap_complete::Shell::Bash)
84+
})
8385
));
8486
}
8587
}

crates/anvil/src/opts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum AnvilSubcommand {
2424
#[command(visible_alias = "com")]
2525
Completions {
2626
#[arg(value_enum)]
27-
shell: clap_complete::Shell,
27+
shell: foundry_common::clap::Shell,
2828
},
2929

3030
/// Generate Fig autocompletion spec.

crates/cast/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ serde.workspace = true
7272
foundry-cli.workspace = true
7373

7474
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
75-
clap_complete = "4"
75+
clap_complete.workspace = true
7676
clap_complete_fig = "4"
7777
comfy-table.workspace = true
7878
dunce.workspace = true

crates/cast/src/opts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ pub enum CastSubcommand {
10651065
#[command(visible_alias = "com")]
10661066
Completions {
10671067
#[arg(value_enum)]
1068-
shell: clap_complete::Shell,
1068+
shell: foundry_common::clap::Shell,
10691069
},
10701070

10711071
/// Generate Fig autocompletion spec.

crates/common/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ solar-sema.workspace = true
5050
tower.workspace = true
5151

5252
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
53+
clap_complete.workspace = true
54+
clap_complete_nushell.workspace = true
5355
comfy-table.workspace = true
5456
dunce.workspace = true
5557
eyre.workspace = true

crates/common/src/clap.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use clap_complete::{Shell as ClapCompleteShell, aot::Generator};
2+
use clap_complete_nushell::Nushell;
3+
4+
#[derive(Clone, Copy)]
5+
pub enum Shell {
6+
ClapCompleteShell(ClapCompleteShell),
7+
Nushell,
8+
}
9+
10+
impl clap::ValueEnum for Shell {
11+
fn value_variants<'a>() -> &'a [Self] {
12+
&[
13+
Self::ClapCompleteShell(ClapCompleteShell::Bash),
14+
Self::ClapCompleteShell(ClapCompleteShell::Zsh),
15+
Self::ClapCompleteShell(ClapCompleteShell::Fish),
16+
Self::ClapCompleteShell(ClapCompleteShell::PowerShell),
17+
Self::ClapCompleteShell(ClapCompleteShell::Elvish),
18+
Self::Nushell,
19+
]
20+
}
21+
22+
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
23+
match self {
24+
Self::ClapCompleteShell(shell) => shell.to_possible_value(),
25+
Self::Nushell => Some(clap::builder::PossibleValue::new("nushell")),
26+
}
27+
}
28+
}
29+
30+
impl Generator for Shell {
31+
fn file_name(&self, name: &str) -> String {
32+
match self {
33+
Self::ClapCompleteShell(shell) => shell.file_name(name),
34+
Self::Nushell => Nushell.file_name(name),
35+
}
36+
}
37+
38+
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) {
39+
match self {
40+
Self::ClapCompleteShell(shell) => shell.generate(cmd, buf),
41+
Self::Nushell => Nushell.generate(cmd, buf),
42+
}
43+
}
44+
}

crates/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub use foundry_common_fmt as fmt;
1818

1919
pub mod abi;
2020
pub mod calc;
21+
pub mod clap;
2122
pub mod comments;
2223
pub mod compile;
2324
pub mod constants;

0 commit comments

Comments
 (0)