From b04bf724366e85160a8c3c106345da94c7b55a08 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 22 Aug 2024 12:29:48 -0500 Subject: [PATCH] fix(complete)!: Be consistent in value language --- clap_complete/src/command/shells.rs | 10 +++++----- clap_complete/src/engine/candidate.rs | 24 ++++++++++++------------ clap_complete/src/engine/complete.rs | 12 ++++-------- clap_complete/src/env/shells.rs | 10 +++++----- clap_complete/tests/testsuite/engine.rs | 2 +- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/clap_complete/src/command/shells.rs b/clap_complete/src/command/shells.rs index acadb16d554..cf1cbdd8099 100644 --- a/clap_complete/src/command/shells.rs +++ b/clap_complete/src/command/shells.rs @@ -247,7 +247,7 @@ fi if i != 0 { write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; } - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; } Ok(()) } @@ -341,7 +341,7 @@ set edit:completion:arg-completer[BIN] = { |@words| if i != 0 { write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; } - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; } Ok(()) } @@ -379,7 +379,7 @@ impl CommandCompleter for Fish { let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; if let Some(help) = candidate.get_help() { write!( buf, @@ -445,7 +445,7 @@ Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; if let Some(help) = candidate.get_help() { write!( buf, @@ -522,7 +522,7 @@ compdef _clap_dynamic_completer BIN"# if i != 0 { write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; } - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; } Ok(()) } diff --git a/clap_complete/src/engine/candidate.rs b/clap_complete/src/engine/candidate.rs index ed0d0df1b77..c75b46eb963 100644 --- a/clap_complete/src/engine/candidate.rs +++ b/clap_complete/src/engine/candidate.rs @@ -6,17 +6,17 @@ use clap::builder::StyledStr; /// A shell-agnostic completion candidate #[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct CompletionCandidate { - content: OsString, + value: OsString, help: Option, hidden: bool, } impl CompletionCandidate { /// Create a new completion candidate - pub fn new(content: impl Into) -> Self { - let content = content.into(); + pub fn new(value: impl Into) -> Self { + let value = value.into(); Self { - content, + value, ..Default::default() } } @@ -35,24 +35,24 @@ impl CompletionCandidate { self } - /// Add a prefix to the content of completion candidate + /// Add a prefix to the value of completion candidate /// /// This is generally used for post-process by [`complete`][crate::engine::complete()] for /// things like pre-pending flags, merging delimiter-separated values, etc. pub fn add_prefix(mut self, prefix: impl Into) -> Self { - let suffix = self.content; - let mut content = prefix.into(); - content.push(&suffix); - self.content = content; + let suffix = self.value; + let mut value = prefix.into(); + value.push(&suffix); + self.value = value; self } } /// Reflection API impl CompletionCandidate { - /// Get the content of the completion candidate - pub fn get_content(&self) -> &OsStr { - &self.content + /// Get the literal value being proposed for completion + pub fn get_value(&self) -> &OsStr { + &self.value } /// Get the help message of the completion candidate diff --git a/clap_complete/src/engine/complete.rs b/clap_complete/src/engine/complete.rs index 24fc9cac291..b9bb252293e 100644 --- a/clap_complete/src/engine/complete.rs +++ b/clap_complete/src/engine/complete.rs @@ -150,15 +150,11 @@ fn complete_arg( } } else { completions.extend(longs_and_visible_aliases(cmd).into_iter().filter( - |comp| { - comp.get_content() - .starts_with(format!("--{}", flag).as_str()) - }, + |comp| comp.get_value().starts_with(format!("--{}", flag).as_str()), )); completions.extend(hidden_longs_aliases(cmd).into_iter().filter(|comp| { - comp.get_content() - .starts_with(format!("--{}", flag).as_str()) + comp.get_value().starts_with(format!("--{}", flag).as_str()) })); } } @@ -352,7 +348,7 @@ fn complete_custom_arg_value( debug!("complete_custom_arg_value: completer={completer:?}, value={value:?}"); let mut values = completer.candidates(); - values.retain(|comp| comp.get_content().starts_with(&value.to_string_lossy())); + values.retain(|comp| comp.get_value().starts_with(&value.to_string_lossy())); values } @@ -365,7 +361,7 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec>(); scs.sort(); scs.dedup(); diff --git a/clap_complete/src/env/shells.rs b/clap_complete/src/env/shells.rs index eee9eee4d66..2c527d94dff 100644 --- a/clap_complete/src/env/shells.rs +++ b/clap_complete/src/env/shells.rs @@ -92,7 +92,7 @@ fi if i != 0 { write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; } - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; } Ok(()) } @@ -195,7 +195,7 @@ set edit:completion:arg-completer[BIN] = { |@words| if i != 0 { write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; } - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; } Ok(()) } @@ -240,7 +240,7 @@ impl EnvCompleter for Fish { let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; if let Some(help) = candidate.get_help() { write!( buf, @@ -313,7 +313,7 @@ Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ let completions = crate::engine::complete(cmd, args, index, current_dir)?; for candidate in completions { - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; if let Some(help) = candidate.get_help() { write!( buf, @@ -399,7 +399,7 @@ compdef _clap_dynamic_completer BIN"# if i != 0 { write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; } - write!(buf, "{}", candidate.get_content().to_string_lossy())?; + write!(buf, "{}", candidate.get_value().to_string_lossy())?; } Ok(()) } diff --git a/clap_complete/tests/testsuite/engine.rs b/clap_complete/tests/testsuite/engine.rs index a4211183902..5acb77bc9e2 100644 --- a/clap_complete/tests/testsuite/engine.rs +++ b/clap_complete/tests/testsuite/engine.rs @@ -1006,7 +1006,7 @@ fn complete(cmd: &mut Command, args: impl AsRef, current_dir: Option<&Path> .unwrap() .into_iter() .map(|candidate| { - let compl = candidate.get_content().to_str().unwrap(); + let compl = candidate.get_value().to_str().unwrap(); if let Some(help) = candidate.get_help() { format!("{compl}\t{help}") } else {