zsh-users / zsh-syntax-highlighting

Fish shell like syntax highlighting for Zsh.
github.com/zsh-users/zsh-syntax-highlighting
BSD 3-Clause "New" or "Revised" License
19.5k stars 1.32k forks source link

main highlighter does not highlight hashed directories with autocd and cdablevars set #930

Open zaidhaan opened 1 year ago

zaidhaan commented 1 year ago

Screenshot: image

I was able to fix it with this

diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh
index f691051..505fe3c 100644
--- a/highlighters/main/main-highlighter.zsh
+++ b/highlighters/main/main-highlighter.zsh
@@ -196,6 +196,8 @@ _zsh_highlight_main__type() {
       REPLY=builtin
     elif (( $+commands[(e)$1] )); then
       REPLY=command
+    elif (( $+nameddirs[(e)$1] )) && [[ -x $nameddirs[$1] ]]; then
+      REPLY=hashed
     # None of the special hashes had a match, so fall back to 'type -w', for
     # forward compatibility with future versions of zsh that may add new command
     # types.

And get these results: image

Though I'm not sure if that's the right approach, or whether that addresses it in the right place.

Additionally, cdablevars appears to also not be respected (not sure whether this should be moved to a new issue but these both seem related so I'll keep it here) image

Even with my patch above, it only is able to partially addresses this, since it appears that after entering into that directory, only then does it update the hash table (and once the hash table is updated, it gets highlighted, as shown below) image


phy1729 commented 1 year ago

I think the right place to add the check would be in _zsh_highlight_main_highlighter_check_path where autocd is handled currently. To check the user's cdablevars setting you can use the zsyh_user_options array as done at the top of that function for autocd.

zaidhaan commented 1 year ago

Ah, thanks. Looks like my fix can do the following only:

That addresses half of what this issue presents, since now just the hashed directory will be highlighted, as expected image

But if you hit tab and allow it to expand into a path, then it does not get highlighted as expected image

image

It feels like this could be made easier if _zsh_highlight_main_highlighter_expand_path handled this instead, but I'm not sure if expansion alone can resolve a hashed directory name

$ _zsh_highlight_main_highlighter_expand_path ~repos/autowikibrowser && echo $REPLY
/home/zai/dev/repos/autowikibrowser

$ _zsh_highlight_main_highlighter_expand_path repos/autowikibrowser && echo $REPLY 
repos/autowikibrowser

Anyhow, I think I'll PR the fix I described above. If it's on the right path, it could be merged. If it's considered incomplete, then it could be worked on further. And if the approach is wrong (e.g. needs to be addressed in _zsh_highlight_main_highlighter_expand_path instead), then it can be closed.