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

Improved completions [for #145] #128

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

Farid-NL
Copy link

@Farid-NL Farid-NL commented May 20, 2024

Hi! 👋

The main goal of this PR is to provide a list of abbreviations when calling the erase and rename commands.

There were other improvements I made a long the way though

Using double quotes only when necessary

Reasoning

Even though could be a performance improvement between using single or double quotes... this might be my OCD triggered.

Separate options from commands

Reasoning

Normally when pressing Tab you expect just the list of commands, but not the options i.e. --help. These will only show up when typing -Tab (for short and long flags) or --Tab (for long flags).

Demo

pr1

List abbreviations when using erase and rename commands

Reasoning

Maybe you don't use an abbreviation anymore, so instead of listing the abbreviations manually, an then type it into the erase or rename command, you can search for it wit the help of the completion system.

Features

  • It show the abbreviation as the candidate and the expansion as the description.
  • It considers scope and type flags for completions.

Demo

pr2

Group related options and set exceptions when using a particular group

Reasoning

There could be multiple options for a specific context, for example:

  • [<SCOPE>]:User (-U || --user), Session (-S || --session)
  • [<TYPE>]:Regular (-r || --regular), Global (-g || --global)
  • [<QUIET_MODE>]:Quiet (-q || --quiet), Quieter (-qq || --quieter)

So when one is chosen, the other options for the same context should not be shown.

Example: -S is used, then --session, -U and --user are not shown.

Demo

pr3

Copy link
Owner

@olets olets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff!

Grouped flags and completing abbreviations is great. Replacing the few tab outliers with spaces is a good fix. And clear commit messages.

I'm going to pull the maintainer card and ask you to bring back the double quotation mark convention. I know how you feel… because I have it opposite

Looks like the abbreviations' completion needs some work. And because abbreviations and expansions can both have single quotes, double quotes, and colons, it's going to be tricky.

completions/_abbr Outdated Show resolved Hide resolved
completions/_abbr Outdated Show resolved Hide resolved
completions/_abbr Outdated Show resolved Hide resolved
completions/_abbr Outdated Show resolved Hide resolved
completions/_abbr Outdated Show resolved Hide resolved
Comment on lines 21 to 22
'--help[Show the manpage.]' \
'(-v --version)'{-v,--version}'[Show the current version.]' \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert pls. I hear your UX expectation, as well as the implicit "these aren't commands so they shouldn't be in cmds". But in the special cases of help and version I prefer to eagerly surface their existence, instead of only after -

Copy link
Author

@Farid-NL Farid-NL May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I will revert it.

Since _values has a similar syntax to _arguments, we could group the related commands the same way, but this may be unnecessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1164090

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since _values has a similar syntax to _arguments, we could group the related commands the same way, but this may be unnecessary

Say more?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, an example would be:

{a,add}"[Add a new abbreviation.]" \
{c,clear-session}"[Erase all session abbreviations.]" \
# ...
{R,rename}"[Rename an abbreviation.]" \
{version,-v,--version}"[Show the current version.]"

In this case I don't think it would ne needed to use the parentheses syntax, i.e.

"(R rename)"{R,rename}"[Rename an abbreviation.]"

for excluding related commands/options, since in this context only one can be used and suggested.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can click resolve on this. If you agree @Farid-NL, go ahead and do it

completions/_abbr Outdated Show resolved Hide resolved

return ret
_abbr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there precedent for wrapping this in a function? (it's been a while since I studied others' completion files) Is it advantageous?

Any risk of confusing the shell by giving it the same name as the file? at one point I had a function in zsh-abbr.zsh called _abbr, and that tripped up the completion system.

Copy link
Author

@Farid-NL Farid-NL May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there precedent for wrapping this in a function? (it's been a while since I studied others' completion files) Is it advantageous?

There's a lot of discrepancies for the structure of completions as far as I can see. So my reasoning was that return is valid inside functions.

I've read mainly these completions:

In some of those files they don't even return anything or wrap the logic in a main function _<command_name>(), some of them do. It seems that when there's some complex logic behind the completions they tend to use a wrapper function.


Any risk of confusing the shell by giving it the same name as the file? at one point I had a function in zsh-abbr.zsh called _abbr, and that tripped up the completion system.

None as far as I know if it's only used in the completion file with a wrapper function, this is used by git and docker completions (from the examples above)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I unwrapped the main logic and place the helper functions above it. It seems to work fine. If you want we can do this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undecided.

I feel like zsh completions files are already so opaque, simplify as much as possible so give the dev community examples that aren't impossible to understand. And it's been a while since I looked, but I'm pretty sure I've seen a bunch of others that don't wrap. On the other hand, my convention is to wrap shell script file contents in a function!

Let's come back to it after everything else is finalized.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can click resolve on this. If you agree @Farid-NL, go ahead and do it

@olets
Copy link
Owner

olets commented May 22, 2024

Thanks for being game to talk this through and try different things.

@Farid-NL
Copy link
Author

Thanks for being game to talk this through and try different things.

Glad I can help!

@olets
Copy link
Owner

olets commented May 28, 2024

@Farid-NL
Copy link
Author

Sorry for the delay, some life related stuff.

I have a question regarding some examples to test

# for ' and "
abbr -S 'a"b"c'=d
abbr -S "a'b'c"=e

How would you normally erase/rename these abbreviations? What would you put exactly to erase/rename them?

I'm getting

abbr erase: No abbreviation `abc` found

or

abbr rename: No abbreviation `abc` found

@olets
Copy link
Owner

olets commented Jun 11, 2024

Sorry for the delay, some life related stuff.

No apologies necessary, take all the time you need.

How would you normally erase/rename these abbreviations?

@Farid-NL
Copy link
Author

Farid-NL commented Jun 11, 2024

So, with that limitation in mind what would be the next steps to green light the PR?


Right now just certain characters (#118) doesn't seem to work in abbreviations, expansions with those characters work however (they are shown correctly in the completion's description).

# Display de completion properly the action (rename/erase) works.
abbr -S a:=b
abbr -S b=a:
abbr -S a="b="
abbr -S we="asd^asd\"hey\""
abbr -S wa="as\!hey"

# Display the completion properly but it won´t rename/erase it
abbr -S 'a"b"c'=d
abbr -S "a'b'c"=e
image

@olets olets added this to the v6 milestone Jul 29, 2024
@olets
Copy link
Owner

olets commented Jul 29, 2024

Haven't forgotten!

Planning this for v6 (no estimated date yet)

Closes #145

@olets olets changed the title Improved completions Improved completions [for #145] Jul 29, 2024
@olets
Copy link
Owner

olets commented Aug 1, 2024

@Farid-NL I'm taking advantage of GitHub's "allow edits from maintainers" feature to force-push your branch. I'm rebasing it off my v6 branch, and may add commits too. If you're relying on this branch, you may want to create a new branch at your fork's commit 408204c4208757db7ced7cebc772aa8c9742e666. In case you don't have an easy way to access that commit, I've made a legacy branch in this repo: Farid-NL-better-completions

@olets
Copy link
Owner

olets commented Aug 1, 2024

Scratch that it doesn't let me force push 🥲

@olets
Copy link
Owner

olets commented Aug 1, 2024

Pushed updates to resolve a couple threads:

And I noticed one important completions bug:

Currently the completions will suggest options after the subcommand arguments

abbr erase myabbreviation # TAB shows `-`, another TAB shows flags

but zsh-abbr has a design limitation where all flags have to go before the subcommand name:

# good
abbr --dry-run erase myabbreviation

# errors
abbr erase myabbreviation --dry-run

I see two possibilities: modify the completions so that flags only come before subcommand, or update abbr to not have that limitation (maybe by switching to use zparseopts instead of my own-rolled solution (v6 branch link)).

Removing the limitation would be cool. But if changing the completion is easy, that's probably the way to go for now.

I don't know when I'll have time to work on either. Not soon. If you or someone else reading this is up for looking into it, awesome

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

Successfully merging this pull request may close these issues.

2 participants