From a2a1e137dd086568d0823e15c7cc295f407d5e10 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 17:39:12 +0000 Subject: [PATCH 01/11] 'main': Pass parameters through the multi-word machinery, as we already do for aliases. Fixes #674. --- highlighters/main/main-highlighter.zsh | 31 +++++++++++++++---- .../param-precommand-option-argument1.zsh | 2 +- .../param-precommand-option-argument3.zsh | 4 +-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a4ff7a1a2..eee1c2374 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -77,6 +77,10 @@ _zsh_highlight_main_add_region_highlight() { [[ $1 == unknown-token ]] && alias_style=unknown-token return fi + if (( in_param )); then + [[ $1 == unknown-token ]] && param_style=unknown-token + return + fi # The calculation was relative to $buf but region_highlight is relative to $BUFFER. (( start += buf_offset )) @@ -406,11 +410,13 @@ _zsh_highlight_main_highlighter_highlight_list() # alias_style is the style to apply to an alias once in_alias=0 # Usually 'alias' but set to 'unknown-token' if any word expanded from # the alias would be highlighted as unknown-token - local alias_style arg buf=$4 highlight_glob=true style + # param_style is analogous for parameter expansions + local alias_style param_style arg buf=$4 highlight_glob=true style local in_array_assignment=false # true between 'a=(' and the matching ')' # in_alias is equal to the number of shifts needed until arg=args[1] pops an # arg from BUFFER and not added by an alias. - integer in_alias=0 len=$#buf + # in_param is analogous for parameter expansions + integer in_alias=0 in_param=0 len=$#buf local -a match mbegin mend list_highlights # seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a local -A seen_alias @@ -489,6 +495,14 @@ _zsh_highlight_main_highlighter_highlight_list() _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style fi fi + if (( in_param )); then + (( in_param-- )) + if (( in_param == 0 )); then + # start_pos and end_pos are of the alias (previous $arg) here + _zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-"unknown-token"} + param_style="" + fi + fi # Initialize this_word and next_word. if (( in_redirection == 0 )); then @@ -513,7 +527,7 @@ _zsh_highlight_main_highlighter_highlight_list() fi fi - if (( in_alias == 0 )); then + if (( in_alias == 0 && in_param == 0 )); then # Compute the new $start_pos and $end_pos, skipping over whitespace in $buf. [[ "$proc_buf" = (#b)(#s)(([ $'\t']|\\$'\n')#)* ]] # The first, outer parenthesis @@ -626,6 +640,7 @@ _zsh_highlight_main_highlighter_highlight_list() local -a match mbegin mend local MATCH; integer MBEGIN MEND local parameter_name + local -a words if [[ $arg[1] == '$' ]] && [[ ${arg[2]} == '{' ]] && [[ ${arg[-1]} == '}' ]]; then parameter_name=${${arg:2}%?} elif [[ $arg[1] == '$' ]]; then @@ -638,14 +653,16 @@ _zsh_highlight_main_highlighter_highlight_list() # Set $arg. case ${(tP)MATCH} in (*array*|*assoc*) - local -a words; words=( ${(P)MATCH} ) - arg=${words[1]} + words=( ${(P)MATCH} ) ;; (*) # scalar, presumably - arg=${(P)MATCH} + words=( ${(P)MATCH} ) ;; esac + (( in_param = 1 + $#words )) + args=( $words $args ) + arg=$args[1] _zsh_highlight_main__type "$arg" 0 res=$REPLY fi @@ -888,6 +905,7 @@ _zsh_highlight_main_highlighter_highlight_list() if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then next_word=':start::start_of_pipeline:' fi + : ${param_style:=$style} else # $arg is a non-command word case $arg in $'\x29') # subshell or end of array assignment @@ -946,6 +964,7 @@ _zsh_highlight_main_highlighter_highlight_list() _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style done (( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style + (( in_param == 1 )) && in_param=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-"unknown-token"} [[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]] REPLY=$(( end_pos + ${#match[1]} - 1 )) reply=($list_highlights) diff --git a/highlighters/main/test-data/param-precommand-option-argument1.zsh b/highlighters/main/test-data/param-precommand-option-argument1.zsh index 58e51efbc..84cf03acc 100644 --- a/highlighters/main/test-data/param-precommand-option-argument1.zsh +++ b/highlighters/main/test-data/param-precommand-option-argument1.zsh @@ -36,7 +36,7 @@ BUFFER='$sudo_u phy1729 echo foo' expected_region_highlight=( '1 7 precommand' # $sudo_u - '9 15 default "issue #674"' # phy1729 + '9 15 default' # phy1729 '18 20 command "issue #540"' # echo (not builtin) '22 24 default' # foo ) diff --git a/highlighters/main/test-data/param-precommand-option-argument3.zsh b/highlighters/main/test-data/param-precommand-option-argument3.zsh index ea5035e7f..9a8dde283 100644 --- a/highlighters/main/test-data/param-precommand-option-argument3.zsh +++ b/highlighters/main/test-data/param-precommand-option-argument3.zsh @@ -36,7 +36,7 @@ BUFFER='$sudo_u phy1729 ls foo' expected_region_highlight=( '1 7 precommand' # sudo_u - '9 15 default "issue #674"' # phy1729 - '17 18 command "issue #674"' # ls + '9 15 default' # phy1729 + '17 18 command' # ls '20 22 default' # foo ) From 2d0f51b54ec18b8962d5dcef358b25258141ac7a Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 19:02:39 +0000 Subject: [PATCH 02/11] WIP: tests: Add tests for issue #670. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They fail as follows: ZSH_PATCHLEVEL=debian/5.7.1-1 Running test brackets Running test main # parameter-value-contains-command-position1 1..2 not ok 1 - [1,7] «$foobar» - expected (1 7 "assign"), observed (1 7 "unknown-token"). ok 2 - cardinality check # parameter-value-contains-command-position2 1..2 not ok 1 - [1,2] «$y» - expected (1 2 "assign"), observed (1 2 "unknown-token"). ok 2 - cardinality check (The "unknown-token" was printed by the `_zsh_highlight_main_add_region_highlight` call on line 967, after the loop.) Before the parent commit, they behaved as follows (note the "BUG:" and "Bail out!" on the first one — that's issue #670.2): ZSH_PATCHLEVEL=debian/5.7.1-1 # parameter-value-contains-command-position1 1..2 ok 1 - [1,7] «$foobar» - # TODO "issue #670" not ok 2 - have 1 expectations and 6 region_highlight entries: «expected_region_highlight=( '1 7 assign "issue ♯670"' )» «region_highlight=( '0 7 assign' '2 7 default' '2 7 command-substitution-unquoted' zsh-syntax-highlighting: BUG: _zsh_highlight_highlighter_main_paint: start(2) >= end(2) Bail out! On './highlighters/main/test-data/parameter-value-contains-command-position2.zsh': output on stderr # parameter-value-contains-command-position2 1..2 ok 1 - [1,2] «$y» - # TODO "issue #670" ok 2 - cardinality check --- ...meter-value-contains-command-position1.zsh | 38 +++++++++++++++++++ ...meter-value-contains-command-position2.zsh | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 highlighters/main/test-data/parameter-value-contains-command-position1.zsh create mode 100644 highlighters/main/test-data/parameter-value-contains-command-position2.zsh diff --git a/highlighters/main/test-data/parameter-value-contains-command-position1.zsh b/highlighters/main/test-data/parameter-value-contains-command-position1.zsh new file mode 100644 index 000000000..b4a8a5584 --- /dev/null +++ b/highlighters/main/test-data/parameter-value-contains-command-position1.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 foobar='x=$(ls)' + +BUFFER=$'$foobar' + +expected_region_highlight=( + # Used to highlight the "ba" as 'command' because the 'ls' showed through; issues #670 and #674 + '1 7 assign' # $foobar +) diff --git a/highlighters/main/test-data/parameter-value-contains-command-position2.zsh b/highlighters/main/test-data/parameter-value-contains-command-position2.zsh new file mode 100644 index 000000000..eca36d45b --- /dev/null +++ b/highlighters/main/test-data/parameter-value-contains-command-position2.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 y='x=$(ls)' + +BUFFER=$'$y' + +expected_region_highlight=( + # Used to trigger a "BUG" message on stderr - issues #670 and #674 + '1 2 assign' # $y +) From bc3b94349d0a401598560ee8c462f00b9616dff6 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 19:31:55 +0000 Subject: [PATCH 03/11] WIP: Snapshot that *almost* passes tests, but may or may not be correct. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current test failures: Running test main # parameter-value-contains-command-position1 1..2 ok 1 - [1,7] «$foobar» not ok 2 - have 1 expectations and 2 region_highlight entries: «expected_region_highlight=( '1 7 assign' )» «region_highlight=( '0 7 assign' '0 7 assign' )» # parameter-value-contains-command-position2 1..2 ok 1 - [1,2] «$y» not ok 2 - have 1 expectations and 2 region_highlight entries: «expected_region_highlight=( '1 2 assign' )» «region_highlight=( '0 2 assign' '0 2 assign' )» --- highlighters/main/main-highlighter.zsh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index eee1c2374..938614b5f 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -78,8 +78,13 @@ _zsh_highlight_main_add_region_highlight() { return fi if (( in_param )); then - [[ $1 == unknown-token ]] && param_style=unknown-token - return + if [[ $1 == unknown-token ]]; then + param_style=unknown-token + fi + if [[ -n $param_style ]]; then + return + fi + param_style=$1 fi # The calculation was relative to $buf but region_highlight is relative to $BUFFER. @@ -963,8 +968,9 @@ _zsh_highlight_main_highlighter_highlight_list() fi _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style done + : ${param_style:=$style} (( in_alias == 1 )) && in_alias=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style - (( in_param == 1 )) && in_param=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos ${param_style:-"unknown-token"} + (( in_param == 1 )) && in_param=0 _zsh_highlight_main_add_region_highlight $start_pos $end_pos $param_style [[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')#) ]] REPLY=$(( end_pos + ${#match[1]} - 1 )) reply=($list_highlights) From aa561299f22bba2b4390a90a1027088873531461 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 20 Feb 2020 11:37:23 +0000 Subject: [PATCH 04/11] 'main': Parameter expansions may not contain assignments. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In «a="b=c"; $a», the '=' sign in the expansion of $a is not active. Therefore, prevent the expansion of $a from being considered an assignment. Update test expectations accordingly. As a side effect, this prevents line 836 from firing for the cases in these two tests, thereby fixing the double $region_highlight addition (see log message of the previous commit). That leaves the line 966 addition. However, the double addition remains a latent bug (see discussion in PR #684). --- highlighters/main/main-highlighter.zsh | 2 +- .../test-data/parameter-value-contains-command-position1.zsh | 2 +- .../test-data/parameter-value-contains-command-position2.zsh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 938614b5f..edb7b4568 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -839,7 +839,7 @@ _zsh_highlight_main_highlighter_highlight_list() function) style=function;; command) style=command;; hashed) style=hashed-command;; - none) if _zsh_highlight_main_highlighter_check_assign; then + none) if (( ! in_param )) && _zsh_highlight_main_highlighter_check_assign; then _zsh_highlight_main_add_region_highlight $start_pos $end_pos assign local i=$(( arg[(i)=] + 1 )) if [[ $arg[i] == '(' ]]; then diff --git a/highlighters/main/test-data/parameter-value-contains-command-position1.zsh b/highlighters/main/test-data/parameter-value-contains-command-position1.zsh index b4a8a5584..05ea1bfae 100644 --- a/highlighters/main/test-data/parameter-value-contains-command-position1.zsh +++ b/highlighters/main/test-data/parameter-value-contains-command-position1.zsh @@ -34,5 +34,5 @@ BUFFER=$'$foobar' expected_region_highlight=( # Used to highlight the "ba" as 'command' because the 'ls' showed through; issues #670 and #674 - '1 7 assign' # $foobar + '1 7 unknown-token' # $foobar (not an assignment) ) diff --git a/highlighters/main/test-data/parameter-value-contains-command-position2.zsh b/highlighters/main/test-data/parameter-value-contains-command-position2.zsh index eca36d45b..5042e8994 100644 --- a/highlighters/main/test-data/parameter-value-contains-command-position2.zsh +++ b/highlighters/main/test-data/parameter-value-contains-command-position2.zsh @@ -34,5 +34,5 @@ BUFFER=$'$y' expected_region_highlight=( # Used to trigger a "BUG" message on stderr - issues #670 and #674 - '1 2 assign' # $y + '1 2 unknown-token' # $y (not an assignment) ) From 65b3166696b1134282defd234d1eae47c3435cac Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 21 Feb 2020 10:17:55 +0000 Subject: [PATCH 05/11] 'main': Add two tests for metacharacters in parameter expansions. Suggested-by: @QBobWatson (in https://github.com/zsh-users/zsh-syntax-highlighting/pull/682#issuecomment-588361771) --- .../parameter-expansion-untokenized1.zsh | 38 +++++++++++++++++++ .../parameter-expansion-untokenized2.zsh | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 highlighters/main/test-data/parameter-expansion-untokenized1.zsh create mode 100644 highlighters/main/test-data/parameter-expansion-untokenized2.zsh diff --git a/highlighters/main/test-data/parameter-expansion-untokenized1.zsh b/highlighters/main/test-data/parameter-expansion-untokenized1.zsh new file mode 100644 index 000000000..f3c9a5e27 --- /dev/null +++ b/highlighters/main/test-data/parameter-expansion-untokenized1.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 x="()" + +BUFFER=$'$x ls' + +expected_region_highlight=( + '1 2 unknown-token "fixed in the next commit"' # $x + '4 5 command' # ls +) diff --git a/highlighters/main/test-data/parameter-expansion-untokenized2.zsh b/highlighters/main/test-data/parameter-expansion-untokenized2.zsh new file mode 100644 index 000000000..638917114 --- /dev/null +++ b/highlighters/main/test-data/parameter-expansion-untokenized2.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 x="^foo^bar" + +BUFFER=$'$x ls' + +expected_region_highlight=( + '1 2 unknown-token "fixed in the next commit"' # $x + '4 5 default' # ls +) From fb074f115936f7152f4fd78b7fac660071b9aea3 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 21 Feb 2020 10:19:51 +0000 Subject: [PATCH 06/11] 'main': Do not look for metacharacters in parameter expansions. Fixes the bug the previous commit added a test for. --- highlighters/main/main-highlighter.zsh | 18 ++++++++++++------ .../parameter-expansion-untokenized1.zsh | 2 +- .../parameter-expansion-untokenized2.zsh | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index edb7b4568..a2818b88c 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -860,11 +860,14 @@ _zsh_highlight_main_highlighter_highlight_list() fi fi continue - elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then + elif (( ! in_param )) && + [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then style=history-expansion - elif [[ $arg[0,1] == $histchars[2,2] ]]; then + elif (( ! in_param )) && + [[ $arg[0,1] == $histchars[2,2] ]]; then style=history-expansion - elif [[ $arg[1,2] == '((' ]]; then + elif (( ! in_param )) && + [[ $arg[1,2] == '((' ]]; then # Arithmetic evaluation. # # Note: prior to zsh-5.1.1-52-g4bed2cf (workers/36669), the ${(z)...} @@ -879,14 +882,17 @@ _zsh_highlight_main_highlighter_highlight_list() _zsh_highlight_main_add_region_highlight $((end_pos - 2)) $end_pos reserved-word fi continue - elif [[ $arg == '()' ]]; then + elif (( ! in_param )) && + [[ $arg == '()' ]]; then # anonymous function style=reserved-word - elif [[ $arg == $'\x28' ]]; then + elif (( ! in_param )) && + [[ $arg == $'\x28' ]]; then # subshell style=reserved-word braces_stack='R'"$braces_stack" - elif [[ $arg == $'\x29' ]]; then + elif (( ! in_param )) && + [[ $arg == $'\x29' ]]; then # end of subshell or command substitution if _zsh_highlight_main__stack_pop 'S'; then REPLY=$start_pos diff --git a/highlighters/main/test-data/parameter-expansion-untokenized1.zsh b/highlighters/main/test-data/parameter-expansion-untokenized1.zsh index f3c9a5e27..8b8b5e4c4 100644 --- a/highlighters/main/test-data/parameter-expansion-untokenized1.zsh +++ b/highlighters/main/test-data/parameter-expansion-untokenized1.zsh @@ -33,6 +33,6 @@ local x="()" BUFFER=$'$x ls' expected_region_highlight=( - '1 2 unknown-token "fixed in the next commit"' # $x + '1 2 unknown-token' # $x '4 5 command' # ls ) diff --git a/highlighters/main/test-data/parameter-expansion-untokenized2.zsh b/highlighters/main/test-data/parameter-expansion-untokenized2.zsh index 638917114..d23a1f213 100644 --- a/highlighters/main/test-data/parameter-expansion-untokenized2.zsh +++ b/highlighters/main/test-data/parameter-expansion-untokenized2.zsh @@ -33,6 +33,6 @@ local x="^foo^bar" BUFFER=$'$x ls' expected_region_highlight=( - '1 2 unknown-token "fixed in the next commit"' # $x + '1 2 unknown-token' # $x '4 5 default' # ls ) From 8be98dc76c21202a4d45490e117bd07bf5989502 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 22 Feb 2020 14:19:46 +0000 Subject: [PATCH 07/11] 'main': Don't add multiple styles for a single parameter's expansion. See https://github.com/zsh-users/zsh-syntax-highlighting/pull/684#issuecomment-589960462 --- highlighters/main/main-highlighter.zsh | 1 + 1 file changed, 1 insertion(+) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a2818b88c..5155bf056 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -85,6 +85,7 @@ _zsh_highlight_main_add_region_highlight() { return fi param_style=$1 + return fi # The calculation was relative to $buf but region_highlight is relative to $BUFFER. From c6a62513f988080d3ad0128a44bc55d36ba3dd24 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 22 Feb 2020 14:29:55 +0000 Subject: [PATCH 08/11] tests: Support non-arrays in typeset_p(). --- tests/test-highlighting.zsh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index b2a6db58a..f335293ed 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -93,9 +93,13 @@ ZSH_HIGHLIGHT_HIGHLIGHTERS=($1) # In zsh<5.3, 'typeset -p arrayvar' emits two lines, so we use this wrapper instead. typeset_p() { - for 1 ; do - print -r -- "$1=( ${(@q-P)1} )" - done + for 1 ; do + if [[ ${(tP)1} == *array* ]]; then + print -r -- "$1=( ${(@q-P)1} )" + else + print -r -- "$1=${(q-P)1}" + fi + done } # Escape # as ♯ and newline as ↵ they are illegal in the 'description' part of TAP output From 92c3d04f88d9476854dafcbd20ab4b20ac7dd67d Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 22 Feb 2020 14:31:18 +0000 Subject: [PATCH 09/11] noop: Whitespace changes only. ./.editorconfig is already set correctly. --- tests/test-highlighting.zsh | 2 +- zsh-syntax-highlighting.zsh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index f335293ed..91534ef82 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -206,7 +206,7 @@ run_test() { local ret=$pipestatus[1] stderr=$pipestatus[2] if (( ! stderr )); then # stdout will become stderr - echo "Bail out! On ${(qq)1}: output on stderr"; return 1 + echo "Bail out! On ${(qq)1}: output on stderr"; return 1 else return $ret fi diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 5c6b478fc..fd2a7c6ca 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -161,12 +161,12 @@ _zsh_highlight() (( REGION_ACTIVE )) || return integer min max if (( MARK > CURSOR )) ; then - min=$CURSOR max=$MARK + min=$CURSOR max=$MARK else - min=$MARK max=$CURSOR + min=$MARK max=$CURSOR fi if (( REGION_ACTIVE == 1 )); then - [[ $KEYMAP = vicmd ]] && (( max++ )) + [[ $KEYMAP = vicmd ]] && (( max++ )) elif (( REGION_ACTIVE == 2 )); then local needle=$'\n' # CURSOR and MARK are 0 indexed between letters like region_highlight From de67fd31e666080c95446154bb9d10e5411e594e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 22 Feb 2020 14:33:39 +0000 Subject: [PATCH 10/11] test harness: Include $PREBUFFER and $BUFFER in the output. For human readers' benefit. --- tests/test-highlighting.zsh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index 91534ef82..1115712ea 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -118,6 +118,8 @@ run_test_internal() { builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! On ${(qq)1}: cd failed: $?"; return 1 } echo "# ${1:t:r}" + [[ -n $PREBUFFER ]] && printf '# %s\n' "$(typeset_p PREBUFFER)" + [[ -n $BUFFER ]] && printf '# %s\n' "$(typeset_p BUFFER)" # Load the data and prepare checking it. local BUFFER CURSOR MARK PENDING PREBUFFER REGION_ACTIVE WIDGET REPLY skip_test unsorted=0 From fd955235076184d2ad393cfd480a9af64c9f5d88 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 22 Feb 2020 14:37:14 +0000 Subject: [PATCH 11/11] test harness: Tweak quiet-test output - Print the test name and data after the plan line - Split on the plan line rather than on comments + That makes tap-filter more suitable to filter TAP output generated by other TAP producers. + However, the filtered output deletes the plan line and adds a blank line in its stead. This suits our use-case of interactive test runs. --- tests/tap-filter | 4 +++- tests/test-highlighting.zsh | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/tap-filter b/tests/tap-filter index feb22d2b6..f9aa60414 100755 --- a/tests/tap-filter +++ b/tests/tap-filter @@ -41,5 +41,7 @@ use strict; undef $/; # slurp mode print for grep { /^ok.*# TODO/m or /^not ok(?!.*# TODO)/m or /^Bail out!/m } - split /^(?=#)/m, + # Split on plan lines and remove them from the output. (To keep them, + # use the lookahead syntax, «(?=…)», to make the match zero-length.) + split /^\d+\.\.\d+$/m, ; diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index 1115712ea..465abe00d 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -117,10 +117,6 @@ run_test_internal() { local srcdir="$PWD" builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! On ${(qq)1}: cd failed: $?"; return 1 } - echo "# ${1:t:r}" - [[ -n $PREBUFFER ]] && printf '# %s\n' "$(typeset_p PREBUFFER)" - [[ -n $BUFFER ]] && printf '# %s\n' "$(typeset_p BUFFER)" - # Load the data and prepare checking it. local BUFFER CURSOR MARK PENDING PREBUFFER REGION_ACTIVE WIDGET REPLY skip_test unsorted=0 local expected_mismatch @@ -145,7 +141,12 @@ run_test_internal() { expected_region_highlight=("${(@n)expected_region_highlight}") fi + # Print the plan line, and some comments for human readers echo "1..$(( $#expected_region_highlight + 1))" + echo "## ${1:t:r}" + [[ -n $PREBUFFER ]] && printf '# %s\n' "$(typeset_p PREBUFFER)" + [[ -n $BUFFER ]] && printf '# %s\n' "$(typeset_p BUFFER)" + local i for ((i=1; i<=$#expected_region_highlight; i++)); do local -a expected_highlight_zone; expected_highlight_zone=( ${(z)expected_region_highlight[i]} )