marlonrichert / zsh-autocomplete

🤖 Real-time type-ahead completion for Zsh. Asynchronous find-as-you-type autocompletion.
MIT License
5.37k stars 148 forks source link

Refine `insert-unambiguous yes` behavior #484

Closed yshui closed 2 years ago

yshui commented 2 years ago

Failure to do so can and will result in your issue being closed without warning.

Environment

print $VENDOR $OSTYPE $SHELL $ZSH_ARGZERO $ZSH_PATCHLEVEL
print -l $_autocomplete__funcfiletrace
git -C ~zsh-autocomplete log --oneline -n1
pc linux-gnu /bin/zsh -zsh zsh-5.9-0-g73d3173
/home/shui/.zshrc:21
887bfd6 (HEAD -> master, origin/master, origin/HEAD) Update info action output and update README.md

Steps to reproduce

I'm using zimfw, my .zshrc:

zstyle ':zim:zmodule' use 'degit'
zstyle ':autocomplete:tab' insert-unambiguous 'yes'
zstyle ':autocomplete:recent-dirs' backend zoxide
ZIM_HOME=$HOME/.config/zimfw
ZDOTDIR=$HOME/.config
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
  source ${ZIM_HOME}/zimfw.zsh init -q
fi

source ${ZIM_HOME}/init.zsh

my .zimrc:

#zmodule zsh-users/zsh-syntax-highlighting
#zmodule zsh-users/zsh-autosuggestions
#zmodule zsh-users/zsh-history-substring-search
#zmodule zsh-users/zsh-completions --fpath src
#zmodule completion
zmodule marlonrichert/zsh-autocomplete
#zmodule romkatv/powerlevel10k --use degit

Pressing TAB always insert the first entry in the list, despite insert-unambiguous yes

marlonrichert commented 2 years ago

Can you please use the setup below (which was in the bug report template, but you deleted it) to create a minimal, reproducible test case? Then I'll reopen your issue.

cd $(mktemp -d)
git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git
> .zshrc <<EOF
PS1='%# ' PS2= RPS2='%^'; setopt transientrprompt interactivecomments
source ~/zsh-autocomplete/zsh-autocomplete.plugin.zsh
EOF
env -i HOME=$PWD PATH=$PATH TERM=$TERM ${TERMINFO:+TERMINFO=$TERMINFO} zsh -d
yshui commented 2 years ago

@marlonrichert it happens with this .zshrc:

PS1='%# ' PS2= RPS2='%^'; setopt transientrprompt interactivecomments
zstyle ':autocomplete:tab' insert-unambiguous 'yes'
source ~/zsh-autocomplete/zsh-autocomplete.plugin.zsh
marlonrichert commented 2 years ago

@yshui Please turn it into a test case that has all the steps I need to reproduce the problem. Simply running the code you now pasted does not reproduce the problem.

yshui commented 2 years ago

So:

  1. Use the above .zshrc
  2. cd into a directory with some files
  3. press TAB

Expected:

The longest common prefix is inserted.

Actual:

The first result is inserted.

marlonrichert commented 2 years ago

some files

Please be more specific than that. Update your test case so that I can copy-paste it into my terminal and run it.

Read this for more help on what to do: https://stackoverflow.com/help/minimal-reproducible-example

yshui commented 2 years ago

some files

literally any files. like:

mkdir files
cd files
touch aaa aab aac

and Tab gives me aaa instead of aa

marlonrichert commented 2 years ago

In that scenario, do you press Tab on an empty line? Or what is left of your cursor when you press Tab?

yshui commented 2 years ago

Or what is left of your cursor when you press Tab?

A command that accepts a file, like ls

marlonrichert commented 2 years ago

Thanks, I can reproduce it now. Will fix.

marlonrichert commented 2 years ago

@yshui You're missing a colon. It should be

zstyle ':autocomplete:tab:*' insert-unambiguous yes

or just

zstyle ':autocomplete:*' insert-unambiguous yes
yshui commented 2 years ago

thanks. That fixed it in the MRE environment, but in my actual environment it still doesn't work.

yshui commented 2 years ago

ah, i see. so the behavior is to insert the common prefix if there is one. if the candidates don't have a common prefix, then the first choice is inserted. I was expecting it to insert nothing, as the common prefix would be an empty string.

marlonrichert commented 2 years ago

Do you think that's what most people using insert-unambiguous would want? I don't use it myself, so it's hard for me to judge.

If it worked like that, wouldn't you then be unable to insert anything else than the common prefix? I mean, how would you then insert any completion that's not exactly equal to the common prefix? Would you always use the arrow keys? Or would you just type until you have only one completion left and then press Tab?

yshui commented 2 years ago

Other people could chime in, but IMO tab and arrow key would fit well with these two different kinds of completions - Tab to insert common prefix, and arrow key to select from the menu. I think another option would be using the "tab, tab again" behavior, i.e. tab once to insert common prefix, if the user tapped tab again immediately, enter selection. I think most of the users would be used to this behavior?

marlonrichert commented 2 years ago

I think most of the users would be used to this behavior?

Probably, but the autocompletion makes it a bit trickier. I’ll look into. Pull requests welcome, too, of course. 🙂

marlonrichert commented 2 years ago

Actually, it might already work out of the box like that, if you also add this (before sourcing the plugin):

zstyle ':autocomplete:*' widget-style menu-complete

Can you try that out?

marlonrichert commented 2 years ago

Tested again and I can't reproduce it.