Skip to content

Commit

Permalink
feat(clap_complete): add native completion support for elvish
Browse files Browse the repository at this point in the history
    There is appending a space that isn't being handled.
  • Loading branch information
shannmu committed Jun 21, 2024
1 parent 70e8417 commit 0a648c2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
62 changes: 62 additions & 0 deletions clap_complete/src/dynamic/shells/elvish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use unicode_xid::UnicodeXID as _;

/// Completion support for Bash
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Elvish;

impl crate::dynamic::Completer for Elvish {
fn file_name(&self, name: &str) -> String {
format!("{name}.elv")
}
fn write_registration(
&self,
name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> {
let bin = shlex::quote(bin);
let completer = shlex::quote(completer);

let script = r#"
use str
set edit:completion:arg-completer[BIN] = { |@words|
set E:_CLAP_IFS = "\n"
var index = (count $words)
set index = (- $index 1)
set E:_CLAP_COMPLETE_INDEX = (to-string $index)
put (COMPLETER complete --shell elvish -- $@words) | to-lines
}
"#
.replace("COMPLETER", &completer)
.replace("BIN", &bin);

writeln!(buf, "{script}")?;
Ok(())
}
fn write_complete(
&self,
cmd: &mut clap::Command,
args: Vec<std::ffi::OsString>,
current_dir: Option<&std::path::Path>,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> {
let index: usize = std::env::var("_CLAP_COMPLETE_INDEX")
.ok()
.and_then(|i| i.parse().ok())
.unwrap_or_default();
let ifs: Option<String> = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok());
let completions = crate::dynamic::complete(cmd, args, index, current_dir)?;

for (i, (completion, _)) in completions.iter().enumerate() {
if i != 0 {
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
}
write!(buf, "{}", completion.to_string_lossy())?;
}
Ok(())
}
}
2 changes: 2 additions & 0 deletions clap_complete/src/dynamic/shells/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Shell support

mod bash;
mod elvish;
mod fish;
mod shell;

pub use bash::*;
pub use elvish::*;
pub use fish::*;
pub use shell::*;

Expand Down
6 changes: 5 additions & 1 deletion clap_complete/src/dynamic/shells/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum Shell {
Bash,
/// Friendly Interactive `SHell` (fish)
Fish,
/// Elf `SHell` (elvish)
Elvish,
}

impl Display for Shell {
Expand Down Expand Up @@ -39,13 +41,14 @@ impl FromStr for Shell {
// Hand-rolled so it can work even when `derive` feature is disabled
impl ValueEnum for Shell {
fn value_variants<'a>() -> &'a [Self] {
&[Shell::Bash, Shell::Fish]
&[Shell::Bash, Shell::Fish, Shell::Elvish]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Shell::Bash => PossibleValue::new("bash"),
Shell::Fish => PossibleValue::new("fish"),
Shell::Elvish => PossibleValue::new("elvish"),
})
}
}
Expand All @@ -55,6 +58,7 @@ impl Shell {
match self {
Self::Bash => &super::Bash,
Self::Fish => &super::Fish,
Self::Elvish => &super::Elvish,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ _exhaustive() {
fi
case "${prev}" in
--shell)
COMPREPLY=($(compgen -W "bash fish" -- "${cur}"))
COMPREPLY=($(compgen -W "bash fish elvish" -- "${cur}"))
return 0
;;
--register)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ complete -c exhaustive -n "__fish_seen_subcommand_from hint" -l email -r -f
complete -c exhaustive -n "__fish_seen_subcommand_from hint" -l global -d 'everywhere'
complete -c exhaustive -n "__fish_seen_subcommand_from hint" -s h -l help -d 'Print help'
complete -c exhaustive -n "__fish_seen_subcommand_from hint" -s V -l version -d 'Print version'
complete -c exhaustive -n "__fish_seen_subcommand_from complete" -l shell -d 'Specify shell to complete for' -r -f -a "{bash '',fish ''}"
complete -c exhaustive -n "__fish_seen_subcommand_from complete" -l shell -d 'Specify shell to complete for' -r -f -a "{bash '',fish '',elvish ''}"
complete -c exhaustive -n "__fish_seen_subcommand_from complete" -l register -d 'Path to write completion-registration to' -r -F
complete -c exhaustive -n "__fish_seen_subcommand_from complete" -l global -d 'everywhere'
complete -c exhaustive -n "__fish_seen_subcommand_from complete" -s h -l help -d 'Print help (see more with \'--help\')'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ _arguments "${_arguments_options[@]}" : \
;;
(complete)
_arguments "${_arguments_options[@]}" : \
'--shell=[Specify shell to complete for]:SHELL:(bash fish)' \
'--shell=[Specify shell to complete for]:SHELL:(bash fish elvish)' \
'--register=[Path to write completion-registration to]:REGISTER:_files' \
'--global[everywhere]' \
'-h[Print help (see more with '\''--help'\'')]' \
Expand Down

0 comments on commit 0a648c2

Please sign in to comment.