Skip to content

Commit b47387b

Browse files
committed
'main': Apply the parameter expansion logic regardless of the type
The existing check was bogus: parameter expansion doesn't depend on whether there happens to be a command literally called «$foo». This enables the parameter elision logic to kick in for words not in command position. Fixes #239.
1 parent 2aca4e2 commit b47387b

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
highlighted as errors.
1818
[#651, 81267ca3130c]
1919

20-
- Support parameter elision in command position (e.g., `$foo ls` where `$foo` is unset or empty)
21-
[#667]
20+
- Support parameter elision (e.g., `cd $foo` where `$foo` is unset or empty)
21+
[#667, #239]
2222

2323
- Don't consider the filename in `sudo -e /path/to/file` to be a command position
2424
[#678]

highlighters/main/main-highlighter.zsh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ _zsh_highlight_highlighter_main_paint()
422422

423423
# Try to expand $1, if it's possible to do so safely.
424424
#
425-
# Uses two parameters from the caller: $parameter_name_pattern and $res.
425+
# Uses one parameter from the caller: $parameter_name_pattern.
426426
#
427427
# If expansion was done, set $reply to the expansion and return true.
428428
# Otherwise, return false.
@@ -447,7 +447,7 @@ _zsh_highlight_main_highlighter__try_expand_parameter()
447447
else
448448
parameter_name=${arg:1}
449449
fi
450-
if [[ $res == none ]] && zmodload -e zsh/parameter &&
450+
if zmodload -e zsh/parameter &&
451451
[[ ${parameter_name} =~ ^${~parameter_name_pattern}$ ]] &&
452452
[[ ${parameters[(e)$MATCH]} != *special* ]]
453453
then
@@ -745,8 +745,10 @@ _zsh_highlight_main_highlighter_highlight_list()
745745
(( in_param = 1 + $#words ))
746746
args=( $words $args )
747747
arg=$args[1]
748-
_zsh_highlight_main__type "$arg" 0
749-
res=$REPLY
748+
if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then
749+
_zsh_highlight_main__type "$arg" 0
750+
res=$REPLY
751+
fi
750752
fi
751753
}
752754
fi

highlighters/main/test-data/brackets-mismatch7.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ expected_region_highlight=(
3939
'11 11 commandseparator' # ;
4040
'13 14 reserved-word' # do
4141
'16 19 builtin' # echo
42-
'21 22 default' # $n
42+
'21 22 comment' # $n - because it's unset when the line is parsed
4343
'23 23 commandseparator' # ;
4444
'25 27 unknown-token' # end
4545
)

highlighters/main/test-data/order-path-after-dollar.zsh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# vim: ft=zsh sw=2 ts=2 et
2929
# -------------------------------------------------------------------------------------------------
3030

31+
local foo='is set'
3132
touch '$foo'
3233
BUFFER=': $foo \$foo'
3334

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
BUFFER=$': $foo ${bar}'
32+
33+
expected_region_highlight=(
34+
'1 1 builtin' # :
35+
'3 6 comment' # $foo
36+
'8 13 comment' # ${bar}
37+
)

0 commit comments

Comments
 (0)