Skip to content

Commit 4073f44

Browse files
committed
main: Stop highlighting alias as its first word too
Fixes #565
1 parent cee6ccf commit 4073f44

15 files changed

+16
-23
lines changed

highlighters/main/main-highlighter.zsh

+15-8
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ _zsh_highlight_main_add_region_highlight() {
7373
integer start=$1 end=$2
7474
shift 2
7575

76-
(( highlighted_alias )) && return
77-
(( in_alias )) && highlighted_alias=1
76+
if (( in_alias )); then
77+
[[ $1 == unknown-token ]] && alias_style=unknown-token
78+
return
79+
fi
7880

7981
# The calculation was relative to $buf but region_highlight is relative to $BUFFER.
8082
(( start += buf_offset ))
@@ -377,15 +379,14 @@ _zsh_highlight_highlighter_main_paint()
377379
_zsh_highlight_main_highlighter_highlight_list()
378380
{
379381
integer start_pos end_pos=0 buf_offset=$1 has_end=$3
382+
# alias_style is the style to apply to an alias once in_alias=0
380383
# last_alias is the last alias arg (lhs) expanded (if in an alias).
381384
# This allows for expanding alias ls='ls -l' while avoiding loops.
382-
local arg buf=$4 highlight_glob=true last_alias style
385+
local alias_style arg buf=$4 highlight_glob=true last_alias style
383386
local in_array_assignment=false # true between 'a=(' and the matching ')'
384-
# highlighted_alias is 1 when the alias arg has been highlighted with a non-alias style.
385-
# E.g. alias x=ls; x has been highlighted as alias AND command.
386387
# in_alias is equal to the number of shifts needed until arg=args[1] pops an
387388
# arg from BUFFER and not added by an alias.
388-
integer highlighted_alias=0 in_alias=0 len=$#buf
389+
integer in_alias=0 len=$#buf
389390
local -a match mbegin mend list_highlights
390391
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
391392
local -A seen_alias
@@ -458,7 +459,11 @@ _zsh_highlight_main_highlighter_highlight_list()
458459
shift args
459460
if (( in_alias )); then
460461
(( in_alias-- ))
461-
(( in_alias == 0 )) && highlighted_alias=0 last_alias= seen_alias=()
462+
if (( in_alias == 0 )); then
463+
last_alias= seen_alias=()
464+
# start_pos and end_pos are of the alias (previous $arg) here
465+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
466+
fi
462467
fi
463468

464469
# Initialize this_word and next_word.
@@ -535,6 +540,7 @@ _zsh_highlight_main_highlighter_highlight_list()
535540
# Avoid looping forever on alias a=b b=c c=b, but allow alias foo='foo bar'
536541
# Also mark insane aliases as unknown-token (cf. #263).
537542
if (( $+seen_alias[$arg] )) || [[ $arg == ?*=* ]]; then
543+
(( in_alias == 0 )) && in_alias=1
538544
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
539545
continue
540546
fi
@@ -550,7 +556,7 @@ _zsh_highlight_main_highlighter_highlight_list()
550556
fi
551557
args=( $alias_args $args )
552558
if (( in_alias == 0 )); then
553-
_zsh_highlight_main_add_region_highlight $start_pos $end_pos alias
559+
alias_style=alias
554560
# Add one because we will in_alias-- on the next loop iteration so
555561
# this iteration should be considered in in_alias as well
556562
(( in_alias += $#alias_args + 1 ))
@@ -909,6 +915,7 @@ _zsh_highlight_main_highlighter_highlight_list()
909915
fi
910916
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
911917
done
918+
(( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
912919
[[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]]
913920
REPLY=$(( end_pos + ${#match[1]} - 1 ))
914921
reply=($list_highlights)

highlighters/main/test-data/alias-comment1.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ BUFFER='x'
3434

3535
expected_region_highlight=(
3636
'1 1 alias' # x
37-
'1 1 comment' # x (#)
3837
)

highlighters/main/test-data/alias-comment2.zsh

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ alias x=$'# foo\npwd'
3333
BUFFER='x'
3434

3535
expected_region_highlight=(
36-
'1 1 alias' # x
37-
'1 1 unknown-token' # x (#)
36+
'1 1 unknown-token' # x
3837
)

highlighters/main/test-data/alias-complex.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ BUFFER='x file echo'
3333

3434
expected_region_highlight=(
3535
'1 1 alias' # x
36-
'1 1 builtin' # x (echo)
3736
'3 6 default' # file
3837
'8 11 builtin' # echo
3938
)

highlighters/main/test-data/alias-loop.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ alias a=b b=c c=b
3333
BUFFER='a foo; :'
3434

3535
expected_region_highlight=(
36-
'1 1 alias' # a
3736
'1 1 unknown-token' # a (invalid alias loop)
3837
'3 5 default' # foo
3938
'6 6 commandseparator' # ;

highlighters/main/test-data/alias-nested-precommand.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ BUFFER='a -u phy1729 echo; :'
3535

3636
expected_region_highlight=(
3737
'1 1 alias' # a
38-
'1 1 precommand' # a (sudo)
3938
'3 4 single-hyphen-option' # -u
4039
'6 12 default' # phy1729
4140
'14 17 builtin' # echo

highlighters/main/test-data/alias-nested.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ BUFFER='a foo; :'
3434

3535
expected_region_highlight=(
3636
'1 1 alias' # a
37-
'1 1 builtin' # a (:)
3837
'3 5 default' # foo
3938
'6 6 commandseparator' # ;
4039
'8 8 builtin' # :

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

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ BUFFER='sdu phy1729 echo foo'
3535

3636
expected_region_highlight=(
3737
'1 3 alias' # sdu
38-
'1 3 precommand' # sdu (sudo)
3938
'5 11 default' # phy1729
4039
'13 16 commmand "issue #540"' # echo (not builtin)
4140
'18 20 default' # foo

highlighters/main/test-data/alias-precommand-option-argument2.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ BUFFER='sbu phy1729 echo foo'
3636

3737
expected_region_highlight=(
3838
'1 3 alias' # sbu
39-
'1 3 precommand' # sbu (sudo)
4039
'5 11 default' # phy1729
4140
'13 16 commmand "issue #540"' # echo (not builtin)
4241
'18 20 default' # foo

highlighters/main/test-data/alias-redirect.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ BUFFER='x foo echo bar'
3232

3333
expected_region_highlight=(
3434
'1 1 alias' # x
35-
'1 1 redirection' # x (>)
3635
'3 5 default' # foo
3736
'7 10 builtin' # echo
3837
'12 14 default' # bar

highlighters/main/test-data/alias-self.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,5 @@ BUFFER='echo bar'
3434

3535
expected_region_highlight=(
3636
'1 4 alias' # echo
37-
'1 4 builtin' # echo
3837
'6 8 default' # bar
3938
)

highlighters/main/test-data/alias-to-dir.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ BUFFER=$'x'
3333

3434
expected_region_highlight=(
3535
'1 1 alias' # x
36-
'1 1 unknown-token "issue #202"' # x (/)
3736
)

highlighters/main/test-data/alias.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ fi
4848
expected_region_highlight+=(
4949
"9 9 commandseparator" # ;
5050
"11 16 alias" # alias1
51-
"11 16 command" # alias1 (ls)
5251
"17 17 commandseparator" # ;
5352
"19 24 unknown-token" # alias2
5453
)

highlighters/main/test-data/noglob-alias.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ BUFFER='x ls'
3232

3333
expected_region_highlight=(
3434
"1 1 alias" # x
35-
"1 1 precommand" # x (command)
3635
"3 4 command" # ls
3736
)

highlighters/main/test-data/off-by-one.zsh

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ BUFFER='a;f;'
3434

3535
expected_region_highlight=(
3636
"1 1 alias" # a
37-
"1 1 builtin" # a (:)
3837
"2 2 commandseparator" # ;
3938
"3 3 function" # f
4039
"4 4 commandseparator" # ;

0 commit comments

Comments
 (0)