refactor(complete): Add add_prefix to CompletionCandidate

This commit is contained in:
shanmu 2024-08-06 18:18:40 +08:00 committed by Ed Page
parent d222ae4cb6
commit 2b3fc25361

View file

@ -193,15 +193,7 @@ fn complete_arg(
completions.extend(
complete_arg_value(value.to_str().ok_or(value), arg, current_dir)
.into_iter()
.map(|comp| {
CompletionCandidate::new(format!(
"--{}={}",
flag,
comp.get_content().to_string_lossy()
))
.help(comp.get_help().cloned())
.hide(comp.is_hide_set())
}),
.map(|comp| comp.add_prefix(format!("--{}=", flag))),
);
}
} else {
@ -235,15 +227,7 @@ fn complete_arg(
completions.extend(
shorts_and_visible_aliases(cmd)
.into_iter()
// HACK: Need better `OsStr` manipulation
.map(|comp| {
CompletionCandidate::new(format!(
"{}{}",
dash_or_arg,
comp.get_content().to_string_lossy()
))
.help(comp.get_help().cloned())
}),
.map(|comp| comp.add_prefix(dash_or_arg.to_string())),
);
} else if let Some(short) = arg.to_short() {
if !short.is_negative_number() {
@ -265,27 +249,16 @@ fn complete_arg(
complete_arg_value(value.to_str().ok_or(value), opt, current_dir)
.into_iter()
.map(|comp| {
CompletionCandidate::new(format!(
"-{}{}{}",
comp.add_prefix(format!(
"-{}{}",
leading_flags.to_string_lossy(),
if has_equal { "=" } else { "" },
comp.get_content().to_string_lossy()
if has_equal { "=" } else { "" }
))
.help(comp.get_help().cloned())
.hide(comp.is_hide_set())
}),
);
} else {
completions.extend(shorts_and_visible_aliases(cmd).into_iter().map(
|comp| {
CompletionCandidate::new(format!(
"-{}{}",
leading_flags.to_string_lossy(),
comp.get_content().to_string_lossy()
))
.help(comp.get_help().cloned())
.hide(comp.is_hide_set())
},
|comp| comp.add_prefix(format!("-{}", leading_flags.to_string_lossy())),
));
}
}
@ -724,6 +697,15 @@ impl CompletionCandidate {
pub fn is_hide_set(&self) -> bool {
self.hidden
}
/// Add a prefix to the content of completion candidate
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;
self
}
}
/// User-provided completion candidates for an argument.