-
-
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 all commits
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 |
|---|---|---|
|
|
@@ -51,8 +51,11 @@ _clap_complete_NAME() { | |
| ) ) | ||
| if [[ $? != 0 ]]; then | ||
| unset COMPREPLY | ||
| elif [[ $_CLAP_COMPLETE_SPACE == false ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then | ||
| compopt -o nospace | ||
| elif [[ $_CLAP_COMPLETE_SPACE == false ]]; then | ||
| if [[ ${#COMPREPLY[@]} -gt 0 ]] && [[ "${COMPREPLY[-1]}" == "_CLAP_COMPLETE_NOSPACE" ]]; then | ||
| unset 'COMPREPLY[-1]' | ||
| compopt -o nospace | ||
| fi | ||
| fi | ||
| } | ||
| if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then | ||
|
|
@@ -90,12 +93,18 @@ fi | |
| let ifs: Option<String> = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok()); | ||
| let completions = crate::engine::complete(cmd, args, index, current_dir)?; | ||
|
|
||
| let has_nospace = completions.iter().any(|c| c.is_nospace_set()); | ||
|
|
||
| for (i, candidate) in completions.iter().enumerate() { | ||
| if i != 0 { | ||
| write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; | ||
| } | ||
| write!(buf, "{}", candidate.get_value().to_string_lossy())?; | ||
| } | ||
| if has_nospace && !completions.is_empty() { | ||
| write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; | ||
| write!(buf, "_CLAP_COMPLETE_NOSPACE")?; | ||
| } | ||
|
Comment on lines
+96
to
+107
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. This makes it so if any candidate doesn't have a space, none of them do. Instead, if nospace is supported, could we always append a space and have bash always add |
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
@@ -160,7 +169,14 @@ set edit:completion:arg-completer[BIN] = { |@words| | |
| var index = (count $words) | ||
| set index = (- $index 1) | ||
|
|
||
| put (env _CLAP_IFS="\n" _CLAP_COMPLETE_INDEX=(to-string $index) VAR="elvish" COMPLETER -- $@words) | to-lines | ||
| env _CLAP_IFS="\n" _CLAP_COMPLETE_INDEX=(to-string $index) VAR="elvish" COMPLETER -- $@words | from-lines | each { |line| | ||
| if (str:has-prefix $line "\x1f") { | ||
| var value = (str:trim-prefix $line "\x1f") | ||
| edit:complex-candidate $value &code-suffix='' | ||
| } else { | ||
| put $line | ||
| } | ||
| } | ||
| } | ||
| "# | ||
| .replace("COMPLETER", &completer) | ||
|
|
@@ -188,6 +204,9 @@ set edit:completion:arg-completer[BIN] = { |@words| | |
| if i != 0 { | ||
| write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; | ||
| } | ||
| if candidate.is_nospace_set() { | ||
| write!(buf, "\x1f")?; | ||
| } | ||
| write!(buf, "{}", candidate.get_value().to_string_lossy())?; | ||
| } | ||
| Ok(()) | ||
|
|
@@ -376,24 +395,18 @@ function _clap_dynamic_completer_NAME() { | |
| )}") | ||
|
|
||
| if [[ -n $completions ]]; then | ||
| local -a dirs=() | ||
| local -a nospace=() | ||
| local -a other=() | ||
| local completion | ||
| for completion in $completions; do | ||
| local value="${completion%%:*}" | ||
| if [[ "$value" == */ ]]; then | ||
| local dir_no_slash="${value%/}" | ||
| if [[ "$completion" == *:* ]]; then | ||
| local desc="${completion#*:}" | ||
| dirs+=("$dir_no_slash:$desc") | ||
| else | ||
| dirs+=("$dir_no_slash") | ||
| fi | ||
| if [[ "$completion" == $'\x1f'* ]]; then | ||
| completion="${completion:1}" | ||
| nospace+=("$completion") | ||
| else | ||
| other+=("$completion") | ||
| fi | ||
| done | ||
| [[ -n $dirs ]] && _describe 'values' dirs -S '/' -r '/' | ||
| [[ -n $nospace ]] && _describe 'values' nospace -S '' | ||
|
Comment on lines
+398
to
+409
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. Again, this is doing it all or nothing |
||
| [[ -n $other ]] && _describe 'values' other | ||
| fi | ||
| } | ||
|
|
@@ -431,6 +444,11 @@ compdef _clap_dynamic_completer_NAME BIN"# | |
| if i != 0 { | ||
| write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; | ||
| } | ||
| // Prefix nospace candidates with \x1f so the registration script can | ||
| // split them into a separate group with -S '' | ||
| if candidate.is_nospace_set() { | ||
| write!(buf, "\x1f")?; | ||
| } | ||
| write!( | ||
| buf, | ||
| "{}", | ||
|
|
||
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.