Skip to content

Commit 67b33f6

Browse files
committed
main: Highlight {command,process} substitution delimiters
1 parent ac3995f commit 67b33f6

10 files changed

+89
-14
lines changed

highlighters/main/main-highlighter.zsh

+60-14
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@
4040
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
4141
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
4242
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
43-
: ${ZSH_HIGHLIGHT_STYLES[command-substitution]:=fg=magenta}
44-
: ${ZSH_HIGHLIGHT_STYLES[process-substitution]:=fg=magenta}
43+
: ${ZSH_HIGHLIGHT_STYLES[command-substitution]:=none}
44+
: ${ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter]:=fg=magenta}
45+
: ${ZSH_HIGHLIGHT_STYLES[process-substitution]:=none}
46+
: ${ZSH_HIGHLIGHT_STYLES[process-substitution-delimiter]:=fg=magenta}
4547
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
4648
: ${ZSH_HIGHLIGHT_STYLES[double-hyphen-option]:=none}
4749
: ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none}
50+
: ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument-delimiter]:=fg=magenta}
4851
: ${ZSH_HIGHLIGHT_STYLES[single-quoted-argument]:=fg=yellow}
4952
: ${ZSH_HIGHLIGHT_STYLES[double-quoted-argument]:=fg=yellow}
5053
: ${ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]:=fg=yellow}
@@ -103,6 +106,10 @@ _zsh_highlight_main_calculate_fallback() {
103106
double-quoted-argument{-unclosed,}
104107
dollar-quoted-argument{-unclosed,}
105108
back-quoted-argument{-unclosed,}
109+
110+
command-substitution{-delimiter,}
111+
process-substitution{-delimiter,}
112+
back-quoted-argument{-delimiter,}
106113
)
107114
local needle=$1 value
108115
reply=($1)
@@ -852,7 +859,7 @@ _zsh_highlight_main_highlighter_check_path()
852859
# This command will at least highlight $1 to end_pos with the default style
853860
_zsh_highlight_main_highlighter_highlight_argument()
854861
{
855-
local base_style=default i=$1 path_eligible=1 start style
862+
local base_style=default i=$1 path_eligible=1 ret start style
856863
local -a highlights
857864

858865
local -a match mbegin mend
@@ -871,8 +878,16 @@ _zsh_highlight_main_highlighter_highlight_argument()
871878
if [[ $arg[i+1] == $'\x28' ]]; then
872879
(( i += 2 ))
873880
_zsh_highlight_main_highlighter_XXX $(( start_pos + i - 1 )) S $has_end $arg[i,end_pos]
881+
ret=$?
874882
(( i += REPLY ))
875-
highlights+=($(( start_pos + $1 - 1 )) $(( start_pos + i )) process-substitution $reply)
883+
highlights+=(
884+
$(( start_pos + $1 - 1 )) $(( start_pos + i )) process-substitution
885+
$(( start_pos + $1 - 1 )) $(( start_pos + $1 + 1 )) process-substitution-delimiter
886+
$reply
887+
)
888+
if (( ret == 0 )); then
889+
highlights+=($(( start_pos + i - 1 )) $(( start_pos + i )) process-substitution-delimiter)
890+
fi
876891
fi
877892
esac
878893

@@ -906,8 +921,16 @@ _zsh_highlight_main_highlighter_highlight_argument()
906921
start=$i
907922
(( i += 2 ))
908923
_zsh_highlight_main_highlighter_XXX $(( start_pos + i - 1 )) S $has_end $arg[i,end_pos]
924+
ret=$?
909925
(( i += REPLY ))
910-
highlights+=($(( start_pos + start - 1)) $(( start_pos + i )) command-substitution $reply)
926+
highlights+=(
927+
$(( start_pos + start - 1)) $(( start_pos + i )) command-substitution
928+
$(( start_pos + start - 1)) $(( start_pos + start + 1)) command-substitution-delimiter
929+
$reply
930+
)
931+
if (( ret == 0 )); then
932+
highlights+=($(( start_pos + i - 1)) $(( start_pos + i )) command-substitution-delimiter)
933+
fi
911934
continue
912935
fi
913936
while [[ $arg[i+1] == [\^=~#+] ]]; do
@@ -921,8 +944,16 @@ _zsh_highlight_main_highlighter_highlight_argument()
921944
start=$i
922945
(( i += 2 ))
923946
_zsh_highlight_main_highlighter_XXX $(( start_pos + i - 1 )) S $has_end $arg[i,end_pos]
947+
ret=$?
924948
(( i += REPLY ))
925-
highlights+=($(( start_pos + start - 1)) $(( start_pos + i )) process-substitution $reply)
949+
highlights+=(
950+
$(( start_pos + start - 1)) $(( start_pos + i )) process-substitution
951+
$(( start_pos + start - 1)) $(( start_pos + start + 1 )) process-substitution-delimiter
952+
$reply
953+
)
954+
if (( ret == 0 )); then
955+
highlights+=($(( start_pos + i - 1)) $(( start_pos + i )) process-substitution-delimiter)
956+
fi
926957
continue
927958
fi
928959
;|
@@ -987,7 +1018,7 @@ _zsh_highlight_main_highlighter_highlight_double_quote()
9871018
{
9881019
local -a match mbegin mend saved_reply
9891020
local MATCH; integer MBEGIN MEND
990-
local i j k style
1021+
local i j k ret style
9911022
reply=()
9921023

9931024
for (( i = $1 + 1 ; i <= end_pos - start_pos ; i += 1 )) ; do
@@ -1021,8 +1052,17 @@ _zsh_highlight_main_highlighter_highlight_double_quote()
10211052
(( i += 2 ))
10221053
saved_reply=($reply)
10231054
_zsh_highlight_main_highlighter_XXX $(( start_pos + i - 1 )) S $has_end $arg[i,end_pos]
1055+
ret=$?
10241056
(( i += REPLY ))
1025-
reply=($saved_reply $j $(( start_pos + i )) command-substitution $reply)
1057+
reply=(
1058+
$saved_reply
1059+
$j $(( start_pos + i )) command-substitution
1060+
$j $(( j + 2 )) command-substitution-delimiter
1061+
$reply
1062+
)
1063+
if (( ret == 0 )); then
1064+
reply+=($(( start_pos + i - 1 )) $(( start_pos + i )) command-substitution-delimiter)
1065+
fi
10261066
continue
10271067
else
10281068
continue
@@ -1123,11 +1163,9 @@ _zsh_highlight_main_highlighter_highlight_backtick()
11231163
# offset is a count of consumed \ (the delta between buf and arg).
11241164
# offsets is an array indexed by buf offset of when the delta between buf and arg changes.
11251165
# It is sparse, so search backwards to the last value
1126-
# unclosed is an array of one highlight to append to reply if this back-quoted-argument
1127-
# is closed and there is an unclosed back-quoted-argument in buf.
1128-
local buf highlight style=back-quoted-argument-unclosed
1166+
local buf highlight style=back-quoted-argument-unclosed style_end
11291167
local -i arg1=$1 end_ i=$1 last offset=0 start subshell_has_end=0
1130-
local -a highlight_zone highlights offsets unclosed
1168+
local -a highlight_zone highlights offsets
11311169
reply=()
11321170

11331171
last=$(( arg1 + 1 ))
@@ -1154,6 +1192,7 @@ _zsh_highlight_main_highlighter_highlight_backtick()
11541192
fi
11551193
else # it's an unquoted ` and this is the end
11561194
style=back-quoted-argument
1195+
style_end=back-quoted-argument-delimiter
11571196
buf=$buf$arg[last,i-1]
11581197
offsets[i-arg1-offset]='' # So we never index past the end
11591198
break
@@ -1170,11 +1209,18 @@ _zsh_highlight_main_highlighter_highlight_backtick()
11701209
highlights+=($start $end_ $highlight)
11711210
if [[ $highlight == back-quoted-argument-unclosed && $style == back-quoted-argument ]]; then
11721211
# An inner backtick command substitution is unclosed, but this level is closed
1173-
unclosed=($(( start_pos + i - 1)) $(( start_pos + i )) unknown-token)
1212+
style_end=unknown-token
11741213
fi
11751214
done
11761215

1177-
reply=($(( start_pos + arg1 - 1 )) $(( start_pos + i )) $style $highlights $unclosed)
1216+
reply=(
1217+
$(( start_pos + arg1 - 1 )) $(( start_pos + i )) $style
1218+
$(( start_pos + arg1 - 1 )) $(( start_pos + arg1 )) back-quoted-argument-delimiter
1219+
$highlights
1220+
)
1221+
if (( $#style_end )); then
1222+
reply+=($(( start_pos + i - 1)) $(( start_pos + i )) $style_end)
1223+
fi
11781224
REPLY=$i
11791225
}
11801226

highlighters/main/test-data/back-quoted-argument.zsh

+7
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,24 @@ expected_region_highlight=(
3333
"1 4 builtin" # echo
3434
"6 18 default" # `echo \`42\``
3535
"6 18 back-quoted-argument" # `echo \`42\``
36+
"6 6 back-quoted-argument-delimiter" # `
3637
"7 10 builtin" # echo
3738
"12 17 default" # \`42\`
3839
"12 17 back-quoted-argument" # \`42\`
40+
"12 13 back-quoted-argument-delimiter" # \`
3941
"14 15 unknown-token" # 42
42+
"16 17 back-quoted-argument-delimiter" # \`
43+
"18 18 back-quoted-argument-delimiter" # `
4044
"20 39 default" # "is `echo equal` to"
4145
"20 39 double-quoted-argument" # "is `echo equal` to"
4246
"24 35 back-quoted-argument" # `echo equal`
47+
"24 24 back-quoted-argument-delimiter" # `
4348
"25 28 builtin" # echo
4449
"30 34 default" # equal
50+
"35 35 back-quoted-argument-delimiter" # `
4551
"41 55 default" # `echo 6 times 9
4652
"41 55 back-quoted-argument-unclosed" # `echo 6 times 9
53+
"41 41 back-quoted-argument-delimiter" # `
4754
"42 45 builtin" # echo
4855
"47 47 default" # 6
4956
"49 53 default" # times

highlighters/main/test-data/command-substitution-in-assignment.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ expected_region_highlight=(
3434
'1 15 assign' # foo=$(echo bar)
3535
'5 15 default' # $(echo bar)
3636
'5 15 command-substitution' # $(echo bar)
37+
'5 6 command-substitution-delimiter' # $(
3738
'7 10 builtin' # echo
3839
'12 14 default' # bar
40+
'15 15 command-substitution-delimiter' # )
3941
'17 17 builtin' # :
4042
)

highlighters/main/test-data/command-substitution-unclosed.zsh

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ expected_region_highlight=(
3434
'1 1 builtin' # :
3535
'3 15 default' # foo$(echo bar
3636
'6 15 command-substitution' # $(echo bar
37+
'6 7 command-substitution-delimiter' # $(
3738
'8 11 builtin' # echo
3839
'13 15 default' # bar
3940
)

highlighters/main/test-data/cthulhu.zsh

+8
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,35 @@ expected_region_highlight=(
3636
'1 4 builtin' # echo
3737
'6 113 default' # Ph\'ng`echo lui "mg"\`echo lw\'nafh \\\`echo Cthu"lhu\\\` R\\'ly$(echo eh wag\\\`echo h\'nag\\\`'l' fht)agn`
3838
'12 113 back-quoted-argument' # `echo lui "mg"\`echo lw\'nafh \\\`echo Cthu"lhu\\\` R\\'ly$(echo eh wag\\\`echo h\'nag\\\`'l' fht)agn`
39+
'12 12 back-quoted-argument-delimiter' # `
3940
'13 16 builtin' # echo
4041
'18 20 default' # lui
4142
'22 112 default' # "mg"\`echo lw\'nafh \\\`echo Cthu"lhu\\\` R\\'ly$(echo eh wag\\\`echo h\'nag\\\`'l' fht)agn
4243
'22 25 double-quoted-argument' # "mg"
4344
'26 112 back-quoted-argument-unclosed' # \`echo lw\'nafh \\\`echo Cthu"lhu\\\` R\\'ly$(echo eh wag\\\`echo h\'nag\\\`'l' fht)agn
45+
'26 27 back-quoted-argument-delimiter' # \`
4446
'28 31 builtin' # echo
4547
'33 40 default' # lw\'nafh
4648
'42 62 default' # \\\`echo Cthu"lhu\\\`
4749
'42 62 back-quoted-argument' # \\\`echo Cthu"lhu\\\`
50+
'42 45 back-quoted-argument-delimiter' # \\\`
4851
'46 49 builtin' # echo
4952
'51 58 default' # Cthu"lhu
5053
'55 58 double-quoted-argument-unclosed' # "lhu
54+
'59 62 back-quoted-argument-delimiter' # \\\`
5155
'64 112 default' # R\\'ly$(echo eh wag\\\`echo h\'nag\\\`'l' fht)agn
5256
'70 109 command-substitution' # $(echo eh wag\\\`echo h\'nag\\\`'l' fht)
57+
'70 71 command-substitution-delimiter' # $(
5358
'72 75 builtin' # echo
5459
'77 78 default' # eh
5560
'80 104 default' # wag\\\`echo h\'nag\\\`'l'
5661
'83 101 back-quoted-argument' # \\\`echo h\'nag\\\`
62+
'83 86 back-quoted-argument-delimiter' # \\\`
5763
'87 90 builtin' # echo
5864
'92 97 default' # h\'nag
65+
'98 101 back-quoted-argument-delimiter' # \\\`
5966
'102 104 single-quoted-argument' # 'l'
6067
'106 108 default' # fht
68+
'109 109 command-substitution-delimiter' # )
6169
'113 113 unknown-token' # `
6270
)

highlighters/main/test-data/dollar-paren.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ expected_region_highlight=(
3535
'3 8 default' # "$(:)"
3636
'3 8 double-quoted-argument' # "$(:)"
3737
'4 7 command-substitution' # $(:)
38+
'4 5 command-substitution-delimiter' # $(
3839
'6 6 builtin' # :
40+
'7 7 command-substitution-delimiter' # )
3941
)

highlighters/main/test-data/process-substitution.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ expected_region_highlight=(
3434
'1 1 builtin' # :
3535
'3 19 double-hyphen-option' # --foo=<(echo bar)
3636
'9 19 process-substitution' # <(echo bar)
37+
'9 10 process-substitution-delimiter' # <(
3738
'11 14 builtin' # echo
3839
'16 18 default' # bar
40+
'19 19 process-substitution-delimiter' # )
3941
'21 26 default' # "<(:)"
4042
'21 26 double-quoted-argument' # "<(:)"
4143
)

highlighters/main/test-data/process-substitution2.zsh

+3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ expected_region_highlight=(
3434
'1 4 builtin' # echo
3535
'6 9 default' # =(:)
3636
'6 9 process-substitution' # =(:)
37+
'6 7 process-substitution-delimiter' # =(
3738
'8 8 builtin' # :
39+
'9 9 process-substitution-delimiter' # )
3840
'11 15 default' # a=(:)
3941
'17 26 default' # =(echo foo
4042
'17 26 process-substitution' # =(echo foo
43+
'17 18 process-substitution-delimiter' # =(
4144
'19 22 builtin' # echo
4245
'24 26 default' # foo
4346
)

highlighters/main/test-data/redirection-in-cmdsubst.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ expected_region_highlight=(
3434
'1 1 builtin' # :
3535
'3 9 default' # $(<foo)
3636
'3 9 command-substitution' # $(<foo)
37+
'3 4 command-substitution-delimiter' # $(
3738
'5 5 redirection' # <
3839
'6 8 default' # foo
40+
'9 9 command-substitution-delimiter' # )
3941
)

highlighters/main/test-data/redirection2.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ expected_region_highlight=(
3333
"1 2 command" # ls
3434
"4 8 default" # >(wc)
3535
"4 8 process-substitution" # >(wc)
36+
"4 5 process-substitution-delimiter" # >(
3637
"6 7 command" # wc
38+
"8 8 process-substitution-delimiter" # )
3739
"10 10 commandseparator" # |
3840
"12 13 command" # nl
3941
)

0 commit comments

Comments
 (0)