Skip to content

Commit f3410c5

Browse files
danielshahafphy1729
authored andcommitted
'main': Expand aliases first. (Issue #264.)
This commit causes an alias to an invalid command to be highlighted as an error (unknown-token).
1 parent cf88b63 commit f3410c5

File tree

5 files changed

+110
-11
lines changed

5 files changed

+110
-11
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ _zsh_highlight_highlighter_main_paint()
299299
_zsh_highlight_main_highlighter_highlight_list()
300300
{
301301
integer start_pos=0 end_pos buf_offset=$1 has_end=$3
302-
local buf=$4 highlight_glob=true arg style
302+
local buf=$4 highlight_glob=true arg arg_raw style
303303
local in_array_assignment=false # true between 'a=(' and the matching ')'
304304
integer len=$#buf
305305
local -a match mbegin mend list_highlights
@@ -362,6 +362,9 @@ _zsh_highlight_main_highlighter_highlight_list()
362362
args=(${(z)buf})
363363
fi
364364
for arg in $args; do
365+
# Save an unmunged copy of the current word.
366+
arg_raw="$arg"
367+
365368
# Initialize $next_word.
366369
if (( in_redirection )); then
367370
(( --in_redirection ))
@@ -472,6 +475,30 @@ _zsh_highlight_main_highlighter_highlight_list()
472475
fi
473476
fi
474477

478+
# Expand aliases.
479+
# TODO: this should be done iteratively, e.g., 'alias x=y y=z z=w\n x'
480+
# And then the entire 'alias' branch of the 'case' statement should
481+
# be done here.
482+
() {
483+
# TODO: path expansion should happen _after_ alias expansion
484+
_zsh_highlight_main_highlighter_expand_path $arg
485+
local expanded_arg="$REPLY"
486+
_zsh_highlight_main__type ${expanded_arg}
487+
}
488+
local res="$REPLY"
489+
if [[ $res == "alias" ]]; then
490+
_zsh_highlight_main__resolve_alias $arg
491+
() {
492+
# Use a temporary array to ensure the subscript is interpreted as
493+
# an array subscript, not as a scalar subscript
494+
local -a reply
495+
# TODO: the ${interactive_comments+set} path needs to skip comments; see test-data/alias-comment1.zsh
496+
reply=( ${interactive_comments-${(z)REPLY}}
497+
${interactive_comments+${(zZ+c+)REPLY}} )
498+
arg=$reply[1]
499+
}
500+
fi
501+
475502
# Special-case the first word after 'sudo'.
476503
if (( ! in_redirection )); then
477504
if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then
@@ -512,10 +539,6 @@ _zsh_highlight_main_highlighter_highlight_list()
512539
next_word+=':sudo_opt:'
513540
next_word+=':start:'
514541
else
515-
_zsh_highlight_main_highlighter_expand_path $arg
516-
local expanded_arg="$REPLY"
517-
_zsh_highlight_main__type ${expanded_arg}
518-
local res="$REPLY"
519542
() {
520543
# Special-case: command word is '$foo', like that, without braces or anything.
521544
#
@@ -588,8 +611,9 @@ _zsh_highlight_main_highlighter_highlight_list()
588611
;;
589612
'suffix alias') style=suffix-alias;;
590613
alias) () {
614+
# Make sure to use $arg_raw here, rather than $arg.
591615
integer insane_alias
592-
case $arg in
616+
case $arg_raw in
593617
# Issue #263: aliases with '=' on their LHS.
594618
#
595619
# There are three cases:
@@ -603,12 +627,13 @@ _zsh_highlight_main_highlighter_highlight_list()
603627
esac
604628
if (( insane_alias )); then
605629
style=unknown-token
630+
# Calling 'type' again; since __type memoizes the answer, this call is just a hash lookup.
631+
elif _zsh_highlight_main__type "$arg"; [[ $REPLY == 'none' ]]; then
632+
style=unknown-token
606633
else
607634
# The common case.
608635
style=alias
609-
_zsh_highlight_main__resolve_alias $arg
610-
local alias_target="$REPLY"
611-
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$alias_target"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg)
636+
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg_raw"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg_raw)
612637
fi
613638
}
614639
;;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -------------------------------------------------------------------------------------------------
2+
# Copyright (c) 2016 zsh-syntax-highlighting contributors
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without modification, are permitted
6+
# provided that the following conditions are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
9+
# and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
11+
# conditions and the following disclaimer in the documentation and/or other materials provided
12+
# with the distribution.
13+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software without specific prior
15+
# written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# -------------------------------------------------------------------------------------------------
26+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
27+
# vim: ft=zsh sw=2 ts=2 et
28+
# -------------------------------------------------------------------------------------------------
29+
30+
# see alias-comment2.zsh
31+
setopt interactivecomments
32+
alias x=$'# foo\npwd'
33+
BUFFER='x'
34+
35+
expected_region_highlight=(
36+
"1 1 alias 'interactivecomments applies to aliases'" # x becomes pwd
37+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -------------------------------------------------------------------------------------------------
2+
# Copyright (c) 2016 zsh-syntax-highlighting contributors
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without modification, are permitted
6+
# provided that the following conditions are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
9+
# and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
11+
# conditions and the following disclaimer in the documentation and/or other materials provided
12+
# with the distribution.
13+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software without specific prior
15+
# written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# -------------------------------------------------------------------------------------------------
26+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
27+
# vim: ft=zsh sw=2 ts=2 et
28+
# -------------------------------------------------------------------------------------------------
29+
30+
# see alias-comment1.zsh
31+
setopt NO_interactivecomments
32+
alias x=$'# foo\npwd'
33+
BUFFER='x'
34+
35+
expected_region_highlight=(
36+
"1 1 unknown-token" # x becomes #
37+
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ alias x=command
3131
BUFFER='x ls'
3232

3333
expected_region_highlight=(
34-
"1 1 alias" # x
34+
"1 1 precommand" # x
3535
"3 4 command" # ls
3636
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# vim: ft=zsh sw=2 ts=2 et
2828
# -------------------------------------------------------------------------------------------------
2929

30-
alias a=A
30+
alias a=:
3131
f() {}
3232

3333
BUFFER='a;f;'

0 commit comments

Comments
 (0)