zdharma-continuum / fast-syntax-highlighting

Feature-rich syntax highlighting for ZSH
BSD 3-Clause "New" or "Revised" License
1.06k stars 76 forks source link

fix: correct highlighting logic for global aliases #26

Closed QuarticCat closed 1 year ago

QuarticCat commented 2 years ago

I have some global aliases. All of them start with a :. Particularly, one of them is named :bg!.

alias -g :bg!='&>/dev/null &!'

I also wrote a completion for them.

compdef _galiases -first-
_galiases() {
    if [[ $PREFIX == :* ]]; then
        local des
        for k v ("${(@kv)galiases}") des+=("${k//:/\\:}:alias -g '$v'")
        _describe 'alias' des
    fi
}

This completion will automatically escape my selection, e.g., :bg! -> :bg\!. This totally makes sense since sometimes we may choose a completion content that contains some special characters like a filename containing a \n. And ! will be escaped because I enabled history expansion, which gives ! some special meanings in some contexts.

Here comes the problem, :bg\! is a valid global alias and will be expanded by ZSH. However, f-sy-h cannot correctly highlight it.

ZSH provides a flag (Q) that can remove an escape. So simply replace ${+galiases[(e)$xxx]} with ${+galiases[(e)${(Q)xxx}]} in file /fast-highlight then it seems work. I don't know if I should also modify chroma/-alias.ch and other places.

vladdoster commented 2 years ago

Hi @QuarticCat, thanks for opening a PR!

Could you provide a screenshot of the issue and this fix?

QuarticCat commented 2 years ago

Preparation:

$ alias -g :bg!='&>/dev/null &!'

Before:

image

After:

image

CC @vladdoster

vladdoster commented 2 years ago

@QuarticCat What version of Zsh are you using?

QuarticCat commented 2 years ago

@vladdoster 5.8.1

vladdoster commented 2 years ago

I have a hunch the current checks aren't super accurate :/.

Screen Shot 2022-04-24 at 15 45 25

Would you be up to writing a unit test for this change? Either in Zunit (example of zunit) or the existing test framework in this repository? I assume it will be consistent results across systems and a handful of Zsh versions, but don't want to test by hand.

Have you run the zinit tests locally?

QuarticCat commented 2 years ago

No. I don't know how to write a unit test for it. And I'm not keen on learning how to do it cuz I'm not even familiar with ZSH.