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: Add highlighting for hashed directories with autocd and cdablevars #931

Open zaidhaan opened 1 year ago

zaidhaan commented 1 year ago

See #930 (or at least just https://github.com/zsh-users/zsh-syntax-highlighting/issues/930#issuecomment-1590633859).

What this PR does:

What this PR does not do:

If this feels like enough to be a PR on its own, then you could merge. If it feels incomplete, but on the right path, someone else could offer guidance and I'll see if I can complete it or they could pick it up themselves, or if it's completely wrong then it could just be closed.

phy1729 commented 11 months ago

I think we can take the CDABLE_VARS documentation fairly literally

          If the argument to a cd command (or an implied cd with the
          AUTO_CD option set) is not a directory, and does not begin with
          a slash, try to expand the expression as if it were preceded by
          a `~' (see the section `Filename Expansion').

and just call _zsh_highlight_main_highlighter_expand_path again, but prefixed with a tilde. Something like

    elif (( autocd && cdablevars )) && [[ ${1[1]} != / ]]; then
      _zsh_highlight_main_highlighter_expand_path "~$1"
      if [[ -d $REPLY && -x $REPLY ]]; then
        REPLY=autodirectory
        return 0
      fi

That covers the case where the path continues and the case where a username is provided.