Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow printing nu completion script with just --completions nushell #2140

Merged
merged 4 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ jobs:
run: |
set -euxo pipefail
cargo build
for shell in bash elvish fish powershell zsh; do
mkdir -p completions
for shell in bash elvish fish nu powershell zsh; do
./target/debug/just --completions $shell > completions/just.$shell
done
mkdir -p man
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ansi_term = "0.12.0"
blake3 = { version = "1.5.0", features = ["rayon", "mmap"] }
camino = "1.0.4"
chrono = "0.4.38"
clap = { version = "4.0.0", features = ["env", "wrap_help"] }
clap = { version = "4.0.0", features = ["derive", "env", "wrap_help"] }
clap_complete = "4.0.0"
clap_mangen = "0.2.20"
ctrlc = { version = "3.1.1", features = ["termination"] }
Expand Down
8 changes: 0 additions & 8 deletions completions/just.nu

This file was deleted.

3 changes: 0 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ watch-readme:
just render-readme
fswatch -ro README.adoc | xargs -n1 -I{} just render-readme

update-completions:
./bin/update-completions

test-completions:
./tests/completions/just.bash

Expand Down
103 changes: 99 additions & 4 deletions src/completions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,99 @@
pub(crate) const FISH_RECIPE_COMPLETIONS: &str = r#"function __fish_just_complete_recipes
use {super::*, clap::ValueEnum};

#[derive(ValueEnum, Debug, Clone, Copy, PartialEq)]
pub(crate) enum Shell {
Bash,
Elvish,
Fish,
#[value(alias = "nu")]
Nushell,
Powershell,
Zsh,
}

impl Shell {
pub(crate) fn script(self) -> RunResult<'static, String> {
match self {
Self::Bash => completions::clap(clap_complete::Shell::Bash),
Self::Elvish => completions::clap(clap_complete::Shell::Elvish),
Self::Fish => completions::clap(clap_complete::Shell::Fish),
Self::Nushell => Ok(completions::NUSHELL_COMPLETION_SCRIPT.into()),
Self::Powershell => completions::clap(clap_complete::Shell::PowerShell),
Self::Zsh => completions::clap(clap_complete::Shell::Zsh),
}
}
}

fn clap(shell: clap_complete::Shell) -> RunResult<'static, String> {
fn replace(haystack: &mut String, needle: &str, replacement: &str) -> RunResult<'static, ()> {
if let Some(index) = haystack.find(needle) {
haystack.replace_range(index..index + needle.len(), replacement);
Ok(())
} else {
Err(Error::internal(format!(
"Failed to find text:\n{needle}\n…in completion script:\n{haystack}"
)))
}
}

let mut script = {
let mut tempfile = tempfile().map_err(|io_error| Error::TempfileIo { io_error })?;

clap_complete::generate(
shell,
&mut crate::config::Config::app(),
env!("CARGO_PKG_NAME"),
&mut tempfile,
);

tempfile
.rewind()
.map_err(|io_error| Error::TempfileIo { io_error })?;

let mut buffer = String::new();

tempfile
.read_to_string(&mut buffer)
.map_err(|io_error| Error::TempfileIo { io_error })?;

buffer
};

match shell {
clap_complete::Shell::Bash => {
for (needle, replacement) in completions::BASH_COMPLETION_REPLACEMENTS {
replace(&mut script, needle, replacement)?;
}
}
clap_complete::Shell::Fish => {
script.insert_str(0, completions::FISH_RECIPE_COMPLETIONS);
}
clap_complete::Shell::PowerShell => {
for (needle, replacement) in completions::POWERSHELL_COMPLETION_REPLACEMENTS {
replace(&mut script, needle, replacement)?;
}
}
clap_complete::Shell::Zsh => {
for (needle, replacement) in completions::ZSH_COMPLETION_REPLACEMENTS {
replace(&mut script, needle, replacement)?;
}
}
_ => {}
}

Ok(script.trim().into())
}

const NUSHELL_COMPLETION_SCRIPT: &str = r#"def "nu-complete just" [] {
(^just --dump --unstable --dump-format json | from json).recipes | transpose recipe data | flatten | where {|row| $row.private == false } | select recipe doc parameters | rename value description
}

# Just: A Command Runner
export extern "just" [
...recipe: string@"nu-complete just", # Recipe(s) to run, may be with argument(s)
]"#;

const FISH_RECIPE_COMPLETIONS: &str = r#"function __fish_just_complete_recipes
just --list 2> /dev/null | tail -n +2 | awk '{
command = $1;
args = $0;
Expand Down Expand Up @@ -37,7 +132,7 @@ complete -c just -a '(__fish_just_complete_recipes)'
# autogenerated completions
"#;

pub(crate) const ZSH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
const ZSH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
(
r#" _arguments "${_arguments_options[@]}" \"#,
r" local common=(",
Expand Down Expand Up @@ -151,7 +246,7 @@ _just "$@""#,
),
];

pub(crate) const POWERSHELL_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[(
const POWERSHELL_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[(
r#"$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText"#,
r#"function Get-JustFileRecipes([string[]]$CommandElements) {
Expand All @@ -178,7 +273,7 @@ pub(crate) const POWERSHELL_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[(
Sort-Object -Property ListItemText"#,
)];

pub(crate) const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
(
r#" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
Expand Down
36 changes: 25 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,9 @@ impl Config {
.arg(
Arg::new(cmd::COMPLETIONS)
.long("completions")
.action(ArgAction::Append)
.num_args(1..)
.action(ArgAction::Set)
.value_name("SHELL")
.value_parser(value_parser!(clap_complete::Shell))
.value_parser(value_parser!(completions::Shell))
.ignore_case(true)
.help("Print shell completion script for <SHELL>"),
)
Expand Down Expand Up @@ -686,7 +685,7 @@ impl Config {
arguments,
overrides,
}
} else if let Some(&shell) = matches.get_one::<clap_complete::Shell>(cmd::COMPLETIONS) {
} else if let Some(&shell) = matches.get_one::<completions::Shell>(cmd::COMPLETIONS) {
Subcommand::Completions { shell }
} else if matches.get_flag(cmd::EDIT) {
Subcommand::Edit
Expand Down Expand Up @@ -1255,13 +1254,13 @@ mod tests {
test! {
name: subcommand_completions,
args: ["--completions", "bash"],
subcommand: Subcommand::Completions{ shell: clap_complete::Shell::Bash },
subcommand: Subcommand::Completions{ shell: completions::Shell::Bash },
}

test! {
name: subcommand_completions_uppercase,
args: ["--completions", "BASH"],
subcommand: Subcommand::Completions{ shell: clap_complete::Shell::Bash },
subcommand: Subcommand::Completions{ shell: completions::Shell::Bash },
}

error! {
Expand Down Expand Up @@ -1544,15 +1543,30 @@ mod tests {
}

error_matches! {
name: completions_arguments,
args: ["--completions", "zsh", "foo"],
name: completions_argument,
args: ["--completions", "foo"],
error: error,
check: {
assert_eq!(error.kind(), clap::error::ErrorKind::InvalidValue);
assert_eq!(error.context().collect::<Vec<_>>(), vec![
(ContextKind::InvalidArg, &ContextValue::String("--completions <SHELL>...".into())),
(ContextKind::InvalidValue, &ContextValue::String("foo".into())),
(ContextKind::ValidValue, &ContextValue::Strings(["bash".into(), "elvish".into(), "fish".into(), "powershell".into(), "zsh".into()].into())),
(
ContextKind::InvalidArg,
&ContextValue::String("--completions <SHELL>".into())),
(
ContextKind::InvalidValue,
&ContextValue::String("foo".into()),
),
(
ContextKind::ValidValue,
&ContextValue::Strings([
"bash".into(),
"elvish".into(),
"fish".into(),
"nushell".into(),
"powershell".into(),
"zsh".into()].into()
),
),
]);
},
}
Expand Down
35 changes: 17 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ pub(crate) use {
unresolved_recipe::UnresolvedRecipe, use_color::UseColor, variables::Variables,
verbosity::Verbosity, warning::Warning,
},
camino::Utf8Path,
derivative::Derivative,
edit_distance::edit_distance,
lexiclean::Lexiclean,
libc::EXIT_FAILURE,
log::{info, warn},
regex::Regex,
serde::{
ser::{SerializeMap, SerializeSeq},
Serialize, Serializer,
},
snafu::{ResultExt, Snafu},
std::{
borrow::Cow,
cmp,
Expand All @@ -47,7 +59,7 @@ pub(crate) use {
ffi::OsString,
fmt::{self, Debug, Display, Formatter},
fs,
io::{self, Write},
io::{self, Read, Seek, Write},
iter::{self, FromIterator},
mem,
ops::Deref,
Expand All @@ -59,23 +71,10 @@ pub(crate) use {
sync::{Mutex, MutexGuard, OnceLock},
vec,
},
{
camino::Utf8Path,
derivative::Derivative,
edit_distance::edit_distance,
lexiclean::Lexiclean,
libc::EXIT_FAILURE,
log::{info, warn},
regex::Regex,
serde::{
ser::{SerializeMap, SerializeSeq},
Serialize, Serializer,
},
snafu::{ResultExt, Snafu},
strum::{Display, EnumDiscriminants, EnumString, IntoStaticStr},
typed_arena::Arena,
unicode_width::{UnicodeWidthChar, UnicodeWidthStr},
},
strum::{Display, EnumDiscriminants, EnumString, IntoStaticStr},
tempfile::tempfile,
typed_arena::Arena,
unicode_width::{UnicodeWidthChar, UnicodeWidthStr},
};

#[cfg(test)]
Expand Down
Loading