mirror of
https://github.com/clap-rs/clap
synced 2024-11-14 08:37:16 +00:00
fix(Elvish): fixes the porting from v2 to v3 for the Elvish shell completions
This commit is contained in:
parent
1ab5e39756
commit
37546c653a
4 changed files with 33 additions and 27 deletions
|
@ -1894,6 +1894,7 @@ impl<'a, 'b> App<'a, 'b> {
|
||||||
Shell::Zsh => format!("_{}", name),
|
Shell::Zsh => format!("_{}", name),
|
||||||
Shell::PowerShell => format!("_{}.ps1", name),
|
Shell::PowerShell => format!("_{}.ps1", name),
|
||||||
Shell::Elvish => format!("{}.elv", name),
|
Shell::Elvish => format!("{}.elv", name),
|
||||||
|
_ => panic!("Unsupported shell type for completion generation")
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut file = match File::create(out_dir.join(file_name)) {
|
let mut file = match File::create(out_dir.join(file_name)) {
|
||||||
|
|
|
@ -2,25 +2,26 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
use app::parser::Parser;
|
use build::App;
|
||||||
use INTERNAL_ERROR_MSG;
|
use INTERNAL_ERROR_MSG;
|
||||||
|
|
||||||
pub struct ElvishGen<'a, 'b>
|
pub struct ElvishGen<'a, 'b, 'c>
|
||||||
where
|
where
|
||||||
'a: 'b,
|
'a: 'b,
|
||||||
|
'b: 'c
|
||||||
{
|
{
|
||||||
p: &'b Parser<'a, 'b>,
|
app: &'c App<'a, 'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> ElvishGen<'a, 'b> {
|
impl<'a, 'b, 'c> ElvishGen<'a, 'b, 'c> {
|
||||||
pub fn new(p: &'b Parser<'a, 'b>) -> Self { ElvishGen { p: p } }
|
pub fn new(p: &'c App<'a, 'b>) -> Self { ElvishGen { app: p } }
|
||||||
|
|
||||||
pub fn generate_to<W: Write>(&self, buf: &mut W) {
|
pub fn generate_to<W: Write>(&self, buf: &mut W) {
|
||||||
let bin_name = self.p.meta.bin_name.as_ref().unwrap();
|
let bin_name = self.app.bin_name.as_ref().unwrap();
|
||||||
|
|
||||||
let mut names = vec![];
|
let mut names = vec![];
|
||||||
let subcommands_cases =
|
let subcommands_cases =
|
||||||
generate_inner(self.p, "", &mut names);
|
generate_inner(self.app, "", &mut names);
|
||||||
|
|
||||||
let result = format!(r#"
|
let result = format!(r#"
|
||||||
edit:completion:arg-completer[{bin_name}] = [@words]{{
|
edit:completion:arg-completer[{bin_name}] = [@words]{{
|
||||||
|
@ -60,50 +61,50 @@ fn get_tooltip<T : ToString>(help: Option<&str>, data: T) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_inner<'a, 'b, 'p>(
|
fn generate_inner<'a, 'b, 'c>(
|
||||||
p: &'p Parser<'a, 'b>,
|
p: &'c App<'a, 'b>,
|
||||||
previous_command_name: &str,
|
previous_command_name: &str,
|
||||||
names: &mut Vec<&'p str>,
|
names: &mut Vec<&'a str>,
|
||||||
) -> String {
|
) -> String where 'a: 'b, 'b: 'c{
|
||||||
debugln!("ElvishGen::generate_inner;");
|
debugln!("ElvishGen::generate_inner;");
|
||||||
let command_name = if previous_command_name.is_empty() {
|
let command_name = if previous_command_name.is_empty() {
|
||||||
p.meta.bin_name.as_ref().expect(INTERNAL_ERROR_MSG).clone()
|
p.bin_name.as_ref().expect(INTERNAL_ERROR_MSG).clone()
|
||||||
} else {
|
} else {
|
||||||
format!("{};{}", previous_command_name, &p.meta.name)
|
format!("{};{}", previous_command_name, &p.name)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut completions = String::new();
|
let mut completions = String::new();
|
||||||
let preamble = String::from("\n cand ");
|
let preamble = String::from("\n cand ");
|
||||||
|
|
||||||
for option in p.opts() {
|
for option in opts!(p) {
|
||||||
if let Some(data) = option.s.short {
|
if let Some(data) = option.short {
|
||||||
let tooltip = get_tooltip(option.b.help, data);
|
let tooltip = get_tooltip(option.help, data);
|
||||||
completions.push_str(&preamble);
|
completions.push_str(&preamble);
|
||||||
completions.push_str(format!("-{} '{}'", data, tooltip).as_str());
|
completions.push_str(format!("-{} '{}'", data, tooltip).as_str());
|
||||||
}
|
}
|
||||||
if let Some(data) = option.s.long {
|
if let Some(data) = option.long {
|
||||||
let tooltip = get_tooltip(option.b.help, data);
|
let tooltip = get_tooltip(option.help, data);
|
||||||
completions.push_str(&preamble);
|
completions.push_str(&preamble);
|
||||||
completions.push_str(format!("--{} '{}'", data, tooltip).as_str());
|
completions.push_str(format!("--{} '{}'", data, tooltip).as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for flag in p.flags() {
|
for flag in flags!(p) {
|
||||||
if let Some(data) = flag.s.short {
|
if let Some(data) = flag.short {
|
||||||
let tooltip = get_tooltip(flag.b.help, data);
|
let tooltip = get_tooltip(flag.help, data);
|
||||||
completions.push_str(&preamble);
|
completions.push_str(&preamble);
|
||||||
completions.push_str(format!("-{} '{}'", data, tooltip).as_str());
|
completions.push_str(format!("-{} '{}'", data, tooltip).as_str());
|
||||||
}
|
}
|
||||||
if let Some(data) = flag.s.long {
|
if let Some(data) = flag.long {
|
||||||
let tooltip = get_tooltip(flag.b.help, data);
|
let tooltip = get_tooltip(flag.help, data);
|
||||||
completions.push_str(&preamble);
|
completions.push_str(&preamble);
|
||||||
completions.push_str(format!("--{} '{}'", data, tooltip).as_str());
|
completions.push_str(format!("--{} '{}'", data, tooltip).as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for subcommand in &p.subcommands {
|
for subcommand in &p.subcommands {
|
||||||
let data = &subcommand.p.meta.name;
|
let data = &subcommand.name;
|
||||||
let tooltip = get_tooltip(subcommand.p.meta.about, data);
|
let tooltip = get_tooltip(subcommand.about, data);
|
||||||
completions.push_str(&preamble);
|
completions.push_str(&preamble);
|
||||||
completions.push_str(format!("{} '{}'", data, tooltip).as_str());
|
completions.push_str(format!("{} '{}'", data, tooltip).as_str());
|
||||||
}
|
}
|
||||||
|
@ -118,7 +119,7 @@ fn generate_inner<'a, 'b, 'p>(
|
||||||
|
|
||||||
for subcommand in &p.subcommands {
|
for subcommand in &p.subcommands {
|
||||||
let subcommand_subcommands_cases =
|
let subcommand_subcommands_cases =
|
||||||
generate_inner(&subcommand.p, &command_name, names);
|
generate_inner(&subcommand, &command_name, names);
|
||||||
subcommands_cases.push_str(&subcommand_subcommands_cases);
|
subcommands_cases.push_str(&subcommand_subcommands_cases);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,11 +28,12 @@ impl<'a, 'b> ComplGen<'a, 'b> {
|
||||||
|
|
||||||
pub fn generate<W: Write>(&self, for_shell: Shell, buf: &mut W) {
|
pub fn generate<W: Write>(&self, for_shell: Shell, buf: &mut W) {
|
||||||
match for_shell {
|
match for_shell {
|
||||||
Shell::Elvish => ElvishGen::new(self.p).generate_to(buf),
|
|
||||||
Shell::Bash => BashGen::new(self.0).generate_to(buf),
|
Shell::Bash => BashGen::new(self.0).generate_to(buf),
|
||||||
Shell::Fish => FishGen::new(self.0).generate_to(buf),
|
Shell::Fish => FishGen::new(self.0).generate_to(buf),
|
||||||
Shell::Zsh => ZshGen::new(self.0).generate_to(buf),
|
Shell::Zsh => ZshGen::new(self.0).generate_to(buf),
|
||||||
Shell::PowerShell => PowerShellGen::new(self.0).generate_to(buf),
|
Shell::PowerShell => PowerShellGen::new(self.0).generate_to(buf),
|
||||||
|
Shell::Elvish => ElvishGen::new(self.0).generate_to(buf),
|
||||||
|
_ => panic!("Unsupported shell type for generating completions")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ pub enum Shell {
|
||||||
PowerShell,
|
PowerShell,
|
||||||
/// Generates a completion file for Elvish
|
/// Generates a completion file for Elvish
|
||||||
Elvish,
|
Elvish,
|
||||||
|
#[doc(hidden)]
|
||||||
|
__Nonexhaustive,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Shell {
|
impl Shell {
|
||||||
|
@ -47,6 +49,7 @@ impl fmt::Display for Shell {
|
||||||
Shell::Zsh => write!(f, "ZSH"),
|
Shell::Zsh => write!(f, "ZSH"),
|
||||||
Shell::PowerShell => write!(f, "POWERSHELL"),
|
Shell::PowerShell => write!(f, "POWERSHELL"),
|
||||||
Shell::Elvish => write!(f, "ELVISH"),
|
Shell::Elvish => write!(f, "ELVISH"),
|
||||||
|
_ => panic!("Unsupported shell type for completion generation")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue