'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:
committed by
Daniel Shahaf
parent
ff61a496b5
commit
77fe96772e
@@ -214,7 +214,7 @@ _zsh_highlight_highlighter_main_paint()
|
||||
fi
|
||||
|
||||
## Variable declarations and initializations
|
||||
local start_pos=0 end_pos highlight_glob=true arg style
|
||||
local start_pos=0 end_pos highlight_glob=true arg arg_raw style
|
||||
local in_array_assignment=false # true between 'a=(' and the matching ')'
|
||||
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
|
||||
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
|
||||
@@ -322,6 +322,9 @@ _zsh_highlight_highlighter_main_paint()
|
||||
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 ))
|
||||
@@ -432,6 +435,30 @@ _zsh_highlight_highlighter_main_paint()
|
||||
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
|
||||
@@ -472,10 +499,6 @@ _zsh_highlight_highlighter_main_paint()
|
||||
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.
|
||||
#
|
||||
@@ -548,8 +571,9 @@ _zsh_highlight_highlighter_main_paint()
|
||||
;;
|
||||
'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:
|
||||
@@ -563,12 +587,13 @@ _zsh_highlight_highlighter_main_paint()
|
||||
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
|
||||
}
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user