forked from zdharma-continuum/fast-syntax-highlighting
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fast-string-highlight
77 lines (69 loc) · 3.05 KB
/
fast-string-highlight
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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
if [[ -n ${match[4]} ]] {
__idx+=${mbegin[2]}
[[ $__quoting = \' ]] && _mybuf=${match[4]} || { _mybuf=${match[4]:1}; (( ++ __idx )); }
} else {
__idx+=${mbegin[2]}
[[ -z $__quoting && -z ${_FAST_COMPLEX_BRACKETS[(r)$((__idx-${#PREBUFFER}-1))]} ]] && {
if [[ ${match[2]} = ["({["] ]]; then
pos_to_level[$__idx]=$(( ++__level ))
level_to_pos[$__level]=$__idx
elif [[ ${match[2]} = ["]})"] ]]; 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
}
if [[ ${match[2]} = \" && $__quoting != \' ]] {
[[ $__quoting = '"' ]] && __quoting="" || __quoting='"';
}
if [[ ${match[2]} = \' && $__quoting != \" ]] {
if [[ $__quoting = ("'"|"$'") ]] {
__quoting=""
} else {
if [[ $match[1] = *\$ ]] {
__quoting="\$'";
} else {
__quoting="'";
}
}
}
_mybuf=${match[5]}
}
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
}