fix(complete)!: Move Completer into shells

It only exists for shell-adaptation, so it feels like it better belongs
there.
This commit is contained in:
Ed Page 2024-08-09 14:00:42 -05:00
parent 261116d4b4
commit 16366d21f1
9 changed files with 29 additions and 34 deletions

View file

@ -1,23 +0,0 @@
use std::ffi::OsString;
/// Shell-specific completions
pub trait Completer {
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String;
/// Register for completions
fn write_registration(
&self,
name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
/// Complete the given command
fn write_complete(
&self,
cmd: &mut clap::Command,
args: Vec<OsString>,
current_dir: Option<&std::path::Path>,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
}

View file

@ -4,13 +4,11 @@
mod candidate; mod candidate;
mod complete; mod complete;
mod completer;
mod custom; mod custom;
pub mod shells; pub mod shells;
pub use candidate::CompletionCandidate; pub use candidate::CompletionCandidate;
pub use complete::complete; pub use complete::complete;
pub use completer::Completer;
pub use custom::ArgValueCompleter; pub use custom::ArgValueCompleter;
pub use custom::CustomCompleter; pub use custom::CustomCompleter;

View file

@ -4,7 +4,7 @@ use unicode_xid::UnicodeXID as _;
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Bash; pub struct Bash;
impl crate::dynamic::Completer for Bash { impl crate::dynamic::shells::Completer for Bash {
fn file_name(&self, name: &str) -> String { fn file_name(&self, name: &str) -> String {
format!("{name}.bash") format!("{name}.bash")
} }

View file

@ -2,7 +2,7 @@
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Elvish; pub struct Elvish;
impl crate::dynamic::Completer for Elvish { impl crate::dynamic::shells::Completer for Elvish {
fn file_name(&self, name: &str) -> String { fn file_name(&self, name: &str) -> String {
format!("{name}.elv") format!("{name}.elv")
} }

View file

@ -2,7 +2,7 @@
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Fish; pub struct Fish;
impl crate::dynamic::Completer for Fish { impl crate::dynamic::shells::Completer for Fish {
fn file_name(&self, name: &str) -> String { fn file_name(&self, name: &str) -> String {
format!("{name}.fish") format!("{name}.fish")
} }

View file

@ -15,8 +15,6 @@ pub use zsh::*;
use std::ffi::OsString; use std::ffi::OsString;
use std::io::Write as _; use std::io::Write as _;
use crate::dynamic::Completer as _;
/// A subcommand definition to `flatten` into your CLI /// A subcommand definition to `flatten` into your CLI
/// ///
/// This provides a one-stop solution for integrating completions into your CLI /// This provides a one-stop solution for integrating completions into your CLI
@ -162,3 +160,25 @@ impl CompleteCommand {
Ok(()) Ok(())
} }
} }
/// Shell-specific completions
pub trait Completer {
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String;
/// Register for completions
fn write_registration(
&self,
name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
/// Complete the given command
fn write_complete(
&self,
cmd: &mut clap::Command,
args: Vec<OsString>,
current_dir: Option<&std::path::Path>,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
}

View file

@ -57,7 +57,7 @@ impl ValueEnum for Shell {
} }
impl Shell { impl Shell {
fn completer(&self) -> &dyn crate::dynamic::Completer { fn completer(&self) -> &dyn crate::dynamic::shells::Completer {
match self { match self {
Self::Bash => &super::Bash, Self::Bash => &super::Bash,
Self::Fish => &super::Fish, Self::Fish => &super::Fish,
@ -67,7 +67,7 @@ impl Shell {
} }
} }
impl crate::dynamic::Completer for Shell { impl crate::dynamic::shells::Completer for Shell {
fn file_name(&self, name: &str) -> String { fn file_name(&self, name: &str) -> String {
self.completer().file_name(name) self.completer().file_name(name)
} }

View file

@ -2,7 +2,7 @@
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Zsh; pub struct Zsh;
impl crate::dynamic::Completer for Zsh { impl crate::dynamic::shells::Completer for Zsh {
fn file_name(&self, name: &str) -> String { fn file_name(&self, name: &str) -> String {
format!("{name}.zsh") format!("{name}.zsh")
} }

View file

@ -114,7 +114,7 @@ fn value_terminator() {
#[cfg(feature = "unstable-dynamic")] #[cfg(feature = "unstable-dynamic")]
#[test] #[test]
fn register_minimal() { fn register_minimal() {
use clap_complete::dynamic::Completer; use clap_complete::dynamic::shells::Completer;
let name = "my-app"; let name = "my-app";
let bin = name; let bin = name;