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

Unable to highlight aliases that start with variables #938

Open matan-h opened 9 months ago

matan-h commented 9 months ago

I get to this issue from bash-my-aws, which have this alias in it: alias buckets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma buckets'.

The buckets alias works, and the autocomplete work fine, but for some reason it highlights it as not found.

There is superuser question about it.

Step to reproduce:

echo "echo run" > $HOME/run.sh
chmod +x ~/run.sh
alias run="\$HOME/run.sh"
run
danielshahaf commented 9 months ago

alias buckets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma buckets'.

echo "echo run" > $HOME/run.sh
chmod +x ~/run.sh
alias run="\$HOME/run.sh"
run

z-sy-h has not implemented the full man zshexpn | less -p "brain damage" set of expansions. What it does do is highlight use of an alias according to how the expansion of the alias would be highlighted.

So, you should find that if you literally type $HOME/run.sh at the prompt, it's highlighted the same way "run" is.

Solving this in the general case is going to require some sort of "indeterminate whether this should be green or red" highlighting, due to side effects (e.g., $(foo), or $((++bar)), or ${baz} where baz is special and its getfn() has side effects). Highlighting the specific case of $foo/bar at command position should easy enough, though. In fact, I thought we'd already merged a PR for this... Perhaps it's still open? (It might not mention aliases.)

[Incidentally, "easy enough" does still mean it'd be good to check that that $foo is scalar, is not special, and either $foo has a value or $zsyh_user_options[unset] doesn't have the value that means attempting to expand an unset variable is an error.]

phy1729 commented 8 months ago

In the case of $HOME/run.sh it's because _zsh_highlight_main_highlighter__try_expand_parameter expects the whole word to be a parameter expansion. Handling that wouldn't fix the original issue with ${BMA_HOME:-$HOME/.bash-my-aws} though.