Skip to content

Commit cb8c736

Browse files
committed
main: Run the entirety of aliases through the state machine
Fixes zsh-users#540 zsh-users#544 zsh-users#552 zsh-users#554 zsh-users#555
1 parent 2d4fe98 commit cb8c736

15 files changed

+108
-52
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ _zsh_highlight_main_add_region_highlight() {
7272
integer start=$1 end=$2
7373
shift 2
7474

75+
(( highlighted_alias )) && return
76+
(( in_alias )) && highlighted_alias=1
77+
7578
# The calculation was relative to $buf but region_highlight is relative to $BUFFER.
7679
(( start += buf_offset ))
7780
(( end += buf_offset ))
@@ -363,10 +366,18 @@ _zsh_highlight_highlighter_main_paint()
363366
_zsh_highlight_main_highlighter_highlight_list()
364367
{
365368
integer start_pos end_pos=0 buf_offset=$1 has_end=$3
366-
local buf=$4 highlight_glob=true arg arg_raw style
369+
# last_alias is the last alias arg (lhs) expanded (if in an alias).
370+
# This allows for expanding alias ls='ls -l' while avoiding loops.
371+
local arg buf=$4 highlight_glob=true last_alias style
367372
local in_array_assignment=false # true between 'a=(' and the matching ')'
368-
integer len=$#buf
373+
# highlighted_alias is 1 when the alias arg has been highlighted with a non-alias style.
374+
# E.g. alias x=ls; x has been highlighted as alias AND command.
375+
# in_alias is equal to the number of shifts needed until arg=args[1] pops an
376+
# arg from BUFFER and not added by an alias.
377+
integer highlighted_alias=0 in_alias=0 len=$#buf
369378
local -a match mbegin mend list_highlights
379+
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
380+
local -A seen_alias
370381
list_highlights=()
371382

372383
# "R" for round
@@ -427,10 +438,12 @@ _zsh_highlight_main_highlighter_highlight_list()
427438
args=(${(z)buf})
428439
fi
429440
while (( $#args )); do
430-
# Save an unmunged copy of the current word.
431441
arg=$args[1]
432-
arg_raw="$arg"
433442
shift args
443+
if (( in_alias )); then
444+
(( in_alias-- ))
445+
(( in_alias == 0 )) && highlighted_alias=0 last_alias= seen_alias=()
446+
fi
434447

435448
# Initialize this_word and next_word.
436449
if (( in_redirection == 0 )); then
@@ -455,7 +468,7 @@ _zsh_highlight_main_highlighter_highlight_list()
455468
fi
456469
fi
457470

458-
if true; then
471+
if (( in_alias == 0 )); then
459472
# Compute the new $start_pos and $end_pos, skipping over whitespace in $buf.
460473
start_pos=$end_pos
461474
if [[ $arg == ';' ]] ; then
@@ -531,62 +544,50 @@ _zsh_highlight_main_highlighter_highlight_list()
531544
# Expand aliases.
532545
_zsh_highlight_main__type "$arg"
533546
local res="$REPLY"
534-
if [[ $res == "alias" ]]; then
535-
() {
536-
local -A seen_alias
537-
while [[ $REPLY == alias ]]; do
547+
if [[ $res == "alias" ]] && [[ $last_alias != $arg ]]; then
548+
# Avoid looping forever on alias a=b b=c c=b, but allow alias foo='foo bar'
549+
if (( $+seen_alias[$arg] )); then
550+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
551+
continue
552+
fi
538553
seen_alias[$arg]=1
554+
last_alias=$arg
539555
_zsh_highlight_main__resolve_alias $arg
540-
# Use a temporary array to ensure the subscript is interpreted as
541-
# an array subscript, not as a scalar subscript
542556
local -a alias_args
543-
# TODO: the ${interactive_comments+set} path needs to skip comments; see test-data/alias-comment1.zsh
557+
# Elision is desired in case alias x=''
544558
alias_args=( ${interactive_comments-${(z)REPLY}}
545559
${interactive_comments+${(zZ+c+)REPLY}} )
546-
# Avoid looping forever on alias a=b b=c c=b, but allow alias foo='foo bar'
547-
[[ $arg == $alias_args[1] ]] && break
548-
arg=$alias_args[1]
549-
if (( $+seen_alias[$arg] )); then
550-
res=none
551-
break
552-
fi
553-
_zsh_highlight_main__type "$arg"
554-
done
555-
}
556-
_zsh_highlight_main_highlighter_expand_path $arg
557-
arg=$REPLY
558-
() {
559-
# Make sure to use $arg_raw here, rather than $arg.
560-
integer insane_alias
561-
case $arg_raw in
560+
case $arg in
562561
# Issue #263: aliases with '=' on their LHS.
563562
#
564563
# There are three cases:
565564
#
566565
# - Unsupported, breaks 'alias -L' output, but invokable:
567566
('='*) :;;
568567
# - Unsupported, not invokable:
569-
(*'='*) insane_alias=1;;
568+
(*'='*)
569+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
570+
continue
571+
;;
570572
# - The common case:
571573
(*) :;;
572574
esac
573-
if (( insane_alias )); then
574-
style=unknown-token
575-
# Calling 'type' again; since __type memoizes the answer, this call is just a hash lookup.
576-
elif ! _zsh_highlight_main__type "$arg" || [[ $REPLY == 'none' ]]; then
577-
style=unknown-token
575+
args=( $alias_args $args )
576+
if (( in_alias == 0 )); then
577+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos alias
578+
# Add one because we will in_alias-- on the next loop iteration so
579+
# this iteration should be considered in in_alias as well
580+
(( in_alias += $#alias_args + 1 ))
578581
else
579-
# The common case.
580-
style=alias
581-
if (( ${+precommand_options[(re)"$arg"]} )) && (( ! ${+precommand_options[(re)"$arg_raw"]} )); then
582-
precommand_options[$arg_raw]=$precommand_options[$arg]
583-
fi
582+
# This arg is already included in the count, so no need to + 1.
583+
(( in_alias += $#alias_args ))
584584
fi
585-
}
585+
(( in_redirection++ )) # Stall this arg
586+
continue
586587
else
587588
_zsh_highlight_main_highlighter_expand_path $arg
588589
arg=$REPLY
589-
_zsh_highlight_main__type "$arg"
590+
_zsh_highlight_main__type "$arg" 0
590591
res="$REPLY"
591592
fi
592593
fi
@@ -635,7 +636,7 @@ _zsh_highlight_main_highlighter_highlight_list()
635636
arg=${(P)MATCH}
636637
;;
637638
esac
638-
_zsh_highlight_main__type "$arg"
639+
_zsh_highlight_main__type "$arg" 0
639640
res=$REPLY
640641
fi
641642
}
@@ -703,7 +704,7 @@ _zsh_highlight_main_highlighter_highlight_list()
703704
next_word=':start:'
704705
elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word
705706
if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then
706-
[[ $res != alias ]] && style=precommand
707+
style=precommand
707708
flags_with_argument=${precommand_options[$arg]%:*}
708709
flags_sans_argument=${precommand_options[$arg]#*:}
709710
next_word=${next_word//:regular:/}

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

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

3535
expected_region_highlight=(
36-
"1 1 alias 'interactivecomments applies to aliases'" # x becomes pwd
36+
'1 1 alias' # x
37+
'1 1 comment' # x (#)
3738
)

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

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ alias a=b b=c c=b
3333
BUFFER='a foo; :'
3434

3535
expected_region_highlight=(
36-
'1 1 unknown-token' # a
36+
'1 1 alias' # a
37+
'1 1 unknown-token' # a (invalid alias loop)
3738
'3 5 default' # foo
3839
'6 6 commandseparator' # ;
3940
'8 8 builtin' # :

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ BUFFER='a -u phy1729 echo; :'
3535

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ BUFFER='a foo; :'
3434

3535
expected_region_highlight=(
3636
'1 1 alias' # a
37+
'1 1 builtin' # a (:)
3738
'3 5 default' # foo
3839
'6 6 commandseparator' # ;
3940
'8 8 builtin' # :

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ BUFFER='sdu phy1729 echo foo'
3535

3636
expected_region_highlight=(
3737
'1 3 alias' # sdu
38-
'5 11 default "issue #540"' # phy1729
38+
'1 3 precommand' # sdu (sudo)
39+
'5 11 default' # phy1729
3940
'13 16 commmand "issue #540"' # echo (not builtin)
4041
'18 20 default' # foo
4142
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2018 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
alias sde='sudo -e'
32+
alias seu='sde -u'
33+
sudo(){}
34+
35+
BUFFER='seu phy1729 echo foo'
36+
37+
expected_region_highlight=(
38+
'1 3 alias' # seu
39+
'1 3 precommand' # seu (sudo)
40+
'5 11 default' # phy1729
41+
'13 16 commmand "issue #540"' # echo (not builtin)
42+
'18 20 default' # foo
43+
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ expected_region_highlight=(
3535
'1 3 unknown-token' # "a"
3636
'5 7 default' # foo
3737
'8 8 commandseparator' # ;
38-
'10 12 command "issue #544' # \ls
38+
'10 12 command' # \ls
3939
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ alias x=\>
3131
BUFFER='x foo echo bar'
3232

3333
expected_region_highlight=(
34-
'1 1 redirection' # x becomes >
34+
'1 1 alias' # x
35+
'1 1 redirection' # x (>)
3536
'3 5 default' # foo
3637
'7 10 builtin' # echo
3738
'12 14 default' # bar

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ BUFFER='echo bar'
3434

3535
expected_region_highlight=(
3636
'1 4 alias' # echo
37+
'1 4 builtin' # echo
3738
'6 8 default' # bar
3839
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ alias x=/
3232
BUFFER=$'x'
3333

3434
expected_region_highlight=(
35-
'1 1 unknown-token' # x
35+
'1 1 alias' # x
36+
'1 1 unknown-token "issue #202"' # x (/)
3637
)

highlighters/main/test-data/alias.zsh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ fi
4848
expected_region_highlight+=(
4949
"9 9 commandseparator" # ;
5050
"11 16 alias" # alias1
51+
"11 16 command" # alias1 (ls)
5152
)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ BUFFER='x ls'
3232

3333
expected_region_highlight=(
3434
"1 1 alias" # x
35+
"1 1 precommand" # x (command)
3536
"3 4 command" # ls
3637
)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ f() {}
3333
BUFFER='a;f;'
3434

3535
expected_region_highlight=(
36-
"1 1 alias" # f
36+
"1 1 alias" # a
37+
"1 1 builtin" # a (:)
3738
"2 2 commandseparator" # ;
38-
"3 3 function" # g
39+
"3 3 function" # f
3940
"4 4 commandseparator" # ;
4041
)

0 commit comments

Comments
 (0)