marlonrichert / zsh-autocomplete

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

Unnecessary tilde expansions supersede directory and file completions #741

Open aaronkollasch opened 4 months ago

aaronkollasch commented 4 months ago

Environment

% typeset -p1 VENDOR OSTYPE ZSH_PATCHLEVEL _autocomplete__funcfiletrace
typeset VENDOR=apple
typeset OSTYPE=darwin23.0
typeset ZSH_PATCHLEVEL=zsh-5.9-0-g73d3173
typeset -a _autocomplete__funcfiletrace=(
  /Users/aaron/.dotfiles/zsh/repos/zsh-autocomplete/zsh-autocomplete.plugin.zsh:4
  /Users/aaron/.dotfiles/zsh/config.d/autocomplete.zsh:28
  /Users/aaron/.zshrc:21
  zsh:0
)
% git -C ~autocomplete log --oneline -n1
cfc3fd9 (HEAD, origin/main, origin/HEAD) Fix grammar mistakes in Readme

Steps to reproduce

% cd $(mktemp -d)
% git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git
<output>
% > .zshrc <<EOF
setopt interactivecomments transientrprompt
PS1='%# '
PS2=
RPS2='%^'
source $PWD/zsh-autocomplete/zsh-autocomplete.plugin.zsh
mkdir -p aaa_1/bbb aaa_2
touch aaa_1/test_1.txt aaa_1/test_2.txt aaa_2/test_1.txt aaa_2/test_2.txt
zstyle ':autocomplete:*' insert-unambiguous yes
zstyle ':autocomplete:*' widget-style menu-select
bindkey '\t' menu-select "$terminfo[kcbt]" menu-select
bindkey -M menuselect '\t' menu-select "$terminfo[kcbt]" reverse-menu-complete
EOF
% env -i HOME=$PWD PATH=$PATH TERM=$TERM ${TERMINFO:+TERMINFO=$TERMINFO} zsh -d
% ls ~/a
expansion
/var/folders/1g/c3dw8nsj7gs0d1dlsb3gcw340000gn/T/tmp.pcKQSBX21Z/a
directory
aaa_1/                           aaa_2/

So the first suggestion is an expansion of ~ followed by the portion of the subdirectory. Because the expansion takes priority, it takes two tabs to get to the first directory completion instead of one. The problem repeats, as if I accept ~/aaa_1 by typing '/', the new first completion is again the tilde expansion, and it's the full path of aaa_1, the folder I've already entered!

% ls ~/aaa_1/
expansion
/var/folders/1g/c3dw8nsj7gs0d1dlsb3gcw340000gn/T/tmp.pcKQSBX21Z/aaa_1/
directory
bbb/
file
test_1.txt                          test_2.txt

This means that to efficiently complete ~/* paths, I must first expand the ~, which clutters up the terminal and requires extra keystrokes.

Contents of ~autocomplete-log/YYYY-MM-DD.log (click to expand)

Potential solution

Using bisection, I traced the issue to commit 06431898a04cbe9d2e128a1b66b84905a8c1de69.

The solution I came up with was to expand the first path component of $word if it begins with ~, before comparing with the full $expansion.

  expansion="${(b)$( eval print -r -- $word )}" 2> /dev/null ||
      return 1
+ if [[ $word == \~* ]]; then
+    word="${(b)$(eval print -r -- ${(M)word#*/})}${(b)word#*/}" 2> /dev/null ||
+         return 1
+ fi
  [[ $expansion == $word ]] &&
      return 1

It feels a bit hack-y but I'm not sure if there's a better way, such as directly disabling tilde expansion in eval or something.

Note that due to the way I used patterns here, completing ~ still suggests the expansion into $HOME as the first result, it just goes away for ~/ or ~/a... and beyond. Seems like a useful middle ground for my purposes.

See this commit for the diff: https://github.com/aaronkollasch/zsh-autocomplete/commit/da40b542b223f8519d0bcce4fd9d4beb96f62301

aaronkollasch commented 3 months ago

Note that there is a related issue involving expansion suggestions for backslash-escaped words/paths as well as quoted words. This interferes with completing paths containing spaces, as the first suggestion is always to remove the escapes (which would break the path).

thearrow commented 3 months ago

Your fix works for me as well - was getting annoyed by this. Thanks!

4rtemis-4rrow commented 3 months ago

@aaronkollasch I was about to open an issue for that, I was just checking if there is already an issue for this?

is there any temporary fix? a lot of my paths contain spaces, so it's getting very annoying

aaronkollasch commented 3 months ago

@4rtemis-4rrow Here's a temporary fix for the spaces issue - It's not perfect in covering all the edge cases that could happen with quotations and escapes, but it does reduce the annoyance level: https://github.com/aaronkollasch/zsh-autocomplete/commit/d53d90dd205b3ef66101d4cf8692c8518d4daf61