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

Common root logic is ignored #23

Closed
tekumara opened this issue Jun 27, 2020 · 5 comments
Closed

Common root logic is ignored #23

tekumara opened this issue Jun 27, 2020 · 5 comments
Assignees

Comments

@tekumara
Copy link

tekumara commented Jun 27, 2020

When there's a common root, z chooses the shortest matching directory as I expect:

$ cat ~/.z
/Users/tekumara/code/setup|1250.4200000000001|1593232602
/Users/tekumara/code|223.405|1593230323

$ z code -l
common:    /Users/tekumara/code
737.07     /Users/tekumara/code
5002.87    /Users/tekumara/code/setup

However, the common root logic isn't applied in the following case, which gives the surprising behaviour of choosing the more frequent directory:

$ cd ~/.vscode/extensions
$ cat ~/.z
/Users/tekumara/code/setup|1252.4200000000001|1593232621
/Users/tekumara/code|223.405|1593230323
/Users/tekumara/.vscode/extensions|1|1593232655

$ z code -l
7.98       /Users/tekumara/.vscode/extensions
733.93     /Users/tekumara/code
4983.06    /Users/tekumara/code/setup

I was still expecting /Users/tekumara/code to win, despite the addition of the vscode directory.

FYI: rupa/z has the same behaviour.

tekumara added a commit to tekumara/setup that referenced this issue Jun 27, 2020
zoxide doesn't match on parent directories
ajeetdsouza/zoxide#90

zshz doesn't choose shortest directory in some cases
agkozak/zsh-z#23

zoxide is faster

$ repeat 10 {time (zshz --add /tmp)}
( zshz --add /tmp; )  0.01s user 0.00s system 75% cpu 0.019 total
( zshz --add /tmp; )  0.01s user 0.00s system 93% cpu 0.014 total
( zshz --add /tmp; )  0.01s user 0.00s system 92% cpu 0.014 total
( zshz --add /tmp; )  0.01s user 0.00s system 92% cpu 0.016 total
( zshz --add /tmp; )  0.01s user 0.00s system 92% cpu 0.016 total
( zshz --add /tmp; )  0.01s user 0.00s system 92% cpu 0.016 total
( zshz --add /tmp; )  0.01s user 0.00s system 90% cpu 0.015 total
( zshz --add /tmp; )  0.01s user 0.00s system 91% cpu 0.016 total
( zshz --add /tmp; )  0.01s user 0.00s system 92% cpu 0.016 total
( zshz --add /tmp; )  0.01s user 0.00s system 92% cpu 0.015 total

$ repeat 10 {time (zoxide add /tmp)}
( zoxide add /tmp; )  0.00s user 0.00s system 49% cpu 0.009 total
( zoxide add /tmp; )  0.00s user 0.00s system 73% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 74% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 73% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 72% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 72% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 72% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 71% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 73% cpu 0.004 total
( zoxide add /tmp; )  0.00s user 0.00s system 74% cpu 0.004 total

repeat 10 {time (z /tmp -e > /dev/null)}
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 95% cpu 0.016 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 96% cpu 0.014 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 95% cpu 0.014 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 95% cpu 0.014 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 96% cpu 0.014 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 96% cpu 0.014 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 95% cpu 0.013 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 95% cpu 0.013 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 97% cpu 0.014 total
( zshz /tmp -e 2>&1 > /dev/null; )  0.01s user 0.00s system 96% cpu 0.014 total

repeat 10 {time (zq /tmp > /dev/null)}
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 67% cpu 0.004 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 69% cpu 0.004 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 73% cpu 0.004 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 70% cpu 0.003 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 69% cpu 0.003 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 71% cpu 0.004 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 68% cpu 0.004 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 69% cpu 0.004 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 74% cpu 0.003 total
( zoxide query /tmp > /dev/null; )  0.00s user 0.00s system 70% cpu 0.004 total
@agkozak agkozak self-assigned this Jun 27, 2020
@agkozak
Copy link
Owner

agkozak commented Jun 27, 2020

You're quite right - that is the behavior of rupa/z, and since zsh-z is for the most part a drop-in replacement for rupa/z, the results should be the same.

The underlying reason is that in the second instance, where you're searching for code and the possible matches are

/Users/tekumara/.vscode/extensions
/Users/tekumara/code
/Users/tekumara/code/setup

there is not really a common root. There would only be a common root if all the matches started with /Users/tekumara/code. The presence of a possible match that is not "common" means that the "common directory" logic does not apply.

I am working on an alternate way for zsh-z to pick out the best match in the no-subdirectories branch, which I encourage you to switch to and try out. You can read about its development here, but the basic idea is that if the best match is /Users/tekumara/code/setup and you'd typed z code, you'd end up in /Users/tekumara/code -- the experimental code just drops any subdirectories of the best match that do not contain the search text in them. If you'd typed z set, then, and the best match had also been /Users/tekumara/code/setup, you'd end up in /Users/tekumara/code/setup, since set was what you were looking for.

The new behavior appears to work well, and I've got a number of people testing it out. If it continues to be a success, I'll merge it into the master branch as an alternative method that can be enabled by setting an environment variable. Try it and let me know what you think.

@agkozak
Copy link
Owner

agkozak commented Jun 27, 2020

Actually, I've decided that the new feature is mature enough to be on the master branch. So use that, but include

ZSHZ_UNCOMMON=1

in your .zshrc.

@tekumara
Copy link
Author

Thanks @agkozak I'm using zsh-z.plugin.zsh on master (0d1f65a) but don't see any differences in behaviour:

$ echo $ZSHZ_UNCOMMON
1

$ z code -l
7.91       /Users/tekumara/.vscode/extensions
150.49     /Users/tekumara/code
856.32     /Users/tekumara/code/setup
$ echo $ZSHZ_UNCOMMON


$ z code -l
7.91       /Users/tekumara/.vscode/extensions
150.49     /Users/tekumara/code
856.30     /Users/tekumara/code/setup

@agkozak
Copy link
Owner

agkozak commented Jun 28, 2020

The new, alternate behavior (when you set ZSHZ_UNCOMMON=1) doesn't change the ranking of the possible matches. It changes where you end up if you simply type z code.

In your example, if you set ZSHZ_UNCOMMON=1 and type z code, ZSH-z picks the highest-ranking match (Users/tekumara/code/setup) and drops the /setup subdirectory (ZSH-z will keep dropping subdirectories until it arrives at the text you were actually searching for). So you end up in /Users/tekumara/code,

If you use the default "common prefix" method, well -- there is no common prefix. You only have a common prefix when all of the possible matches start with the same letters, and the presence of /Users/tekumara/.vscode/extensions in the database prevents that from happening. So there's no common directory to jump to, and ZSH-z simply sends you to the highest-ranking match: /Users/tekumara/code/setup.

@tekumara
Copy link
Author

OIC thanks for helping me understand! I didn't realise the new logic was applied on top of the scoring.

So as you say, with ZSHZ_UNCOMMON=1, then z code takes me to /Users/tekumara/code as I'd like instead of /Users/tekumara/code/setup.

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

No branches or pull requests

2 participants