Skip to content

Commit 43306d8

Browse files
committed
'main': Pass parameters through the multi-word machinery, as we already do for aliases.
Fixes zsh-users#674.
1 parent 4e92449 commit 43306d8

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 ))
@@ -398,11 +402,13 @@ _zsh_highlight_main_highlighter_highlight_list()
398402
# alias_style is the style to apply to an alias once in_alias=0
399403
# Usually 'alias' but set to 'unknown-token' if any word expanded from
400404
# the alias would be highlighted as unknown-token
401-
local alias_style arg buf=$4 highlight_glob=true style
405+
# param_style is analogous for parameter expansions
406+
local alias_style param_style arg buf=$4 highlight_glob=true style
402407
local in_array_assignment=false # true between 'a=(' and the matching ')'
403408
# in_alias is equal to the number of shifts needed until arg=args[1] pops an
404409
# arg from BUFFER and not added by an alias.
405-
integer in_alias=0 len=$#buf
410+
# in_param is analogous for parameter expansions
411+
integer in_alias=0 in_param=0 len=$#buf
406412
local -a match mbegin mend list_highlights
407413
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
408414
local -A seen_alias
@@ -481,6 +487,14 @@ _zsh_highlight_main_highlighter_highlight_list()
481487
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
482488
fi
483489
fi
490+
if (( in_param )); then
491+
(( in_param-- ))
492+
if (( in_param == 0 )); then
493+
# start_pos and end_pos are of the alias (previous $arg) here
494+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-unknown_token}
495+
param_style=""
496+
fi
497+
fi
484498

485499
# Initialize this_word and next_word.
486500
if (( in_redirection == 0 )); then
@@ -505,7 +519,7 @@ _zsh_highlight_main_highlighter_highlight_list()
505519
fi
506520
fi
507521

508-
if (( in_alias == 0 )); then
522+
if (( in_alias == 0 && in_param == 0 )); then
509523
# Compute the new $start_pos and $end_pos, skipping over whitespace in $buf.
510524
[[ "$proc_buf" = (#b)(#s)(([ $'\t']|\\$'\n')#)* ]]
511525
# The first, outer parenthesis
@@ -618,6 +632,7 @@ _zsh_highlight_main_highlighter_highlight_list()
618632
local -a match mbegin mend
619633
local MATCH; integer MBEGIN MEND
620634
local parameter_name
635+
local -a words
621636
if [[ $arg[1] == '$' ]] && [[ ${arg[2]} == '{' ]] && [[ ${arg[-1]} == '}' ]]; then
622637
parameter_name=${${arg:2}%?}
623638
elif [[ $arg[1] == '$' ]]; then
@@ -630,14 +645,16 @@ _zsh_highlight_main_highlighter_highlight_list()
630645
# Set $arg.
631646
case ${(tP)MATCH} in
632647
(*array*|*assoc*)
633-
local -a words; words=( ${(P)MATCH} )
634-
arg=${words[1]}
648+
words=( ${(P)MATCH} )
635649
;;
636650
(*)
637651
# scalar, presumably
638-
arg=${(P)MATCH}
652+
words=( ${(P)MATCH} )
639653
;;
640654
esac
655+
(( in_param = 1 + $#words ))
656+
args=( $words $args )
657+
arg=$args[1]
641658
_zsh_highlight_main__type "$arg" 0
642659
res=$REPLY
643660
fi
@@ -880,6 +897,7 @@ _zsh_highlight_main_highlighter_highlight_list()
880897
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
881898
next_word=':start::start_of_pipeline:'
882899
fi
900+
: ${param_style:=$style}
883901
else # $arg is a non-command word
884902
case $arg in
885903
$'\x29') # subshell or end of array assignment
@@ -938,6 +956,7 @@ _zsh_highlight_main_highlighter_highlight_list()
938956
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
939957
done
940958
(( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
959+
(( in_param == 1 )) && in_param=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-unknown_token}
941960
[[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]]
942961
REPLY=$(( end_pos + ${#match[1]} - 1 ))
943962
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)