aboutsummaryrefslogtreecommitdiff
path: root/.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight
diff options
context:
space:
mode:
authorBlista Kanjo2023-08-02 22:22:54 -0400
committerBlista Kanjo2023-08-02 22:22:54 -0400
commit7e72e9ac87fc1c052726202f84a1e16466e06ad4 (patch)
treeeb35478ad142aa154d80d85aa583500c5eab65b8 /.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight
parent08e564a8e1d4c13ad4c046ca4bfc9ca7f72373bd (diff)
feat: `zsh` fast-syntax-highlighting plugin
Diffstat (limited to '.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight')
-rw-r--r--.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight66
1 files changed, 66 insertions, 0 deletions
diff --git a/.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight b/.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight
new file mode 100644
index 0000000..dd8eb4f
--- /dev/null
+++ b/.config/shell/zsh-fast-syntax-highlighting/fast-string-highlight
@@ -0,0 +1,66 @@
+# vim:ft=zsh:sw=4:sts=4
+
+#
+# $1 - PREBUFFER
+# $2 - BUFFER
+#
+function -fast-highlight-string-process {
+ emulate -LR zsh
+ setopt extendedglob warncreateglobal typesetsilent
+
+ local -A pos_to_level level_to_pos pair_map final_pairs
+ local input=$1$2 _mybuf=$1$2 __style __quoting
+ integer __idx=0 __pair_idx __level=0 __start __end
+ local -a match mbegin mend
+
+ pair_map=( "(" ")" "{" "}" "[" "]" )
+
+ while [[ $_mybuf = (#b)[^"{}()[]\\\"'"]#((["({[]})\"'"])|[\\](*))(*) ]]; do
+ [[ -n ${match[3]} ]] && {
+ __idx+=${mbegin[1]}
+
+ [[ $__quoting = \' ]] && _mybuf=${match[3]} || { _mybuf=${match[3]:1}; (( ++ __idx )); }
+ } || {
+ __idx+=${mbegin[1]}
+ [[ -z $__quoting && -z ${_FAST_COMPLEX_BRACKETS[(r)$((__idx-${#PREBUFFER}-1))]} ]] && {
+ if [[ ${match[1]} = ["({["] ]]; then
+ pos_to_level[$__idx]=$(( ++__level ))
+ level_to_pos[$__level]=$__idx
+ elif [[ ${match[1]} = ["]})"] ]]; then
+ if (( __level > 0 )); then
+ __pair_idx=${level_to_pos[$__level]}
+ pos_to_level[$__idx]=$(( __level -- ))
+ [[ ${pair_map[${input[__pair_idx]}]} = ${input[__idx]} ]] && {
+ final_pairs[$__idx]=$__pair_idx
+ final_pairs[$__pair_idx]=$__idx
+ }
+ else
+ pos_to_level[$__idx]=-1
+ fi
+ fi
+ }
+
+ [[ ${match[1]} = \" && $__quoting != \' ]] && { [[ $__quoting = '"' ]] && __quoting="" || __quoting='"'; }
+ [[ ${match[1]} = \' && $__quoting != \" ]] && { [[ $__quoting = "'" ]] && __quoting="" || __quoting="'"; }
+
+ _mybuf=${match[4]}
+ }
+ done
+
+ for __idx in ${(k)pos_to_level}; do
+ (( ${+final_pairs[$__idx]} )) && __style=${FAST_THEME_NAME}bracket-level-$(( ( (pos_to_level[$__idx]-1) % 3 ) + 1 )) || __style=${FAST_THEME_NAME}unknown-token
+ (( __start=__idx-${#PREBUFFER}-1, __end=__idx-${#PREBUFFER}, __start >= 0 )) && \
+ reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
+ done
+
+ # If cursor is on a bracket, then highlight corresponding bracket, if any.
+ if [[ $WIDGET != zle-line-finish ]]; then
+ __idx=$(( CURSOR + 1 ))
+ if (( ${+pos_to_level[$__idx]} )) && (( ${+final_pairs[$__idx]} )); then
+ (( __start=final_pairs[$__idx]-${#PREBUFFER}-1, __end=final_pairs[$__idx]-${#PREBUFFER}, __start >= 0 )) && \
+ reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}paired-bracket]}") && \
+ reply+=("$CURSOR $__idx ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}paired-bracket]}")
+ fi
+ fi
+ return 0
+}