Skip to content

Highlight aliases once #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ _zsh_highlight_main_add_region_highlight() {
integer start=$1 end=$2
shift 2

(( highlighted_alias )) && return
(( in_alias )) && highlighted_alias=1
if (( in_alias )); then
[[ $1 == unknown-token ]] && alias_style=unknown-token
return
fi

# The calculation was relative to $buf but region_highlight is relative to $BUFFER.
(( start += buf_offset ))
Expand Down Expand Up @@ -377,15 +379,16 @@ _zsh_highlight_highlighter_main_paint()
_zsh_highlight_main_highlighter_highlight_list()
{
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).
# 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 ')'
# 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
# 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
# seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a
local -A seen_alias
Expand Down Expand Up @@ -458,7 +461,11 @@ _zsh_highlight_main_highlighter_highlight_list()
shift args
if (( in_alias )); then
(( 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

# Initialize this_word and next_word.
Expand Down Expand Up @@ -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'
# Also mark insane aliases as unknown-token (cf. #263).
if (( $+seen_alias[$arg] )) || [[ $arg == ?*=* ]]; then
(( in_alias == 0 )) && in_alias=1
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
continue
fi
Expand All @@ -543,11 +551,14 @@ _zsh_highlight_main_highlighter_highlight_list()
_zsh_highlight_main__resolve_alias $arg
local -a alias_args
# Elision is desired in case alias x=''
alias_args=( ${interactive_comments-${(z)REPLY}}
${interactive_comments+${(zZ+c+)REPLY}} )
if [[ $zsyh_user_options[interactivecomments] == on ]]; then
alias_args=(${(zZ+c+)REPLY})
else
alias_args=(${(z)REPLY})
fi
args=( $alias_args $args )
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
# this iteration should be considered in in_alias as well
(( in_alias += $#alias_args + 1 ))
Expand Down Expand Up @@ -906,6 +917,7 @@ _zsh_highlight_main_highlighter_highlight_list()
fi
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
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')#) ]]
REPLY=$(( end_pos + ${#match[1]} - 1 ))
reply=($list_highlights)
Expand Down
3 changes: 1 addition & 2 deletions highlighters/main/test-data/alias-comment1.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ alias x=$'# foo\npwd'
BUFFER='x'

expected_region_highlight=(
'1 1 alias' # x
'1 1 comment' # x (#)
'1 1 alias "issue #616"' # x
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-comment2.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ alias x=$'# foo\npwd'
BUFFER='x'

expected_region_highlight=(
'1 1 alias' # x
'1 1 unknown-token' # x (#)
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-complex.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ BUFFER='x file echo'

expected_region_highlight=(
'1 1 alias' # x
'1 1 builtin' # x (echo)
'3 6 default' # file
'8 11 builtin' # echo
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-loop.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ alias a=b b=c c=b
BUFFER='a foo; :'

expected_region_highlight=(
'1 1 alias' # a
'1 1 unknown-token' # a (invalid alias loop)
'3 5 default' # foo
'6 6 commandseparator' # ;
Expand Down
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-nested-precommand.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ BUFFER='a -u phy1729 echo; :'

expected_region_highlight=(
'1 1 alias' # a
'1 1 precommand' # a (sudo)
'3 4 single-hyphen-option' # -u
'6 12 default' # phy1729
'14 17 builtin' # echo
Expand Down
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-nested.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ BUFFER='a foo; :'

expected_region_highlight=(
'1 1 alias' # a
'1 1 builtin' # a (:)
'3 5 default' # foo
'6 6 commandseparator' # ;
'8 8 builtin' # :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

alias sdu='sudo -u'
alias sudo_u='sudo -u'
sudo(){}

BUFFER='sdu phy1729 echo foo'
BUFFER='sudo_u phy1729 echo foo'

expected_region_highlight=(
'1 3 alias' # sdu
'1 3 precommand' # sdu (sudo)
'5 11 default' # phy1729
'13 16 commmand "issue #540"' # echo (not builtin)
'18 20 default' # foo
'1 6 alias' # sudo_u
'8 14 default' # phy1729
'17 19 command "issue #540"' # echo (not builtin)
'21 23 default' # foo
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

alias sde='sudo -e'
alias seu='sde -u'
alias sudo_b='sudo -b'
alias sudo_b_u='sudo_b -u'
sudo(){}

BUFFER='seu phy1729 echo foo'
BUFFER='sudo_b_u phy1729 echo foo'

expected_region_highlight=(
'1 3 alias' # seu
'1 3 precommand' # seu (sudo)
'5 11 default' # phy1729
'13 16 commmand "issue #540"' # echo (not builtin)
'18 20 default' # foo
'1 8 alias' # sudo_b_u
'10 16 default' # phy1729
'18 21 command "issue #540"' # echo (not builtin)
'23 25 default' # foo
)
41 changes: 41 additions & 0 deletions highlighters/main/test-data/alias-precommand-option-argument3.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2019 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

alias sudo_u='sudo -u'
sudo(){}

BUFFER='sudo_u phy1729 ls foo'

expected_region_highlight=(
'1 6 alias' # sudo_u
'8 14 default' # phy1729
'16 17 command' # ls
'19 21 default' # foo
)
42 changes: 42 additions & 0 deletions highlighters/main/test-data/alias-precommand-option-argument4.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2018 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

alias sudo_b='sudo -b'
alias sudo_b_u='sudo_b -u'
sudo(){}

BUFFER='sudo_b_u phy1729 ls foo'

expected_region_highlight=(
'1 8 alias' # sudo_b_u
'10 16 default' # phy1729
'18 19 command' # ls
'21 23 default' # foo
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-redirect.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ BUFFER='x foo echo bar'

expected_region_highlight=(
'1 1 alias' # x
'1 1 redirection' # x (>)
'3 5 default' # foo
'7 10 builtin' # echo
'12 14 default' # bar
Expand Down
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-self.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ BUFFER='echo bar'

expected_region_highlight=(
'1 4 alias' # echo
'1 4 builtin' # echo
'6 8 default' # bar
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias-to-dir.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ alias x=/
BUFFER=$'x'

expected_region_highlight=(
'1 1 alias' # x
'1 1 unknown-token "issue #202"' # x (/)
)
37 changes: 37 additions & 0 deletions highlighters/main/test-data/alias-unknown-token1.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2019 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

alias a=b b=foo

BUFFER='a '

expected_region_highlight=(
'1 1 unknown-token' # a
)
37 changes: 37 additions & 0 deletions highlighters/main/test-data/alias-unknown-token2.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2019 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

alias a='() { ls "$@" ; foo }'

BUFFER='a '

expected_region_highlight=(
'1 1 unknown-token' # a
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/alias.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ fi
expected_region_highlight+=(
"9 9 commandseparator" # ;
"11 16 alias" # alias1
"11 16 command" # alias1 (ls)
"17 17 commandseparator" # ;
"19 24 unknown-token" # alias2
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/noglob-alias.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ BUFFER='x ls'

expected_region_highlight=(
"1 1 alias" # x
"1 1 precommand" # x (command)
"3 4 command" # ls
)
1 change: 0 additions & 1 deletion highlighters/main/test-data/off-by-one.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ BUFFER='a;f;'

expected_region_highlight=(
"1 1 alias" # a
"1 1 builtin" # a (:)
"2 2 commandseparator" # ;
"3 3 function" # f
"4 4 commandseparator" # ;
Expand Down