From 1ba2458aee7defc293889f3c1bd4521e96a40d57 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Tue, 10 Nov 2020 16:43:11 -0500 Subject: [PATCH] add cursor-containing-brackets style --- .../brackets/brackets-highlighter.zsh | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index c1577c2..8c9763b 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -36,6 +36,7 @@ : ${ZSH_HIGHLIGHT_STYLES[bracket-level-4]:=fg=yellow,bold} : ${ZSH_HIGHLIGHT_STYLES[bracket-level-5]:=fg=cyan,bold} : ${ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]:=standout} +: ${ZSH_HIGHLIGHT_STYLES[cursor-containing-brackets]:=} # Whether the brackets highlighter should be called or not. _zsh_highlight_highlighter_brackets_predicate() @@ -48,7 +49,7 @@ _zsh_highlight_highlighter_brackets_paint() { local char style local -i bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} buflen=${#BUFFER} escaped_state level=0 matchingpos pos - local -A levelpos lastoflevel matching + local -A containing levelpos lastoflevel matching # Find all brackets and remember which one is matching pos=0 @@ -57,29 +58,37 @@ _zsh_highlight_highlighter_brackets_paint() (( ++pos )) if [[ $char = "\\" ]]; then (( escaped_state = !escaped_state )) + containing[$pos]=$containing[$(( pos - 1 ))] continue fi case $char in ["([{"]) - if (( ! escaped_state )); then + if (( escaped_state )); then + containing[$pos]=$containing[$(( pos - 1 ))] + else levelpos[$pos]=$((++level)) lastoflevel[$level]=$pos + containing[$pos]=$pos fi ;; [")]}"]) if (( escaped_state )); then - : + containing[$pos]=$containing[$(( pos - 1 ))] elif (( level > 0 )); then matchingpos=$lastoflevel[$level] levelpos[$pos]=$((level--)) if _zsh_highlight_brackets_match $matchingpos $pos; then matching[$matchingpos]=$pos matching[$pos]=$matchingpos + containing[$pos]=containing[$(( matchingpos - 1))] fi else levelpos[$pos]=-1 + containing[$pos]=$containing[$(( pos - 1 ))] fi ;; + *) + containing[$pos]=$containing[$(( pos - 1 ))] esac escaped_state=0 done @@ -95,13 +104,25 @@ _zsh_highlight_highlighter_brackets_paint() fi done - # If cursor is on a bracket, then highlight corresponding bracket, if any. if [[ $WIDGET != zle-line-finish ]]; then + # If cursor is on a bracket, then highlight corresponding bracket, if any. pos=$((CURSOR + 1)) if (( $+levelpos[$pos] )) && (( $+matching[$pos] )); then local -i otherpos=$matching[$pos] _zsh_highlight_add_highlight $((otherpos - 1)) $otherpos cursor-matchingbracket fi + if [[ -n $ZSH_HIGHLIGHT_STYLES[cursor-containing-brackets] ]]; then + # If cursor is inside a balanced pair, highlight the pair + pos=$CURSOR + if (( containing[$pos] > 0 )); then + local -i opener=$containing[$pos] + local -i closer=$matching[$opener] + if (( closer )); then + _zsh_highlight_add_highlight $((opener - 1)) $opener cursor-containing-brackets + _zsh_highlight_add_highlight $((closer - 1)) $closer cursor-containing-brackets + fi + fi + fi fi }