Skip to content

Commit 5545fb9

Browse files
committed
'main': In command position, do not highlight directories (unless AUTO_CD is set) and non-executable files.
Fixes #202. Test expectations are updated. For example, BUFFER='/bin' is now highlighted as path_prefix because it's a prefix of '/bin/sh' which would be valid. However, BUFFER='/bin;' is now properly highlighted as an error (unless AUTO_CD is set).
1 parent a6eb966 commit 5545fb9

8 files changed

+118
-18
lines changed

highlighters/main/main-highlighter.zsh

+34-9
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ _zsh_highlight_main_highlighter_highlight_list()
10061006
fi
10071007
_zsh_highlight_main__stack_pop 'R' reserved-word
10081008
else
1009-
if _zsh_highlight_main_highlighter_check_path $arg; then
1009+
if _zsh_highlight_main_highlighter_check_path $arg 1; then
10101010
style=$REPLY
10111011
else
10121012
style=unknown-token
@@ -1115,12 +1115,19 @@ _zsh_highlight_main_highlighter_highlight_path_separators()
11151115
# Check if $1 is a path.
11161116
# If yes, return 0 and in $REPLY the style to use.
11171117
# Else, return non-zero (and the contents of $REPLY is undefined).
1118+
#
1119+
# $2 should be non-zero iff we're in command position.
11181120
_zsh_highlight_main_highlighter_check_path()
11191121
{
11201122
_zsh_highlight_main_highlighter_expand_path "$1"
11211123
local expanded_path="$REPLY" tmp_path
1124+
integer in_command_position=$2
11221125

1123-
REPLY=path
1126+
if (( in_command_position )); then
1127+
REPLY=arg0
1128+
else
1129+
REPLY=path
1130+
fi
11241131

11251132
if [[ ${1[1]} == '=' && $1 == ??* && ${1[2]} != $'\x28' && $zsyh_user_options[equals] == 'on' && $expanded_path[1] != '/' ]]; then
11261133
REPLY=unknown-token # will error out if executed
@@ -1142,15 +1149,23 @@ _zsh_highlight_main_highlighter_check_path()
11421149
tmp_path=$tmp_path:h
11431150
done
11441151

1145-
[[ -L $expanded_path ]] && return 0
1146-
[[ -e $expanded_path ]] && return 0
1152+
if (( in_command_position )); then
1153+
if [[ -x $expanded_path ]] && [[ $zsyh_user_options[autocd] == on || ! -d $expanded_path ]]; then
1154+
return 0
1155+
fi
1156+
else
1157+
if [[ -L $expanded_path || -e $expanded_path ]]; then
1158+
return 0
1159+
fi
1160+
fi
11471161

11481162
# Search the path in CDPATH
1149-
if [[ $expanded_path != /* ]]; then
1150-
local cdpath_dir
1163+
if [[ $expanded_path != /* ]] &&
1164+
{ (( ! in_command_position )) || [[ $zsyh_user_options[autocd] == on ]] }; then
11511165
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
1166+
local cdpath_dir
11521167
for cdpath_dir in $cdpath ; do
1153-
[[ -e "$cdpath_dir/$expanded_path" ]] && return 0
1168+
[[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]] && return 0
11541169
done
11551170
fi
11561171

@@ -1162,7 +1177,14 @@ _zsh_highlight_main_highlighter_check_path()
11621177
[[ $WIDGET != zle-line-finish ]]; then
11631178
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
11641179
local -a tmp
1165-
tmp=( ${expanded_path}*(N) )
1180+
if (( in_command_position )); then
1181+
# We include directories even when autocd is enabled, because those
1182+
# directories might contain executable files: e.g., BUFFER="/bi" en route
1183+
# to typing "/bin/sh".
1184+
tmp=( ${expanded_path}*(N-*,N-/) )
1185+
else
1186+
tmp=( ${expanded_path}*(N) )
1187+
fi
11661188
(( ${+tmp[1]} )) && REPLY=path_prefix && return 0
11671189
fi
11681190

@@ -1173,6 +1195,8 @@ _zsh_highlight_main_highlighter_check_path()
11731195
# Highlight an argument and possibly special chars in quotes starting at $1 in $arg
11741196
# This command will at least highlight $1 to end_pos with the default style
11751197
# If $2 is set to 0, the argument cannot be highlighted as an option.
1198+
#
1199+
# This function currently assumes it's never called for the command word.
11761200
_zsh_highlight_main_highlighter_highlight_argument()
11771201
{
11781202
local base_style=default i=$1 option_eligible=${2:-1} path_eligible=1 ret start style
@@ -1307,7 +1331,8 @@ _zsh_highlight_main_highlighter_highlight_argument()
13071331
else
13081332
base_style=numeric-fd
13091333
fi
1310-
elif _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then
1334+
# This function is currently never called for the command word, so $2 is hard-coded as 0.
1335+
elif _zsh_highlight_main_highlighter_check_path $arg[$1,-1] 0; then
13111336
base_style=$REPLY
13121337
_zsh_highlight_main_highlighter_highlight_path_separators $base_style
13131338
highlights+=($reply)

highlighters/main/test-data/abspath-in-command-position1.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
BUFFER=$'/'
3232

3333
expected_region_highlight=(
34-
'1 1 path' # /
34+
'1 1 path_prefix' # /
3535
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
setopt autocd
32+
BUFFER=$'/'
33+
34+
expected_region_highlight=(
35+
'1 1 arg0' # /
36+
)

highlighters/main/test-data/abspath-in-command-position3.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
BUFFER=$'/bin; /bin'
3232

3333
expected_region_highlight=(
34-
'1 4 path' # /bin (in middle)
34+
'1 4 unknown-token' # /bin (in middle)
3535
'5 5 commandseparator' # ;
36-
'7 10 path' # /bin (at end)
36+
'7 10 path_prefix' # /bin (at end)
3737
)

highlighters/main/test-data/abspath-in-command-position3b.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ setopt autocd
3232
BUFFER=$'/bin; /bin'
3333

3434
expected_region_highlight=(
35-
'1 4 path' # /bin (in middle)
35+
'1 4 arg0' # /bin (in middle)
3636
'5 5 commandseparator' # ;
37-
'7 10 path' # /bin (at end)
37+
'7 10 arg0' # /bin (at end)
3838
)

highlighters/main/test-data/path-dollared-word3.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
BUFFER='$PWD; ${PWD}'
3535

3636
expected_region_highlight=(
37-
"1 4 path" # $PWD
37+
"1 4 unknown-token" # $PWD (without AUTO_CD)
3838
"5 5 commandseparator" # ;
39-
"7 12 path" # ${PWD}
39+
"7 12 path_prefix" # ${PWD}
4040
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
setopt autocd
32+
33+
BUFFER=$'$PWD; ${PWD}'
34+
35+
expected_region_highlight=(
36+
'1 4 arg0' # $PWD
37+
'5 5 commandseparator' # ;
38+
'7 12 arg0' # ${PWD}
39+
)

highlighters/main/test-data/plain-file-in-command-position.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ chmod -x foo
3333
BUFFER=$'./foo; ./foo'
3434

3535
expected_region_highlight=(
36-
'1 5 unknown-token "issue #202"' # ./foo (in middle)
36+
'1 5 unknown-token' # ./foo (in middle)
3737
'6 6 commandseparator' # ;
38-
'8 12 unknown-token "issue #202"' # ./foo (at end)
38+
'8 12 unknown-token' # ./foo (at end)
3939
)

0 commit comments

Comments
 (0)