Skip to content

Commit 59a61e1

Browse files
committed
refactor(complete): Pull out common candidate code
1 parent 1448791 commit 59a61e1

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

clap_complete/src/engine/complete.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,7 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
426426
.filter_map(|a| {
427427
a.get_long_and_visible_aliases().map(|longs| {
428428
longs.into_iter().map(|s| {
429-
CompletionCandidate::new(format!("--{}", s))
430-
.help(a.get_help().cloned())
431-
.id(Some(format!("arg::{}", a.get_id())))
432-
.tag(Some(
433-
a.get_help_heading().unwrap_or("Options").to_owned().into(),
434-
))
435-
.hide(a.is_hide_set())
429+
populate_arg_candidate(CompletionCandidate::new(format!("--{}", s)), a)
436430
})
437431
})
438432
})
@@ -448,12 +442,7 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
448442
.filter_map(|a| {
449443
a.get_aliases().map(|longs| {
450444
longs.into_iter().map(|s| {
451-
CompletionCandidate::new(format!("--{}", s))
452-
.help(a.get_help().cloned())
453-
.id(Some(format!("arg::{}", a.get_id())))
454-
.tag(Some(
455-
a.get_help_heading().unwrap_or("Options").to_owned().into(),
456-
))
445+
populate_arg_candidate(CompletionCandidate::new(format!("--{}", s)), a)
457446
.hide(true)
458447
})
459448
})
@@ -471,24 +460,31 @@ fn shorts_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
471460
.filter_map(|a| {
472461
a.get_short_and_visible_aliases().map(|shorts| {
473462
shorts.into_iter().map(|s| {
474-
CompletionCandidate::new(s.to_string())
475-
.help(
476-
a.get_help()
477-
.cloned()
478-
.or_else(|| a.get_long().map(|long| format!("--{long}").into())),
479-
)
480-
.id(Some(format!("arg::{}", a.get_id())))
481-
.tag(Some(
482-
a.get_help_heading().unwrap_or("Options").to_owned().into(),
483-
))
484-
.hide(a.is_hide_set())
463+
populate_arg_candidate(CompletionCandidate::new(s.to_string()), a).help(
464+
a.get_help()
465+
.cloned()
466+
.or_else(|| a.get_long().map(|long| format!("--{long}").into())),
467+
)
485468
})
486469
})
487470
})
488471
.flatten()
489472
.collect()
490473
}
491474

475+
fn populate_arg_candidate(candidate: CompletionCandidate, arg: &clap::Arg) -> CompletionCandidate {
476+
candidate
477+
.help(arg.get_help().cloned())
478+
.id(Some(format!("arg::{}", arg.get_id())))
479+
.tag(Some(
480+
arg.get_help_heading()
481+
.unwrap_or("Options")
482+
.to_owned()
483+
.into(),
484+
))
485+
.hide(arg.is_hide_set())
486+
}
487+
492488
/// Get the possible values for completion
493489
fn possible_values(a: &clap::Arg) -> Option<Vec<clap::builder::PossibleValue>> {
494490
if !a.get_num_args().expect("built").takes_values() {
@@ -511,34 +507,31 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
511507
.flat_map(|sc| {
512508
sc.get_name_and_visible_aliases()
513509
.into_iter()
514-
.map(|s| {
515-
CompletionCandidate::new(s.to_string())
516-
.help(sc.get_about().cloned())
517-
.id(Some(format!("command::{}", sc.get_name())))
518-
.tag(Some(
519-
p.get_subcommand_help_heading()
520-
.unwrap_or("Commands")
521-
.to_owned()
522-
.into(),
523-
))
524-
.hide(sc.is_hide_set())
525-
})
510+
.map(|s| populate_command_candidate(CompletionCandidate::new(s.to_string()), p, sc))
526511
.chain(sc.get_aliases().map(|s| {
527-
CompletionCandidate::new(s.to_string())
528-
.help(sc.get_about().cloned())
529-
.id(Some(format!("command::{}", sc.get_name())))
530-
.tag(Some(
531-
p.get_subcommand_help_heading()
532-
.unwrap_or("Commands")
533-
.to_owned()
534-
.into(),
535-
))
512+
populate_command_candidate(CompletionCandidate::new(s.to_string()), p, sc)
536513
.hide(true)
537514
}))
538515
})
539516
.collect()
540517
}
541518

519+
fn populate_command_candidate(
520+
candidate: CompletionCandidate,
521+
cmd: &clap::Command,
522+
subcommand: &clap::Command,
523+
) -> CompletionCandidate {
524+
candidate
525+
.help(subcommand.get_about().cloned())
526+
.id(Some(format!("command::{}", subcommand.get_name())))
527+
.tag(Some(
528+
cmd.get_subcommand_help_heading()
529+
.unwrap_or("Commands")
530+
.to_owned()
531+
.into(),
532+
))
533+
.hide(subcommand.is_hide_set())
534+
}
542535
/// Parse the short flags and find the first `takes_values` option.
543536
fn parse_shortflags<'c, 's>(
544537
cmd: &'c clap::Command,

0 commit comments

Comments
 (0)