Skip to content

Commit 9220bbd

Browse files
shannmuepage
authored andcommitted
feat(clap_complete): Support hiding possible values
1 parent 4395c31 commit 9220bbd

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

clap_complete/src/dynamic/completer.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,14 @@ fn complete_arg(
110110
completions.extend(
111111
complete_arg_value(value.to_str().ok_or(value), arg, current_dir)
112112
.into_iter()
113-
.map(|(os, help)| {
114-
// HACK: Need better `OsStr` manipulation
113+
.map(|comp| {
115114
CompletionCandidate::new(format!(
116115
"--{}={}",
117116
flag,
118-
os.to_string_lossy()
117+
comp.get_content().to_string_lossy()
119118
))
120-
.help(help)
121-
.visible(true)
119+
.help(comp.get_help().cloned())
120+
.visible(comp.is_visible())
122121
}),
123122
);
124123
}
@@ -169,11 +168,7 @@ fn complete_arg(
169168
.get_positionals()
170169
.find(|p| p.get_index() == Some(pos_index))
171170
{
172-
completions.extend(
173-
complete_arg_value(arg.to_value(), positional, current_dir)
174-
.into_iter()
175-
.map(|(os, help)| CompletionCandidate::new(os).help(help).visible(true)),
176-
);
171+
completions.extend(complete_arg_value(arg.to_value(), positional, current_dir).into_iter());
177172
}
178173

179174
if let Ok(value) = arg.to_value() {
@@ -191,16 +186,19 @@ fn complete_arg_value(
191186
value: Result<&str, &OsStr>,
192187
arg: &clap::Arg,
193188
current_dir: Option<&std::path::Path>,
194-
) -> Vec<(OsString, Option<StyledStr>)> {
189+
) -> Vec<CompletionCandidate> {
195190
let mut values = Vec::new();
196191
debug!("complete_arg_value: arg={arg:?}, value={value:?}");
197192

198193
if let Some(possible_values) = possible_values(arg) {
199194
if let Ok(value) = value {
200195
values.extend(possible_values.into_iter().filter_map(|p| {
201196
let name = p.get_name();
202-
name.starts_with(value)
203-
.then(|| (OsString::from(name), p.get_help().cloned()))
197+
name.starts_with(value).then(|| {
198+
CompletionCandidate::new(OsString::from(name))
199+
.help(p.get_help().cloned())
200+
.visible(!p.is_hide_set())
201+
})
204202
}));
205203
}
206204
} else {
@@ -249,7 +247,7 @@ fn complete_path(
249247
value_os: &OsStr,
250248
current_dir: Option<&std::path::Path>,
251249
is_wanted: impl Fn(&std::path::Path) -> bool,
252-
) -> Vec<(OsString, Option<StyledStr>)> {
250+
) -> Vec<CompletionCandidate> {
253251
let mut completions = Vec::new();
254252

255253
let current_dir = match current_dir {
@@ -281,12 +279,20 @@ fn complete_path(
281279
let path = entry.path();
282280
let mut suggestion = pathdiff::diff_paths(&path, current_dir).unwrap_or(path);
283281
suggestion.push(""); // Ensure trailing `/`
284-
completions.push((suggestion.as_os_str().to_owned(), None));
282+
completions.push(
283+
CompletionCandidate::new(suggestion.as_os_str().to_owned())
284+
.help(None)
285+
.visible(true),
286+
);
285287
} else {
286288
let path = entry.path();
287289
if is_wanted(&path) {
288290
let suggestion = pathdiff::diff_paths(&path, current_dir).unwrap_or(path);
289-
completions.push((suggestion.as_os_str().to_owned(), None));
291+
completions.push(
292+
CompletionCandidate::new(suggestion.as_os_str().to_owned())
293+
.help(None)
294+
.visible(true),
295+
);
290296
}
291297
}
292298
}

clap_complete/tests/testsuite/dynamic.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ fn suggest_hidden_possible_value() {
135135

136136
assert_data_eq!(
137137
complete!(cmd, "--test=test"),
138-
snapbox::str![
139-
"--test=test-visible\tSay hello to the world
140-
--test=test-hidden\tSay hello to the moon"
141-
]
138+
snapbox::str!["--test=test-visible\tSay hello to the world"]
142139
);
143140

144141
assert_data_eq!(

0 commit comments

Comments
 (0)