Skip to content
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

preserve ordering of completions passed to zsh autocomplete #1900

Closed
h4ck3rk3y opened this issue Jan 26, 2023 · 8 comments · Fixed by #1903
Closed

preserve ordering of completions passed to zsh autocomplete #1900

h4ck3rk3y opened this issue Jan 26, 2023 · 8 comments · Fixed by #1903

Comments

@h4ck3rk3y
Copy link
Contributor

if eval _describe "completions" completions $flagPrefix $noSpace; then

The way describes works with the following input - apple cat bat, it would sort it for you, and suggest apple bat cat. If we pass the -V flag we end up preserving the ordering of the passed completion,

a real world example,

CURRENT: 4, words[*]: your favorite command 
Truncated words[*]: your favorite command ,
lastParam: , lastChar: 
Adding extra empty parameter
About to call: eval your __complete favorite command  ""
completion output: dry-thunder
patient-field
twilight-shadow
edf1abc0bfc445b38d46a82585a8bfd0
edf78299bbbd453f9552a3b730d68834
edffa85eb3b14e22951f47e252c5fbbf
:4
last line: :4
directive: 4
completions: dry-thunder
patient-field
twilight-shadow
edf1abc0bfc445b38d46a82585a8bfd0
edf78299bbbd453f9552a3b730d68834
edffa85eb3b14e22951f47e252c5fbbf
flagPrefix: 
Adding completion: dry-thunder
Adding completion: patient-field
Adding completion: twilight-shadow
Adding completion: edf1abc0bfc445b38d46a82585a8bfd0
Adding completion: edf78299bbbd453f9552a3b730d68834
Adding completion: edffa85eb3b14e22951f47e252c5fbbf
Calling _describe
_describe found some completions

on pressing tab you get, dry-thunder edf1abc0bfc445b38d46a82585a8bfd0 edf78299bbbd453f9552a3b730d68834 edffa85eb3b14e22951f47e252c5fbbf patient-field twilight-shadow

if we change the above line to

if eval _describe -V "completions" completions $flagPrefix $noSpace; then

we maintain the passed ordering. Would you be open to a contribution that gives users an option to preserve ordering?

@marckhouzam
Copy link
Collaborator

marckhouzam commented Jan 27, 2023

Thanks for the report @h4ck3rk3y .

I think this is tricky because even with the -v things will be dependent on the order the program returns the completions.

I guess that for this option to make sense the program would consciously order the list of completions and specify to preserve the order?

We'd also have to look at how this case applies to the other three shells.

But I'm open to the idea.

@h4ck3rk3y
Copy link
Contributor Author

h4ck3rk3y commented Jan 27, 2023

Yep thats the idea, the person using the library would use a flag to preserve order and then send their ordered list.

Agree, we'll have to figure out how this applies to powershell, fish and bash too. Will see if I can make a contribution for this, sometime next week, time permitting.

@h4ck3rk3y
Copy link
Contributor Author

h4ck3rk3y commented Jan 27, 2023

Bash requires the the nosort option

cobra/bash_completionsV2.go

Lines 354 to 358 in badcce1

if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_%[1]s %[1]s
else
complete -o default -o nospace -F __start_%[1]s %[1]s
fi
, plugging in -o nosort here works.

Fish has a --keep-order , -k flag

Seems like power shell keeps the order by default even with the current code base

@marckhouzam
Copy link
Collaborator

Sounds like a nice addition. I'm looking forward to your contribution.

@h4ck3rk3y
Copy link
Contributor Author

I went through the Cobra code a little more and I am thinking of doing it in the following way

  1. Add another directive lets call it ShellCompDirectiveKeepOrder
  2. Have some logic in the completion to check whether the directive is set, if it is set then add the --keep-order or equivalent flag for the underlying shell

This allows some granular control per validation function and if a user wants to start using it they just set the directive. Let me know if this makes sense!

@marckhouzam
Copy link
Collaborator

Brilliant! That sounds perfect.

@irvinlim
Copy link

+1 for this, just stumbled onto the same issue and came to the same findings about adding -o nosort for zsh.

Would love to provide any assistance if any help is needed support this!

@h4ck3rk3y
Copy link
Contributor Author

h4ck3rk3y commented Jan 30, 2023

i have created a pr using the directive logic i mentioned above, is there a good example to test behavior of generated scripts against passed directives?

i have tested the changes by using generations for our own binary against situations where we want to preserve out put and where we don't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants