From b47387b55eaf3867f8c4d296b87e9d4d0de174a3 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 19 Mar 2020 22:06:21 +0000 Subject: [PATCH 1/4] 'main': Apply the parameter expansion logic regardless of the type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- changelog.md | 4 +- highlighters/main/main-highlighter.zsh | 10 +++-- .../main/test-data/brackets-mismatch7.zsh | 2 +- .../test-data/order-path-after-dollar.zsh | 1 + .../main/test-data/parameter-elision-argv.zsh | 37 +++++++++++++++++++ 5 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 highlighters/main/test-data/parameter-elision-argv.zsh diff --git a/changelog.md b/changelog.md index 496b4a8..be2320a 100644 --- a/changelog.md +++ b/changelog.md @@ -17,8 +17,8 @@ highlighted as errors. [#651, 81267ca3130c] -- Support parameter elision in command position (e.g., `$foo ls` where `$foo` is unset or empty) - [#667] +- Support parameter elision (e.g., `cd $foo` where `$foo` is unset or empty) + [#667, #239] - Don't consider the filename in `sudo -e /path/to/file` to be a command position [#678] diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 10abbb9..eea1d79 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -422,7 +422,7 @@ _zsh_highlight_highlighter_main_paint() # 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. # Otherwise, return false. @@ -447,7 +447,7 @@ _zsh_highlight_main_highlighter__try_expand_parameter() else parameter_name=${arg:1} fi - if [[ $res == none ]] && zmodload -e zsh/parameter && + if zmodload -e zsh/parameter && [[ ${parameter_name} =~ ^${~parameter_name_pattern}$ ]] && [[ ${parameters[(e)$MATCH]} != *special* ]] then @@ -745,8 +745,10 @@ _zsh_highlight_main_highlighter_highlight_list() (( in_param = 1 + $#words )) args=( $words $args ) arg=$args[1] - _zsh_highlight_main__type "$arg" 0 - res=$REPLY + if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then + _zsh_highlight_main__type "$arg" 0 + res=$REPLY + fi fi } fi diff --git a/highlighters/main/test-data/brackets-mismatch7.zsh b/highlighters/main/test-data/brackets-mismatch7.zsh index 1caa936..7499821 100644 --- a/highlighters/main/test-data/brackets-mismatch7.zsh +++ b/highlighters/main/test-data/brackets-mismatch7.zsh @@ -39,7 +39,7 @@ expected_region_highlight=( '11 11 commandseparator' # ; '13 14 reserved-word' # do '16 19 builtin' # echo - '21 22 default' # $n + '21 22 comment' # $n - because it's unset when the line is parsed '23 23 commandseparator' # ; '25 27 unknown-token' # end ) diff --git a/highlighters/main/test-data/order-path-after-dollar.zsh b/highlighters/main/test-data/order-path-after-dollar.zsh index 773c183..4b922f0 100644 --- a/highlighters/main/test-data/order-path-after-dollar.zsh +++ b/highlighters/main/test-data/order-path-after-dollar.zsh @@ -28,6 +28,7 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- +local foo='is set' touch '$foo' BUFFER=': $foo \$foo' diff --git a/highlighters/main/test-data/parameter-elision-argv.zsh b/highlighters/main/test-data/parameter-elision-argv.zsh new file mode 100644 index 0000000..fd030cb --- /dev/null +++ b/highlighters/main/test-data/parameter-elision-argv.zsh @@ -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} +) From 78f40ee2b672c810842d94e9ffbeed6577ce91dd Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 28 Mar 2020 04:28:52 +0000 Subject: [PATCH 2/4] fixup! 'main': Apply the parameter expansion logic regardless of the type --- highlighters/main/main-highlighter.zsh | 2 +- .../test-data/param-argument-specials.zsh | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 highlighters/main/test-data/param-argument-specials.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index eea1d79..c4b71ae 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -1299,7 +1299,7 @@ _zsh_highlight_main_highlighter_highlight_argument() fi esac - for (( ; i <= $#arg ; i += 1 )); do + for (( ; i <= $#arg && ! in_param ; i += 1 )); do case "$arg[$i]" in "\\") (( i += 1 )); continue;; "'") diff --git a/highlighters/main/test-data/param-argument-specials.zsh b/highlighters/main/test-data/param-argument-specials.zsh new file mode 100644 index 0000000..2195da0 --- /dev/null +++ b/highlighters/main/test-data/param-argument-specials.zsh @@ -0,0 +1,38 @@ +#!/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 +# ------------------------------------------------------------------------------------------------- + +local s='`foo`' + +BUFFER=$': $s' + +expected_region_highlight=( + '1 1 builtin' # : + '3 4 default' # $s +) From d0a11ba837ef4bb094207391ab4f8f3deb9697cc Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 28 Mar 2020 04:29:30 +0000 Subject: [PATCH 3/4] WIP: 'main': Add some in_param/in_alias checks by code inspection --- highlighters/main/main-highlighter.zsh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index c4b71ae..1cef81b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -1141,6 +1141,9 @@ _zsh_highlight_main_highlighter_check_assign() _zsh_highlight_main_highlighter_highlight_path_separators() { + if (( in_param || in_alias )); then + return + fi local pos style_pathsep style_pathsep=$1_pathseparator reply=() @@ -1386,7 +1389,7 @@ _zsh_highlight_main_highlighter_highlight_argument() done if (( path_eligible )); then - if (( in_redirection )) && [[ $last_arg == *['<>']['&'] && $arg[$1,-1] == (<0->|p|-) ]]; then + if (( in_redirection && ! in_param )) && [[ $last_arg == *['<>']['&'] && $arg[$1,-1] == (<0->|p|-) ]]; then if [[ $arg[$1,-1] == (p|-) ]]; then base_style=redirection else From b7a9e29cf0e0fb57c8b5836657fe29212bccac7e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 28 Mar 2020 04:58:28 +0000 Subject: [PATCH 4/4] fixup! WIP: 'main': Add some in_param/in_alias checks by code inspection --- highlighters/main/main-highlighter.zsh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 1cef81b..de52167 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -1141,13 +1141,14 @@ _zsh_highlight_main_highlighter_check_assign() _zsh_highlight_main_highlighter_highlight_path_separators() { - if (( in_param || in_alias )); then - return - fi local pos style_pathsep style_pathsep=$1_pathseparator reply=() - [[ -z "$ZSH_HIGHLIGHT_STYLES[$style_pathsep]" || "$ZSH_HIGHLIGHT_STYLES[$1]" == "$ZSH_HIGHLIGHT_STYLES[$style_pathsep]" ]] && return 0 + if (( in_param || in_alias )) || + [[ -z "$ZSH_HIGHLIGHT_STYLES[$style_pathsep]" || "$ZSH_HIGHLIGHT_STYLES[$1]" == "$ZSH_HIGHLIGHT_STYLES[$style_pathsep]" ]] + then + return 0 + fi for (( pos = start_pos; $pos <= end_pos; pos++ )) ; do if [[ $BUFFER[pos] == / ]]; then reply+=($((pos - 1)) $pos $style_pathsep)