Skip to content

Commit

Permalink
fix(complete)!: Be consistent in value language
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 22, 2024
1 parent 0e28259 commit b04bf72
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
10 changes: 5 additions & 5 deletions clap_complete/src/command/shells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
Expand Down
24 changes: 12 additions & 12 deletions clap_complete/src/engine/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StyledStr>,
hidden: bool,
}

impl CompletionCandidate {
/// Create a new completion candidate
pub fn new(content: impl Into<OsString>) -> Self {
let content = content.into();
pub fn new(value: impl Into<OsString>) -> Self {
let value = value.into();
Self {
content,
value,
..Default::default()
}
}
Expand All @@ -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<OsString>) -> 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
Expand Down
12 changes: 4 additions & 8 deletions clap_complete/src/engine/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}));
}
}
Expand Down Expand Up @@ -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
}

Expand All @@ -365,7 +361,7 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<CompletionCandid

let mut scs = subcommands(cmd)
.into_iter()
.filter(|x| x.get_content().starts_with(value))
.filter(|x| x.get_value().starts_with(value))
.collect::<Vec<_>>();
scs.sort();
scs.dedup();
Expand Down
10 changes: 5 additions & 5 deletions clap_complete/src/env/shells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/testsuite/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ fn complete(cmd: &mut Command, args: impl AsRef<str>, 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 {
Expand Down

0 comments on commit b04bf72

Please sign in to comment.