refactor(complete): Isolate all CommandCompleter knowledge

This commit is contained in:
Ed Page 2024-08-10 20:34:20 -05:00
parent d064946c60
commit 2b2b2be610
2 changed files with 13 additions and 16 deletions

View file

@ -262,7 +262,7 @@ pub trait CommandCompleter {
impl CommandCompleter for Shell { impl CommandCompleter for Shell {
fn file_name(&self, name: &str) -> String { fn file_name(&self, name: &str) -> String {
self.completer().file_name(name) shell_completer(self).file_name(name)
} }
fn write_registration( fn write_registration(
&self, &self,
@ -271,8 +271,7 @@ impl CommandCompleter for Shell {
completer: &str, completer: &str,
buf: &mut dyn std::io::Write, buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
self.completer() shell_completer(self).write_registration(name, bin, completer, buf)
.write_registration(name, bin, completer, buf)
} }
fn write_complete( fn write_complete(
&self, &self,
@ -281,7 +280,17 @@ impl CommandCompleter for Shell {
current_dir: Option<&std::path::Path>, current_dir: Option<&std::path::Path>,
buf: &mut dyn std::io::Write, buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
self.completer().write_complete(cmd, args, current_dir, buf) shell_completer(self).write_complete(cmd, args, current_dir, buf)
}
}
fn shell_completer(shell: &Shell) -> &dyn CommandCompleter {
match shell {
Shell::Bash => &super::Bash,
Shell::Elvish => &super::Elvish,
Shell::Fish => &super::Fish,
Shell::Powershell => &super::Powershell,
Shell::Zsh => &super::Zsh,
} }
} }

View file

@ -132,18 +132,6 @@ impl ValueEnum for Shell {
} }
} }
impl Shell {
fn completer(&self) -> &dyn CommandCompleter {
match self {
Self::Bash => &Bash,
Self::Elvish => &Elvish,
Self::Fish => &Fish,
Self::Powershell => &Powershell,
Self::Zsh => &Zsh,
}
}
}
/// A [`CommandCompleter`] for Bash /// A [`CommandCompleter`] for Bash
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Bash; pub struct Bash;