diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 2a2130e..e1ffdae 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -46,39 +46,53 @@ _zsh_highlight_brackets_highlighter_predicate() # Brackets highlighting function. _zsh_highlight_brackets_highlighter() { - local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} - if ((bracket_color_size > 0)); then - local c level pos - typeset -A levelpos lastoflevel matching revmatching - ((level = 0)) - for pos in {1..${#BUFFER}}; do - case $BUFFER[pos] in - "("|"["|"{") - levelpos[$pos]=$((++level)) - lastoflevel[$level]=$pos - ;; - ")"|"]"|"}") - matching[$lastoflevel[$level]]=$pos - revmatching[$pos]=$lastoflevel[$level] - levelpos[$pos]=$((level--)) - ;; - esac - done - for pos in ${(k)levelpos}; do - if [[ -z $matching[$pos] ]] && [[ -z $revmatching[$pos] ]]; then - region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) - else - local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) - region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style]) - fi - done - ((c = CURSOR + 1)) - if [[ -n $levelpos[$c] ]]; then - local otherpos - ((otherpos = -1)) - [[ -n $matching[$c] ]] && otherpos=$matching[$c] - [[ -n $revmatching[$c] ]] && otherpos=$revmatching[$c] - region_highlight+=("$((otherpos - 1)) $otherpos standout") + local level pos + local -A levelpos lastoflevel matching typepos + ((level = 0)) + for ((pos = 1; $pos <= ${#BUFFER}; pos++ )) ; do + case $BUFFER[pos] in + ["([{"]) + levelpos[$pos]=$((++level)) + lastoflevel[$level]=$pos + typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $BUFFER[$pos]` + ;; + [")]}"]) + matching[$lastoflevel[$level]]=$pos + matching[$pos]=$lastoflevel[$level] + levelpos[$pos]=$((level--)) + typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $BUFFER[$pos]` + ;; + ['"'\']) + local quotetype=$BUFFER[$pos] + while (( $pos < ${#BUFFER} )) ; do + ((++pos)) + [[ $BUFFER[$pos] == $quotetype ]] && break + done + ;; + esac + done + for pos in ${(k)levelpos}; do + if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then + region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) + else + local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} + local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) + region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style]) fi + done + ((pos = CURSOR + 1)) + if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then + local otherpos=$matching[$pos] + region_highlight+=("$((otherpos - 1)) $otherpos standout") fi } + +_zsh_highlight_brackets_highlighter_brackettype() +{ + case $1 in + ["()"]) print round;; + ["[]"]) print bracket;; + ["{}"]) print curly;; + *) ;; + esac +}