main: Stop highlighting alias as its first word too

Fixes #565 and #576
This commit is contained in:
Matthew Martin 2018-10-22 07:50:53 -05:00
parent a88d41e095
commit 9cc0060334
15 changed files with 18 additions and 23 deletions

View File

@ -73,8 +73,10 @@ _zsh_highlight_main_add_region_highlight() {
integer start=$1 end=$2 integer start=$1 end=$2
shift 2 shift 2
(( highlighted_alias )) && return if (( in_alias )); then
(( in_alias )) && highlighted_alias=1 [[ $1 == unknown-token ]] && alias_style=unknown-token
return
fi
# The calculation was relative to $buf but region_highlight is relative to $BUFFER. # The calculation was relative to $buf but region_highlight is relative to $BUFFER.
(( start += buf_offset )) (( start += buf_offset ))
@ -377,15 +379,16 @@ _zsh_highlight_highlighter_main_paint()
_zsh_highlight_main_highlighter_highlight_list() _zsh_highlight_main_highlighter_highlight_list()
{ {
integer start_pos end_pos=0 buf_offset=$1 has_end=$3 integer start_pos end_pos=0 buf_offset=$1 has_end=$3
# alias_style is the style to apply to an alias once in_alias=0
# Usually 'alias' but set to 'unknown-token' if any word expanded from
# the alias would be highlighted as unknown-token
# last_alias is the last alias arg (lhs) expanded (if in an alias). # last_alias is the last alias arg (lhs) expanded (if in an alias).
# This allows for expanding alias ls='ls -l' while avoiding loops. # This allows for expanding alias ls='ls -l' while avoiding loops.
local arg buf=$4 highlight_glob=true last_alias style local alias_style arg buf=$4 highlight_glob=true last_alias style
local in_array_assignment=false # true between 'a=(' and the matching ')' local in_array_assignment=false # true between 'a=(' and the matching ')'
# highlighted_alias is 1 when the alias arg has been highlighted with a non-alias style.
# E.g. alias x=ls; x has been highlighted as alias AND command.
# in_alias is equal to the number of shifts needed until arg=args[1] pops an # in_alias is equal to the number of shifts needed until arg=args[1] pops an
# arg from BUFFER and not added by an alias. # arg from BUFFER and not added by an alias.
integer highlighted_alias=0 in_alias=0 len=$#buf integer in_alias=0 len=$#buf
local -a match mbegin mend list_highlights local -a match mbegin mend list_highlights
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a # seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
local -A seen_alias local -A seen_alias
@ -458,7 +461,11 @@ _zsh_highlight_main_highlighter_highlight_list()
shift args shift args
if (( in_alias )); then if (( in_alias )); then
(( in_alias-- )) (( in_alias-- ))
(( in_alias == 0 )) && highlighted_alias=0 last_alias= seen_alias=() if (( in_alias == 0 )); then
last_alias= seen_alias=()
# start_pos and end_pos are of the alias (previous $arg) here
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
fi
fi fi
# Initialize this_word and next_word. # Initialize this_word and next_word.
@ -535,6 +542,7 @@ _zsh_highlight_main_highlighter_highlight_list()
# Avoid looping forever on alias a=b b=c c=b, but allow alias foo='foo bar' # Avoid looping forever on alias a=b b=c c=b, but allow alias foo='foo bar'
# Also mark insane aliases as unknown-token (cf. #263). # Also mark insane aliases as unknown-token (cf. #263).
if (( $+seen_alias[$arg] )) || [[ $arg == ?*=* ]]; then if (( $+seen_alias[$arg] )) || [[ $arg == ?*=* ]]; then
(( in_alias == 0 )) && in_alias=1
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token _zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
continue continue
fi fi
@ -550,7 +558,7 @@ _zsh_highlight_main_highlighter_highlight_list()
fi fi
args=( $alias_args $args ) args=( $alias_args $args )
if (( in_alias == 0 )); then if (( in_alias == 0 )); then
_zsh_highlight_main_add_region_highlight $start_pos $end_pos alias alias_style=alias
# Add one because we will in_alias-- on the next loop iteration so # Add one because we will in_alias-- on the next loop iteration so
# this iteration should be considered in in_alias as well # this iteration should be considered in in_alias as well
(( in_alias += $#alias_args + 1 )) (( in_alias += $#alias_args + 1 ))
@ -909,6 +917,7 @@ _zsh_highlight_main_highlighter_highlight_list()
fi fi
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
done done
(( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
[[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]] [[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]]
REPLY=$(( end_pos + ${#match[1]} - 1 )) REPLY=$(( end_pos + ${#match[1]} - 1 ))
reply=($list_highlights) reply=($list_highlights)

View File

@ -33,6 +33,5 @@ alias x=$'# foo\npwd'
BUFFER='x' BUFFER='x'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # x '1 1 alias "issue #616"' # x
'1 1 comment' # x (#)
) )

View File

@ -33,6 +33,5 @@ alias x=$'# foo\npwd'
BUFFER='x' BUFFER='x'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # x
'1 1 unknown-token' # x (#) '1 1 unknown-token' # x (#)
) )

View File

@ -33,7 +33,6 @@ BUFFER='x file echo'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # x '1 1 alias' # x
'1 1 builtin' # x (echo)
'3 6 default' # file '3 6 default' # file
'8 11 builtin' # echo '8 11 builtin' # echo
) )

View File

@ -33,7 +33,6 @@ alias a=b b=c c=b
BUFFER='a foo; :' BUFFER='a foo; :'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # a
'1 1 unknown-token' # a (invalid alias loop) '1 1 unknown-token' # a (invalid alias loop)
'3 5 default' # foo '3 5 default' # foo
'6 6 commandseparator' # ; '6 6 commandseparator' # ;

View File

@ -35,7 +35,6 @@ BUFFER='a -u phy1729 echo; :'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # a '1 1 alias' # a
'1 1 precommand' # a (sudo)
'3 4 single-hyphen-option' # -u '3 4 single-hyphen-option' # -u
'6 12 default' # phy1729 '6 12 default' # phy1729
'14 17 builtin' # echo '14 17 builtin' # echo

View File

@ -34,7 +34,6 @@ BUFFER='a foo; :'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # a '1 1 alias' # a
'1 1 builtin' # a (:)
'3 5 default' # foo '3 5 default' # foo
'6 6 commandseparator' # ; '6 6 commandseparator' # ;
'8 8 builtin' # : '8 8 builtin' # :

View File

@ -35,7 +35,6 @@ BUFFER='sdu phy1729 echo foo'
expected_region_highlight=( expected_region_highlight=(
'1 3 alias' # sdu '1 3 alias' # sdu
'1 3 precommand' # sdu (sudo)
'5 11 default' # phy1729 '5 11 default' # phy1729
'13 16 commmand "issue #540"' # echo (not builtin) '13 16 commmand "issue #540"' # echo (not builtin)
'18 20 default' # foo '18 20 default' # foo

View File

@ -36,7 +36,6 @@ BUFFER='sbu phy1729 echo foo'
expected_region_highlight=( expected_region_highlight=(
'1 3 alias' # sbu '1 3 alias' # sbu
'1 3 precommand' # sbu (sudo)
'5 11 default' # phy1729 '5 11 default' # phy1729
'13 16 commmand "issue #540"' # echo (not builtin) '13 16 commmand "issue #540"' # echo (not builtin)
'18 20 default' # foo '18 20 default' # foo

View File

@ -32,7 +32,6 @@ BUFFER='x foo echo bar'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # x '1 1 alias' # x
'1 1 redirection' # x (>)
'3 5 default' # foo '3 5 default' # foo
'7 10 builtin' # echo '7 10 builtin' # echo
'12 14 default' # bar '12 14 default' # bar

View File

@ -34,6 +34,5 @@ BUFFER='echo bar'
expected_region_highlight=( expected_region_highlight=(
'1 4 alias' # echo '1 4 alias' # echo
'1 4 builtin' # echo
'6 8 default' # bar '6 8 default' # bar
) )

View File

@ -32,6 +32,5 @@ alias x=/
BUFFER=$'x' BUFFER=$'x'
expected_region_highlight=( expected_region_highlight=(
'1 1 alias' # x
'1 1 unknown-token "issue #202"' # x (/) '1 1 unknown-token "issue #202"' # x (/)
) )

View File

@ -48,7 +48,6 @@ fi
expected_region_highlight+=( expected_region_highlight+=(
"9 9 commandseparator" # ; "9 9 commandseparator" # ;
"11 16 alias" # alias1 "11 16 alias" # alias1
"11 16 command" # alias1 (ls)
"17 17 commandseparator" # ; "17 17 commandseparator" # ;
"19 24 unknown-token" # alias2 "19 24 unknown-token" # alias2
) )

View File

@ -32,6 +32,5 @@ BUFFER='x ls'
expected_region_highlight=( expected_region_highlight=(
"1 1 alias" # x "1 1 alias" # x
"1 1 precommand" # x (command)
"3 4 command" # ls "3 4 command" # ls
) )

View File

@ -34,7 +34,6 @@ BUFFER='a;f;'
expected_region_highlight=( expected_region_highlight=(
"1 1 alias" # a "1 1 alias" # a
"1 1 builtin" # a (:)
"2 2 commandseparator" # ; "2 2 commandseparator" # ;
"3 3 function" # f "3 3 function" # f
"4 4 commandseparator" # ; "4 4 commandseparator" # ;