-
-
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 elvish support for native completion
- Loading branch information
Showing
8 changed files
with
122 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,58 @@ | ||
/// 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#" | ||
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
14 changes: 14 additions & 0 deletions
14
clap_complete/tests/snapshots/home/dynamic/exhaustive/elvish/elvish/rc.elv
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,14 @@ | ||
set edit:rprompt = (constantly "") | ||
set edit:prompt = (constantly "% ") | ||
|
||
set edit:completion:arg-completer[exhaustive] = { |@words| | ||
set E:_CLAP_IFS = "\n" | ||
|
||
var index = (count $words) | ||
set index = (- $index 1) | ||
set E:_CLAP_COMPLETE_INDEX = (to-string $index) | ||
|
||
put (exhaustive complete --shell elvish -- $@words) | to-lines | ||
} | ||
|
||
|
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