Skip to content

Commit a2a1e13

Browse files
committed
'main': Pass parameters through the multi-word machinery, as we already do for aliases.
Fixes #674.
1 parent 52ea5c6 commit a2a1e13

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ _zsh_highlight_main_add_region_highlight() {
7777
[[ $1 == unknown-token ]] && alias_style=unknown-token
7878
return
7979
fi
80+
if (( in_param )); then
81+
[[ $1 == unknown-token ]] && param_style=unknown-token
82+
return
83+
fi
8084

8185
# The calculation was relative to $buf but region_highlight is relative to $BUFFER.
8286
(( start += buf_offset ))
@@ -406,11 +410,13 @@ _zsh_highlight_main_highlighter_highlight_list()
406410
# alias_style is the style to apply to an alias once in_alias=0
407411
# Usually 'alias' but set to 'unknown-token' if any word expanded from
408412
# the alias would be highlighted as unknown-token
409-
local alias_style arg buf=$4 highlight_glob=true style
413+
# param_style is analogous for parameter expansions
414+
local alias_style param_style arg buf=$4 highlight_glob=true style
410415
local in_array_assignment=false # true between 'a=(' and the matching ')'
411416
# in_alias is equal to the number of shifts needed until arg=args[1] pops an
412417
# arg from BUFFER and not added by an alias.
413-
integer in_alias=0 len=$#buf
418+
# in_param is analogous for parameter expansions
419+
integer in_alias=0 in_param=0 len=$#buf
414420
local -a match mbegin mend list_highlights
415421
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
416422
local -A seen_alias
@@ -489,6 +495,14 @@ _zsh_highlight_main_highlighter_highlight_list()
489495
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
490496
fi
491497
fi
498+
if (( in_param )); then
499+
(( in_param-- ))
500+
if (( in_param == 0 )); then
501+
# start_pos and end_pos are of the alias (previous $arg) here
502+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-"unknown-token"}
503+
param_style=""
504+
fi
505+
fi
492506

493507
# Initialize this_word and next_word.
494508
if (( in_redirection == 0 )); then
@@ -513,7 +527,7 @@ _zsh_highlight_main_highlighter_highlight_list()
513527
fi
514528
fi
515529

516-
if (( in_alias == 0 )); then
530+
if (( in_alias == 0 && in_param == 0 )); then
517531
# Compute the new $start_pos and $end_pos, skipping over whitespace in $buf.
518532
[[ "$proc_buf" = (#b)(#s)(([ $'\t']|\\$'\n')#)* ]]
519533
# The first, outer parenthesis
@@ -626,6 +640,7 @@ _zsh_highlight_main_highlighter_highlight_list()
626640
local -a match mbegin mend
627641
local MATCH; integer MBEGIN MEND
628642
local parameter_name
643+
local -a words
629644
if [[ $arg[1] == '$' ]] && [[ ${arg[2]} == '{' ]] && [[ ${arg[-1]} == '}' ]]; then
630645
parameter_name=${${arg:2}%?}
631646
elif [[ $arg[1] == '$' ]]; then
@@ -638,14 +653,16 @@ _zsh_highlight_main_highlighter_highlight_list()
638653
# Set $arg.
639654
case ${(tP)MATCH} in
640655
(*array*|*assoc*)
641-
local -a words; words=( ${(P)MATCH} )
642-
arg=${words[1]}
656+
words=( ${(P)MATCH} )
643657
;;
644658
(*)
645659
# scalar, presumably
646-
arg=${(P)MATCH}
660+
words=( ${(P)MATCH} )
647661
;;
648662
esac
663+
(( in_param = 1 + $#words ))
664+
args=( $words $args )
665+
arg=$args[1]
649666
_zsh_highlight_main__type "$arg" 0
650667
res=$REPLY
651668
fi
@@ -888,6 +905,7 @@ _zsh_highlight_main_highlighter_highlight_list()
888905
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
889906
next_word=':start::start_of_pipeline:'
890907
fi
908+
: ${param_style:=$style}
891909
else # $arg is a non-command word
892910
case $arg in
893911
$'\x29') # subshell or end of array assignment
@@ -946,6 +964,7 @@ _zsh_highlight_main_highlighter_highlight_list()
946964
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
947965
done
948966
(( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
967+
(( in_param == 1 )) && in_param=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-"unknown-token"}
949968
[[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]]
950969
REPLY=$(( end_pos + ${#match[1]} - 1 ))
951970
reply=($list_highlights)

highlighters/main/test-data/param-precommand-option-argument1.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ BUFFER='$sudo_u phy1729 echo foo'
3636

3737
expected_region_highlight=(
3838
'1 7 precommand' # $sudo_u
39-
'9 15 default "issue #674"' # phy1729
39+
'9 15 default' # phy1729
4040
'18 20 command "issue #540"' # echo (not builtin)
4141
'22 24 default' # foo
4242
)

highlighters/main/test-data/param-precommand-option-argument3.zsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ BUFFER='$sudo_u phy1729 ls foo'
3636

3737
expected_region_highlight=(
3838
'1 7 precommand' # sudo_u
39-
'9 15 default "issue #674"' # phy1729
40-
'17 18 command "issue #674"' # ls
39+
'9 15 default' # phy1729
40+
'17 18 command' # ls
4141
'20 22 default' # foo
4242
)

0 commit comments

Comments
 (0)