'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.
This commit is contained in:
Daniel Shahaf 2020-03-19 22:06:21 +00:00
parent 2aca4e2c02
commit b47387b55e
5 changed files with 47 additions and 7 deletions

View File

@ -17,8 +17,8 @@
highlighted as errors. highlighted as errors.
[#651, 81267ca3130c] [#651, 81267ca3130c]
- Support parameter elision in command position (e.g., `$foo ls` where `$foo` is unset or empty) - Support parameter elision (e.g., `cd $foo` where `$foo` is unset or empty)
[#667] [#667, #239]
- Don't consider the filename in `sudo -e /path/to/file` to be a command position - Don't consider the filename in `sudo -e /path/to/file` to be a command position
[#678] [#678]

View File

@ -422,7 +422,7 @@ _zsh_highlight_highlighter_main_paint()
# Try to expand $1, if it's possible to do so safely. # Try to expand $1, if it's possible to do so safely.
# #
# Uses two parameters from the caller: $parameter_name_pattern and $res. # Uses one parameter from the caller: $parameter_name_pattern.
# #
# If expansion was done, set $reply to the expansion and return true. # If expansion was done, set $reply to the expansion and return true.
# Otherwise, return false. # Otherwise, return false.
@ -447,7 +447,7 @@ _zsh_highlight_main_highlighter__try_expand_parameter()
else else
parameter_name=${arg:1} parameter_name=${arg:1}
fi fi
if [[ $res == none ]] && zmodload -e zsh/parameter && if zmodload -e zsh/parameter &&
[[ ${parameter_name} =~ ^${~parameter_name_pattern}$ ]] && [[ ${parameter_name} =~ ^${~parameter_name_pattern}$ ]] &&
[[ ${parameters[(e)$MATCH]} != *special* ]] [[ ${parameters[(e)$MATCH]} != *special* ]]
then then
@ -745,9 +745,11 @@ _zsh_highlight_main_highlighter_highlight_list()
(( in_param = 1 + $#words )) (( in_param = 1 + $#words ))
args=( $words $args ) args=( $words $args )
arg=$args[1] arg=$args[1]
if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then
_zsh_highlight_main__type "$arg" 0 _zsh_highlight_main__type "$arg" 0
res=$REPLY res=$REPLY
fi fi
fi
} }
fi fi

View File

@ -39,7 +39,7 @@ expected_region_highlight=(
'11 11 commandseparator' # ; '11 11 commandseparator' # ;
'13 14 reserved-word' # do '13 14 reserved-word' # do
'16 19 builtin' # echo '16 19 builtin' # echo
'21 22 default' # $n '21 22 comment' # $n - because it's unset when the line is parsed
'23 23 commandseparator' # ; '23 23 commandseparator' # ;
'25 27 unknown-token' # end '25 27 unknown-token' # end
) )

View File

@ -28,6 +28,7 @@
# vim: ft=zsh sw=2 ts=2 et # vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
local foo='is set'
touch '$foo' touch '$foo'
BUFFER=': $foo \$foo' BUFFER=': $foo \$foo'

View File

@ -0,0 +1,37 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020 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
# -------------------------------------------------------------------------------------------------
BUFFER=$': $foo ${bar}'
expected_region_highlight=(
'1 1 builtin' # :
'3 6 comment' # $foo
'8 13 comment' # ${bar}
)