-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clap_complete): Add zsh support for native completion
- Loading branch information
Showing
9 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/// Completion support for zsh | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub struct Zsh; | ||
|
||
impl crate::dynamic::Completer for Zsh { | ||
fn file_name(&self, name: &str) -> String { | ||
format!("{name}.zsh") | ||
} | ||
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#"#compdef exhaustive | ||
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)}") | ||
if [[ -n $completions ]]; then | ||
compadd -a completions | ||
fi | ||
} | ||
compdef _clap_dynamic_completer BIN"# | ||
.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()); | ||
|
||
// If the current word is empty, add an empty string to the args | ||
let mut args = args.clone(); | ||
if args.len() == index { | ||
args.push("".into()); | ||
} | ||
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(()) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
clap_complete/tests/snapshots/home/dynamic/exhaustive/zsh/.zshenv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fpath=($fpath $ZDOTDIR/zsh) | ||
autoload -U +X compinit && compinit | ||
precmd_functions="" # avoid the prompt being overwritten | ||
PS1='%% ' | ||
PROMPT='%% ' |
13 changes: 13 additions & 0 deletions
13
clap_complete/tests/snapshots/home/dynamic/exhaustive/zsh/zsh/_exhaustive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#compdef exhaustive | ||
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)}") | ||
|
||
if [[ -n $completions ]]; then | ||
compadd -a completions | ||
fi | ||
} | ||
|
||
compdef _clap_dynamic_completer exhaustive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters