'main': Expand aliases first. (Issue #264.)

This commit causes an alias to an invalid command to be highlighted as an error
(unknown-token).
This commit is contained in:
Daniel Shahaf 2016-07-28 08:16:22 +00:00 committed by Matthew Martin
parent cf88b63523
commit f3410c5862
5 changed files with 110 additions and 11 deletions

View File

@ -299,7 +299,7 @@ _zsh_highlight_highlighter_main_paint()
_zsh_highlight_main_highlighter_highlight_list()
{
integer start_pos=0 end_pos buf_offset=$1 has_end=$3
local buf=$4 highlight_glob=true arg style
local buf=$4 highlight_glob=true arg arg_raw style
local in_array_assignment=false # true between 'a=(' and the matching ')'
integer len=$#buf
local -a match mbegin mend list_highlights
@ -362,6 +362,9 @@ _zsh_highlight_main_highlighter_highlight_list()
args=(${(z)buf})
fi
for arg in $args; do
# Save an unmunged copy of the current word.
arg_raw="$arg"
# Initialize $next_word.
if (( in_redirection )); then
(( --in_redirection ))
@ -472,6 +475,30 @@ _zsh_highlight_main_highlighter_highlight_list()
fi
fi
# Expand aliases.
# TODO: this should be done iteratively, e.g., 'alias x=y y=z z=w\n x'
# And then the entire 'alias' branch of the 'case' statement should
# be done here.
() {
# TODO: path expansion should happen _after_ alias expansion
_zsh_highlight_main_highlighter_expand_path $arg
local expanded_arg="$REPLY"
_zsh_highlight_main__type ${expanded_arg}
}
local res="$REPLY"
if [[ $res == "alias" ]]; then
_zsh_highlight_main__resolve_alias $arg
() {
# Use a temporary array to ensure the subscript is interpreted as
# an array subscript, not as a scalar subscript
local -a reply
# TODO: the ${interactive_comments+set} path needs to skip comments; see test-data/alias-comment1.zsh
reply=( ${interactive_comments-${(z)REPLY}}
${interactive_comments+${(zZ+c+)REPLY}} )
arg=$reply[1]
}
fi
# Special-case the first word after 'sudo'.
if (( ! in_redirection )); then
if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then
@ -512,10 +539,6 @@ _zsh_highlight_main_highlighter_highlight_list()
next_word+=':sudo_opt:'
next_word+=':start:'
else
_zsh_highlight_main_highlighter_expand_path $arg
local expanded_arg="$REPLY"
_zsh_highlight_main__type ${expanded_arg}
local res="$REPLY"
() {
# Special-case: command word is '$foo', like that, without braces or anything.
#
@ -588,8 +611,9 @@ _zsh_highlight_main_highlighter_highlight_list()
;;
'suffix alias') style=suffix-alias;;
alias) () {
# Make sure to use $arg_raw here, rather than $arg.
integer insane_alias
case $arg in
case $arg_raw in
# Issue #263: aliases with '=' on their LHS.
#
# There are three cases:
@ -603,12 +627,13 @@ _zsh_highlight_main_highlighter_highlight_list()
esac
if (( insane_alias )); then
style=unknown-token
# Calling 'type' again; since __type memoizes the answer, this call is just a hash lookup.
elif _zsh_highlight_main__type "$arg"; [[ $REPLY == 'none' ]]; then
style=unknown-token
else
# The common case.
style=alias
_zsh_highlight_main__resolve_alias $arg
local alias_target="$REPLY"
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$alias_target"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg)
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg_raw"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg_raw)
fi
}
;;

View File

@ -0,0 +1,37 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2016 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
# -------------------------------------------------------------------------------------------------
# see alias-comment2.zsh
setopt interactivecomments
alias x=$'# foo\npwd'
BUFFER='x'
expected_region_highlight=(
"1 1 alias 'interactivecomments applies to aliases'" # x becomes pwd
)

View File

@ -0,0 +1,37 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2016 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
# -------------------------------------------------------------------------------------------------
# see alias-comment1.zsh
setopt NO_interactivecomments
alias x=$'# foo\npwd'
BUFFER='x'
expected_region_highlight=(
"1 1 unknown-token" # x becomes #
)

View File

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

View File

@ -27,7 +27,7 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
alias a=A
alias a=:
f() {}
BUFFER='a;f;'