fix(complete)!: Switch --shell to a positional

This matches how I've seen it done elsewhere
and removes extra boilerplate
This commit is contained in:
Ed Page 2024-08-10 19:38:59 -05:00
parent c9df58bcf7
commit 64bf287e68
11 changed files with 27 additions and 33 deletions

View file

@ -50,27 +50,27 @@ use super::Shell;
///
/// Bash
/// ```bash
/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc
/// echo "source <(your_program complete bash --register -)" >> ~/.bashrc
/// ```
///
/// Elvish
/// ```elvish
/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv
/// echo "eval (your_program complete elvish --register -)" >> ~/.elvish/rc.elv
/// ```
///
/// Fish
/// ```fish
/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish
/// echo "source (your_program complete fish --register - | psub)" >> ~/.config/fish/config.fish
/// ```
///
/// Powershell
/// ```powershell
/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE
/// echo "your_program complete powershell --register - | Invoke-Expression" >> $PROFILE
/// ```
///
/// Zsh
/// ```zsh
/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc
/// echo "source <(your_program complete zsh --register -)" >> ~/.zshrc
/// ```
#[derive(clap::Subcommand)]
#[allow(missing_docs)]
@ -149,37 +149,37 @@ impl CompleteCommand {
///
/// Bash
/// ```bash
/// echo "source <(your_program complete --shell bash)" >> ~/.bashrc
/// echo "source <(your_program complete bash)" >> ~/.bashrc
/// ```
///
/// Elvish
/// ```elvish
/// echo "eval (your_program complete --shell elvish)" >> ~/.elvish/rc.elv
/// echo "eval (your_program complete elvish)" >> ~/.elvish/rc.elv
/// ```
///
/// Fish
/// ```fish
/// echo "source (your_program complete --shell fish | psub)" >> ~/.config/fish/config.fish
/// echo "source (your_program complete fish | psub)" >> ~/.config/fish/config.fish
/// ```
///
/// Powershell
/// ```powershell
/// echo "your_program complete --shell powershell | Invoke-Expression" >> $PROFILE
/// echo "your_program complete powershell | Invoke-Expression" >> $PROFILE
/// ```
///
/// Zsh
/// ```zsh
/// echo "source <(your_program complete --shell zsh)" >> ~/.zshrc
/// echo "source <(your_program complete zsh)" >> ~/.zshrc
/// ```
#[derive(clap::Args, Clone, Debug)]
#[command(about = None, long_about = None)]
pub struct CompleteArgs {
/// Specify shell to complete for
#[arg(value_name = "NAME")]
shell: Option<Shell>,
#[arg(raw = true, value_name = "ARG", hide = true)]
comp_words: Option<Vec<OsString>>,
/// Specify shell to complete for
#[arg(long, value_name = "NAME")]
shell: Option<Shell>,
}
impl CompleteArgs {
@ -290,7 +290,7 @@ _clap_complete_NAME() {
else
export _CLAP_COMPLETE_SPACE=true
fi
COMPREPLY=( $("COMPLETER" complete --shell bash -- "${COMP_WORDS[@]}") )
COMPREPLY=( $("COMPLETER" complete bash -- "${COMP_WORDS[@]}") )
if [[ $? != 0 ]]; then
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
@ -401,7 +401,7 @@ set edit:completion:arg-completer[BIN] = { |@words|
set index = (- $index 1)
set E:_CLAP_COMPLETE_INDEX = (to-string $index)
put (COMPLETER complete --shell elvish -- $@words) | to-lines
put (COMPLETER complete elvish -- $@words) | to-lines
}
"#
.replace("COMPLETER", &completer)
@ -451,7 +451,7 @@ impl ShellCompleter for super::Fish {
writeln!(
buf,
r#"complete -x -c {bin} -a "("'{completer}'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""#
r#"complete -x -c {bin} -a "("'{completer}'" complete fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""#
)
}
fn write_complete(
@ -501,7 +501,7 @@ impl ShellCompleter for super::Powershell {
Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{
param($wordToComplete, $commandAst, $cursorPosition)
$results = Invoke-Expression "&{completer} complete --shell powershell -- $($commandAst.ToString())";
$results = Invoke-Expression "&{completer} complete powershell -- $($commandAst.ToString())";
$results | ForEach-Object {{
$split = $_.Split("`t");
$cmd = $split[0];
@ -565,7 +565,7 @@ function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n'
local completions=("${(@f)$(COMPLETER complete --shell zsh -- ${words} 2>/dev/null)}")
local completions=("${(@f)$(COMPLETER complete zsh -- ${words} 2>/dev/null)}")
if [[ -n $completions ]]; then
compadd -a completions

View file

@ -10,7 +10,7 @@ _clap_complete_exhaustive() {
else
export _CLAP_COMPLETE_SPACE=true
fi
COMPREPLY=( $("exhaustive" complete --shell bash -- "${COMP_WORDS[@]}") )
COMPREPLY=( $("exhaustive" complete bash -- "${COMP_WORDS[@]}") )
if [[ $? != 0 ]]; then
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

View file

@ -8,7 +8,7 @@ set edit:completion:arg-completer[exhaustive] = { |@words|
set index = (- $index 1)
set E:_CLAP_COMPLETE_INDEX = (to-string $index)
put (exhaustive complete --shell elvish -- $@words) | to-lines
put (exhaustive complete elvish -- $@words) | to-lines
}

View file

@ -1 +1 @@
complete -x -c exhaustive -a "("'exhaustive'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))"
complete -x -c exhaustive -a "("'exhaustive'" complete fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))"

View file

@ -3,7 +3,7 @@ function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
export _CLAP_IFS=$'\n'
local completions=("${(@f)$(exhaustive complete --shell zsh -- ${words} 2>/dev/null)}")
local completions=("${(@f)$(exhaustive complete zsh -- ${words} 2>/dev/null)}")
if [[ -n $completions ]]; then
compadd -a completions

View file

@ -238,16 +238,12 @@ _exhaustive() {
return 0
;;
exhaustive__complete)
opts="-h -V --shell --global --help --version [ARG]..."
opts="-h -V --global --help --version bash elvish fish powershell zsh [ARG]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--shell)
COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;

View file

@ -239,7 +239,6 @@ set edit:completion:arg-completer[exhaustive] = {|@words|
cand --version 'Print version'
}
&'exhaustive;complete'= {
cand --shell 'Specify shell to complete for'
cand --global 'everywhere'
cand -h 'Print help'
cand --help 'Print help'

View file

@ -136,7 +136,6 @@ complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -l email -r
complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -l global -d 'everywhere'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -s h -l help -d 'Print help'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand hint" -s V -l version -d 'Print version'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -l shell -d 'Specify shell to complete for' -r -f -a "{bash\t'',elvish\t'',fish\t'',powershell\t'',zsh\t''}"
complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -l global -d 'everywhere'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -s h -l help -d 'Print help'
complete -c exhaustive -n "__fish_exhaustive_using_subcommand complete" -s V -l version -d 'Print version'

View file

@ -325,12 +325,12 @@ _arguments "${_arguments_options[@]}" : \
;;
(complete)
_arguments "${_arguments_options[@]}" : \
'--shell=[Specify shell to complete for]:NAME:(bash elvish fish powershell zsh)' \
'--global[everywhere]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::shell -- Specify shell to complete for:(bash elvish fish powershell zsh)' \
'*::comp_words:' \
&& ret=0
;;

View file

@ -8,7 +8,7 @@ _clap_complete_my_app() {
else
export _CLAP_COMPLETE_SPACE=true
fi
COMPREPLY=( $("my-app" complete --shell bash -- "${COMP_WORDS[@]}") )
COMPREPLY=( $("my-app" complete bash -- "${COMP_WORDS[@]}") )
if [[ $? != 0 ]]; then
unset COMPREPLY
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

View file

@ -330,7 +330,7 @@ pub(crate) fn register_example<R: completest::RuntimeBuilder>(context: &str, nam
let mut registration = std::process::Command::new(&bin_path);
match context {
"static" => registration.args([format!("--generate={shell_name}")]),
"dynamic" => registration.args(["complete".to_owned(), format!("--shell={shell_name}")]),
"dynamic" => registration.args(["complete", shell_name]),
_ => unreachable!("unsupported context {}", context),
};
let registration = registration.output().unwrap();