zsh-syntax-highlighting icon indicating copy to clipboard operation
zsh-syntax-highlighting copied to clipboard

Syntax highlighting seemingly not expanding variable substitutions in aliases

Open johnsonjh opened this issue 4 years ago • 5 comments
trafficstars

I override vi, vim, and nvim with a local version (if available).

The following is the relevant segments from .zshrc:

export VARERR='Error: Variable is not set.'
export EDITOR="/opt/nvim/bin/nvim"
if [ -x "${EDITOR:-}" ]; then        
    alias   nvim="\${EDITOR:?\${VARERR}}"
    alias    vim="\${EDITOR:?\${VARERR}}"
    alias     vi="\${EDITOR:?\${VARERR}}"
fi  

If the local nvim exists, the alias table is set as so:

» whence -va nvim vim vi
nvim is an alias for ${EDITOR:?${VARERR}}
nvim is /usr/bin/nvim
vim is an alias for ${EDITOR:?${VARERR}}
vim is /usr/bin/vim
vi is an alias for ${EDITOR:?${VARERR}}
vi is /usr/bin/vi

This causes zsh-syntax-highlighting to color vi in red, erroneously.

If the alias is more "conventional", then it works as expected, coloring vi in green: Screenshot I apologize in advance if this is not expected to work, or if it has already been reported.

I did a cursory search but did not see a similar issue.

johnsonjh avatar Feb 22 '21 01:02 johnsonjh

This is morally the same as #771. z-sy-h will look though a small subset of parameter substitutions and doesn't at present distinguish between a known syntax error and parameter expansions it doesn't know how to look through safely.

phy1729 avatar Feb 22 '21 01:02 phy1729

I think an acceptable way to handle this (at least to me) would be to color aliases that exist, but are not simply parsable as yellow instead of red.

This seems an acceptable compromise, as it wouldn't make me do a double-take, thinking there is an error when there is not, and would be much easier to implement than complete substitution and expansion logic.

johnsonjh avatar Feb 27 '21 08:02 johnsonjh

Yeah, that's our thinking too, as mentioned on the referenced issues. Interested in writing a patch for this? Some cases of variable expansion at command position are already handled; all that's needed is to have unhandled cases set some new style.

danielshahaf avatar Feb 27 '21 09:02 danielshahaf

I might be able to write a decent patch... I'll give it a shot once I'm in front of a "real" computer, which should be sometime next week, unless someone else gets to it first.

johnsonjh avatar Feb 28 '21 05:02 johnsonjh

Thanks, @johnsonjh. If you have questions about the approach or the details, do ask. And don't forget docs and tests :-)

danielshahaf avatar Feb 28 '21 07:02 danielshahaf