From 2b1eadbc60afbcdac9c611cad77a1feedd66552e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 17:39:12 +0000 Subject: [PATCH 1/4] WIP: 'main': Pass parameters through the multi-word machinery, as we already do for aliases. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #674. WIP: One test fails: # commmand-parameter 1..8 ok 1 - [1,2] «$x» ok 2 - [4,13] «"argument"» ok 3 - [4,13] «"argument"» ok 4 - [14,14] «;» not ok 5 - [16,17] «$y» - expected (16 17 "precommand"), observed (16 17 "command"). ok 6 - [18,18] «;» ok 7 - [20,21] «$z» ok 8 - cardinality check --- highlighters/main/main-highlighter.zsh | 30 +++++++++++++++---- .../param-precommand-option-argument1.zsh | 2 +- .../param-precommand-option-argument3.zsh | 4 +-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 038215d52..bb3827165 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 )) @@ -394,11 +398,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 @@ -477,6 +483,13 @@ _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} + fi + fi # Initialize this_word and next_word. if (( in_redirection == 0 )); then @@ -501,7 +514,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 @@ -614,6 +627,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 @@ -626,14 +640,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 @@ -876,6 +892,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 @@ -934,6 +951,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 fb2ed719a..a8d20489a 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 4bcfa8af9..99610bbe8 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 c2b40ed06e53c7d2b91ea591bca95a9aa961468e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 19:02:39 +0000 Subject: [PATCH 2/4] 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: # commmand-parameter 1..8 ok 1 - [1,2] «$x» ok 2 - [4,13] «"argument"» ok 3 - [4,13] «"argument"» ok 4 - [14,14] «;» not ok 5 - [16,17] «$y» - expected (16 17 "precommand"), observed (16 17 "command"). ok 6 - [18,18] «;» ok 7 - [20,21] «$z» ok 8 - cardinality check # 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 --- ...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 5eccd8a50eb55817276f909ac0e79b18db30b02b Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 19:26:11 +0000 Subject: [PATCH 3/4] WIP: Fix command-parameter --- 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 bb3827165..bea4d4460 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -488,6 +488,7 @@ _zsh_highlight_main_highlighter_highlight_list() 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 From 81f26d0d95b5987ff4bbd7f8f294c81173a21644 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 16 Jan 2020 19:31:55 +0000 Subject: [PATCH 4/4] 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 bea4d4460..95d35d1c2 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. @@ -951,8 +956,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)