From ef5fa7f6dea8aac13971c4ddfa1d366e971f7b0c Mon Sep 17 00:00:00 2001 From: Marie Ramlow Date: Fri, 6 Oct 2023 14:29:58 +0200 Subject: [PATCH] cli: add nushell completion support --- CHANGELOG.md | 2 ++ Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + cli/Cargo.toml | 1 + cli/src/commands/mod.rs | 22 +++++++++++++++++----- docs/install-and-setup.md | 10 +++++++++- 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4bb22efd50..202d7da7c12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New features +* `jj util completion` now supports nushell completion generation with `--nushell`. + ### Fixed bugs * Updating the working copy to a commit where a file that's currently ignored diff --git a/Cargo.lock b/Cargo.lock index 9658e1c3ee6..bed58b11b28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -368,6 +368,16 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_complete_nushell" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df47268eb308aaac1ffbfcd8ae43e43820cd094a051b03956d238f2bc60dc3fe" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.4.2" @@ -994,6 +1004,7 @@ dependencies = [ "chrono", "clap", "clap_complete", + "clap_complete_nushell", "clap_mangen", "config", "criterion", diff --git a/Cargo.toml b/Cargo.toml index 98e046d5bbd..1f17e3e6fc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ bytes = "1.5.0" cargo_metadata = "0.17.0" clap = { version = "4.4.6", features = ["derive", "deprecated", "wrap_help"] } clap_complete = "4.4.3" +clap_complete_nushell = "4.4.1" clap_mangen = "0.2.10" chrono = { version = "0.4.31", default-features = false, features = [ "std", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 261fef02dd2..94515ae8cf3 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -33,6 +33,7 @@ cargo_metadata = { workspace = true } chrono = { workspace = true } clap = { workspace = true } clap_complete = { workspace = true } +clap_complete_nushell = { workspace = true } clap_mangen = { workspace = true } config = { workspace = true } criterion = { workspace = true, optional = true } diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 64efba6c664..5849b2c7ab3 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -1144,6 +1144,15 @@ struct UtilCompletionArgs { /// source <(jj util completion --zsh) #[arg(long, verbatim_doc_comment)] zsh: bool, + /// Print a completion script Nushell + /// + /// Apply it by running this: + /// + /// jj util completion --nushell | save ~/.config/nushell/jj.nu + /// 'use ~/.config/nushell/jj.nu' | save --append $nu.config-path + /// source $nu.config-path + #[arg(long, verbatim_doc_comment)] + nushell: bool, } /// Print a ROFF (manpage) @@ -3653,16 +3662,19 @@ fn cmd_util( ) -> Result<(), CommandError> { match subcommand { UtilCommands::Completion(completion_matches) => { + use clap_complete::{generate, Shell}; + let mut app = command.app().clone(); let mut buf = vec![]; - let shell = if completion_matches.zsh { - clap_complete::Shell::Zsh + if completion_matches.zsh { + generate(Shell::Zsh, &mut app, "jj", &mut buf); } else if completion_matches.fish { - clap_complete::Shell::Fish + generate(Shell::Fish, &mut app, "jj", &mut buf); + } else if completion_matches.nushell { + generate(clap_complete_nushell::Nushell, &mut app, "jj", &mut buf); } else { - clap_complete::Shell::Bash + generate(Shell::Bash, &mut app, "jj", &mut buf); }; - clap_complete::generate(shell, &mut app, "jj", &mut buf); ui.stdout_formatter().write_all(&buf)?; } UtilCommands::Mangen(_mangen_matches) => { diff --git a/docs/install-and-setup.md b/docs/install-and-setup.md index b411bb73c94..3cb4cc6d39c 100644 --- a/docs/install-and-setup.md +++ b/docs/install-and-setup.md @@ -142,7 +142,7 @@ $ jj config set --user user.email "martinvonz@google.com" ## Command-line completion To set up command-line completion, source the output of -`jj util completion --bash/--zsh/--fish`. Exactly how to source it +`jj util completion --bash/--zsh/--fish/--nushell`. Exactly how to source it depends on your shell. ### Bash @@ -170,3 +170,11 @@ jj util completion --fish | source ```shell source-bash $(jj util completion) ``` + +### Nushell + +```shell +jj util completion --nushell | save ~/.config/nushell/jj.nu +'use ~/.config/nushell/jj.nu' | save --append $nu.config-path +source $nu.config-path +```