-
-
Notifications
You must be signed in to change notification settings - Fork 1.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 native completion support for elvish
There is appending a space that isn't being handled.
- Loading branch information
Showing
6 changed files
with
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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(()) | ||
} | ||
} | ||
|
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
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