-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(complete): Add per-candidate trailing space control #6266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,7 +271,7 @@ fn complete_option( | |
| completions.extend( | ||
| complete_arg_value(value.to_str().ok_or(value), arg, current_dir) | ||
| .into_iter() | ||
| .map(|comp| comp.add_prefix(format!("--{flag}="))), | ||
| .map(|comp| comp.add_prefix(format!("--{flag}=")).nospace(true)), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unconditionally adding |
||
| ); | ||
| } | ||
| } else { | ||
|
|
@@ -308,7 +308,12 @@ fn complete_option( | |
| .into_iter() | ||
| .map(|comp| { | ||
| let sep = if has_equal { "=" } else { "" }; | ||
| comp.add_prefix(format!("-{leading_flags}{sep}")) | ||
| let comp = comp.add_prefix(format!("-{leading_flags}{sep}")); | ||
| if has_equal { | ||
| comp.nospace(true) | ||
| } else { | ||
| comp | ||
| } | ||
|
Comment on lines
+311
to
+316
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||
| }), | ||
| ); | ||
| } else { | ||
|
|
@@ -393,7 +398,7 @@ fn complete_arg_value( | |
| if let Some(prefix) = prefix { | ||
| values = values | ||
| .into_iter() | ||
| .map(|comp| comp.add_prefix(prefix)) | ||
| .map(|comp| comp.add_prefix(prefix).nospace(true)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
| .collect(); | ||
| } | ||
| values = values | ||
|
|
@@ -478,9 +483,14 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> { | |
| p.get_arguments() | ||
| .filter_map(|a| { | ||
| a.get_long_and_visible_aliases().map(|longs| { | ||
| longs | ||
| .into_iter() | ||
| .map(|s| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a)) | ||
| longs.into_iter().map(|s| { | ||
| if a.is_require_equals_set() { | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}=")), a) | ||
| .nospace(true) | ||
| } else { | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a) | ||
|
Comment on lines
+487
to
+491
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this conflict with your I also think I prefer the approach taken in that PR. |
||
| } | ||
| }) | ||
| }) | ||
| }) | ||
| .flatten() | ||
|
|
@@ -495,7 +505,14 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> { | |
| .filter_map(|a| { | ||
| a.get_aliases().map(|longs| { | ||
| longs.into_iter().map(|s| { | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a).hide(true) | ||
| if a.is_require_equals_set() { | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}=")), a) | ||
| .hide(true) | ||
| .nospace(true) | ||
| } else { | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a) | ||
| .hide(true) | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -336,7 +336,8 @@ pub(crate) fn complete_path( | |
| let mut suggestion = prefix.join(&raw_file_name); | ||
| suggestion.push(""); // Ensure trailing `/` | ||
| let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned()) | ||
| .hide(is_hidden(&raw_file_name)); | ||
| .hide(is_hidden(&raw_file_name)) | ||
| .nospace(true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we limit this to Granted, besides sort order, we don't have a good way of communicating which directories match |
||
|
|
||
| if is_wanted(&entry.path()) { | ||
| completions.push(candidate); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1478,3 +1478,166 @@ fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path> | |
| .collect::<Vec<_>>() | ||
| .join("\n") | ||
| } | ||
|
|
||
| /// Like `complete` but includes `[nospace]` marker for candidates with nospace set | ||
| fn complete_with_nospace( | ||
|
Comment on lines
+1482
to
+1483
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally ask for tests to be added in a prior commit, with them passing in that commit |
||
| cmd: &mut Command, | ||
| args: impl AsRef<str>, | ||
| current_dir: Option<&Path>, | ||
| ) -> String { | ||
| let input = args.as_ref(); | ||
| let mut args = vec![std::ffi::OsString::from(cmd.get_name())]; | ||
| let arg_index; | ||
|
|
||
| if let Some((prior, after)) = input.split_once("[TAB]") { | ||
| args.extend(prior.split_whitespace().map(From::from)); | ||
| if prior.ends_with(char::is_whitespace) { | ||
| args.push(std::ffi::OsString::default()); | ||
| } | ||
| arg_index = args.len() - 1; | ||
| args.extend(after.split_whitespace().map(From::from)); | ||
| } else { | ||
| args.extend(input.split_whitespace().map(From::from)); | ||
| if input.ends_with(char::is_whitespace) { | ||
| args.push(std::ffi::OsString::default()); | ||
| } | ||
| arg_index = args.len() - 1; | ||
| } | ||
|
|
||
| clap_complete::engine::complete(cmd, args, arg_index, current_dir) | ||
| .unwrap() | ||
| .into_iter() | ||
| .map(|candidate| { | ||
| let compl = candidate.get_value().to_str().unwrap(); | ||
| let nospace = if candidate.is_nospace_set() { | ||
| "[nospace]" | ||
| } else { | ||
| "" | ||
| }; | ||
| if let Some(help) = candidate.get_help() { | ||
| format!("{compl}\t{help}{nospace}") | ||
| } else if !nospace.is_empty() { | ||
| format!("{compl}\t{nospace}") | ||
| } else { | ||
| compl.to_owned() | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join("\n") | ||
| } | ||
|
|
||
| #[test] | ||
| fn nospace_on_directory_completions() { | ||
| let testdir = snapbox::dir::DirRoot::mutable_temp().unwrap(); | ||
| let testdir_path = testdir.path().unwrap(); | ||
|
|
||
| fs::write(testdir_path.join("a_file"), "").unwrap(); | ||
| fs::create_dir_all(testdir_path.join("b_dir")).unwrap(); | ||
|
|
||
| let mut cmd = Command::new("dynamic").arg( | ||
| clap::Arg::new("input") | ||
| .long("input") | ||
| .value_hint(clap::ValueHint::AnyPath), | ||
| ); | ||
|
|
||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "--input [TAB]", Some(testdir_path)), | ||
| snapbox::str![[r#" | ||
| . | ||
| a_file | ||
| b_dir/ [nospace] | ||
| "#]], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nospace_on_equals_value_completions() { | ||
| let mut cmd = Command::new("dynamic").arg( | ||
| clap::Arg::new("format") | ||
| .long("format") | ||
| .value_parser(["json", "yaml"]), | ||
| ); | ||
|
|
||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "--format=[TAB]", None), | ||
| snapbox::str![[r#" | ||
| --format=json [nospace] | ||
| --format=yaml [nospace] | ||
| "#]], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nospace_on_short_equals_value_completions() { | ||
| let mut cmd = Command::new("dynamic").arg( | ||
| clap::Arg::new("format") | ||
| .long("format") | ||
| .short('F') | ||
| .value_parser(["json", "yaml"]), | ||
| ); | ||
|
|
||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "-F=[TAB]", None), | ||
| snapbox::str![[r#" | ||
| -F=json [nospace] | ||
| -F=yaml [nospace] | ||
| "#]], | ||
| ); | ||
|
|
||
| // Without equals, no nospace | ||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "-F [TAB]", None), | ||
| snapbox::str![[r#" | ||
| json | ||
| yaml | ||
| "#]], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nospace_on_require_equals_options() { | ||
| let mut cmd = Command::new("dynamic").arg( | ||
| clap::Arg::new("format") | ||
| .long("format") | ||
| .require_equals(true) | ||
| .value_parser(["json", "yaml"]), | ||
| ); | ||
|
|
||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "--[TAB]", None), | ||
| snapbox::str![[r#" | ||
| --format= [nospace] | ||
| --help Print help | ||
| "#]], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nospace_on_delimiter_completions() { | ||
| let mut cmd = Command::new("delimiter").arg( | ||
| clap::Arg::new("delimiter") | ||
| .long("delimiter") | ||
| .value_parser(["comma", "space", "tab"]) | ||
| .value_delimiter(','), | ||
| ); | ||
|
|
||
| // First value: no nospace (no prefix) | ||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "--delimiter [TAB]", None), | ||
| snapbox::str![[r#" | ||
| comma | ||
| space | ||
| tab | ||
| "#]], | ||
| ); | ||
|
|
||
| // After delimiter: nospace because of prefix | ||
| assert_data_eq!( | ||
| complete_with_nospace(&mut cmd, "--delimiter comma,[TAB]", None), | ||
| snapbox::str![[r#" | ||
| comma,comma [nospace] | ||
| comma,space [nospace] | ||
| comma,tab [nospace] | ||
| "#]], | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.