Skip to content

Commit 3a57b27

Browse files
committed
main: Convert in_alias to an array of shift counts
No functional change. Keeps track of how deep we are in expanding nested aliases for the next commit.
1 parent 1715f39 commit 3a57b27

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ _zsh_highlight_main_add_region_highlight() {
7676
integer start=$1 end=$2
7777
shift 2
7878

79-
if (( in_alias )); then
79+
if (( $#in_alias )); then
8080
[[ $1 == unknown-token ]] && alias_style=unknown-token
8181
return
8282
fi
@@ -498,17 +498,18 @@ _zsh_highlight_main_highlighter__try_expand_parameter()
498498
_zsh_highlight_main_highlighter_highlight_list()
499499
{
500500
integer start_pos end_pos=0 buf_offset=$1 has_end=$3
501-
# alias_style is the style to apply to an alias once in_alias=0
501+
# alias_style is the style to apply to an alias once $#in_alias == 0
502502
# Usually 'alias' but set to 'unknown-token' if any word expanded from
503503
# the alias would be highlighted as unknown-token
504504
# param_style is analogous for parameter expansions
505505
local alias_style param_style last_arg arg buf=$4 highlight_glob=true saw_assignment=false style
506506
local in_array_assignment=false # true between 'a=(' and the matching ')'
507-
# in_alias is equal to the number of shifts needed until arg=args[1] pops an
508-
# arg from BUFFER and not added by an alias.
507+
# in_alias is an array of integers with each element equal to the number
508+
# of shifts needed until arg=args[1] pops an arg from the next level up
509+
# alias or from BUFFER.
509510
# in_param is analogous for parameter expansions
510-
integer in_alias=0 in_param=0 len=$#buf
511-
local -a match mbegin mend list_highlights
511+
integer in_param=0 len=$#buf
512+
local -a in_alias match mbegin mend list_highlights
512513
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
513514
local -A seen_alias
514515
# Pattern for parameter names
@@ -596,9 +597,11 @@ _zsh_highlight_main_highlighter_highlight_list()
596597
last_arg=$arg
597598
arg=$args[1]
598599
shift args
599-
if (( in_alias )); then
600-
(( in_alias-- ))
601-
if (( in_alias == 0 )); then
600+
if (( $#in_alias )); then
601+
(( in_alias[-1]-- ))
602+
# Remove trailing 0 entries
603+
in_alias=($in_alias[1,$in_alias[(I)<1->]])
604+
if (( $#in_alias == 0 )); then
602605
seen_alias=()
603606
# start_pos and end_pos are of the alias (previous $arg) here
604607
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
@@ -637,7 +640,7 @@ _zsh_highlight_main_highlighter_highlight_list()
637640
fi
638641
fi
639642

640-
if (( in_alias == 0 && in_param == 0 )); then
643+
if (( $#in_alias == 0 && in_param == 0 )); then
641644
# Compute the new $start_pos and $end_pos, skipping over whitespace in $buf.
642645
[[ "$proc_buf" = (#b)(#s)(([ $'\t']|[\\]$'\n')#)(?|)* ]]
643646
# The first, outer parenthesis
@@ -693,7 +696,6 @@ _zsh_highlight_main_highlighter_highlight_list()
693696
if [[ $res == "alias" ]]; then
694697
# Mark insane aliases as unknown-token (cf. #263).
695698
if [[ $arg == ?*=* ]]; then
696-
(( in_alias == 0 )) && in_alias=1
697699
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
698700
continue
699701
fi
@@ -707,15 +709,15 @@ _zsh_highlight_main_highlighter_highlight_list()
707709
alias_args=(${(z)REPLY})
708710
fi
709711
args=( $alias_args $args )
710-
if (( in_alias == 0 )); then
712+
if (( $#in_alias == 0 )); then
711713
alias_style=alias
712-
# Add one because we will in_alias-- on the next loop iteration so
713-
# this iteration should be considered in in_alias as well
714-
(( in_alias += $#alias_args + 1 ))
715714
else
716-
# This arg is already included in the count, so no need to + 1.
717-
(( in_alias += $#alias_args ))
715+
# Transfer the count of this arg to the new element about to be appended.
716+
(( in_alias[-1]-- ))
718717
fi
718+
# Add one because we will in_alias[-1]-- on the next loop iteration so
719+
# this iteration should be considered in in_alias as well
720+
in_alias+=( $(($#alias_args + 1)) )
719721
(( in_redirection++ )) # Stall this arg
720722
continue
721723
else
@@ -854,7 +856,7 @@ _zsh_highlight_main_highlighter_highlight_list()
854856
style=commandseparator
855857
elif [[ $this_word == *':start:'* ]] && [[ $arg == $'\n' ]]; then
856858
style=commandseparator
857-
elif [[ $this_word == *':start:'* ]] && [[ $arg == ';' ]] && (( in_alias )); then
859+
elif [[ $this_word == *':start:'* ]] && [[ $arg == ';' ]] && (( $#in_alias )); then
858860
style=commandseparator
859861
else
860862
# Empty commands (semicolon follows nothing) are valid syntax.
@@ -1147,7 +1149,7 @@ _zsh_highlight_main_highlighter_highlight_list()
11471149
fi
11481150
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
11491151
done
1150-
(( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
1152+
(( $#in_alias )) && in_alias=() _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
11511153
(( in_param == 1 )) && in_param=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $param_style
11521154
[[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]]
11531155
REPLY=$(( end_pos + ${#match[1]} - 1 ))
@@ -1258,7 +1260,7 @@ _zsh_highlight_main_highlighter_check_path()
12581260

12591261
# If this word ends the buffer, check if it's the prefix of a valid path.
12601262
if (( has_end && (len == end_pos) )) &&
1261-
(( ! in_alias )) &&
1263+
(( ! $#in_alias )) &&
12621264
[[ $WIDGET != zle-line-finish ]]; then
12631265
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
12641266
local -a tmp

0 commit comments

Comments
 (0)