From 48a20d067fb156278dc3d3f203043b26005e842c Mon Sep 17 00:00:00 2001 From: Matthew Martin Date: Sat, 13 Oct 2018 10:40:13 -0500 Subject: [PATCH] main: Break double-quoted-argument on command substitutions --- highlighters/main/main-highlighter.zsh | 14 ++++-- highlighters/main/test-data/dollar-paren.zsh | 12 +++++- .../quoted-command-substitution-empty.zsh | 43 +++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 highlighters/main/test-data/quoted-command-substitution-empty.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 64622a6..76dfad0 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -1120,8 +1120,8 @@ _zsh_highlight_main_highlighter_highlight_single_quote() # Highlight special chars inside double-quoted strings _zsh_highlight_main_highlighter_highlight_double_quote() { - local -a match mbegin mend saved_reply - local MATCH; integer MBEGIN MEND + local -a breaks match mbegin mend saved_reply + local MATCH; integer last_break=$(( start_pos + $1 - 1 )) MBEGIN MEND local i j k ret style reply=() @@ -1153,11 +1153,13 @@ _zsh_highlight_main_highlighter_highlight_double_quote() (( k += 1 )) # highlight both dollar signs (( i += 1 )) # don't consider the second one as introducing another parameter expansion elif [[ $arg[i+1] == $'\x28' ]]; then + breaks+=( $last_break $(( start_pos + i - 1 )) ) (( i += 2 )) saved_reply=($reply) _zsh_highlight_main_highlighter_highlight_list $(( start_pos + i - 1 )) S $has_end $arg[i,end_pos] ret=$? (( i += REPLY )) + last_break=$(( start_pos + i )) reply=( $saved_reply $j $(( start_pos + i )) command-substitution-quoted @@ -1200,7 +1202,13 @@ _zsh_highlight_main_highlighter_highlight_double_quote() (( i-- )) style=double-quoted-argument-unclosed fi - reply=($(( start_pos + $1 - 1)) $(( start_pos + i )) $style $reply) + (( last_break != start_pos + i )) && breaks+=( $last_break $(( start_pos + i )) ) + saved_reply=($reply) + reply=() + for 1 2 in $breaks; do + reply+=($1 $2 $style) + done + reply+=($saved_reply) REPLY=$i } diff --git a/highlighters/main/test-data/dollar-paren.zsh b/highlighters/main/test-data/dollar-paren.zsh index 51abddb..2bbcef1 100644 --- a/highlighters/main/test-data/dollar-paren.zsh +++ b/highlighters/main/test-data/dollar-paren.zsh @@ -28,14 +28,22 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- -BUFFER=': "$(:)"' +BUFFER=': "$(:)" "foo$(:)bar' expected_region_highlight=( '1 1 builtin' # : '3 8 default' # "$(:)" - '3 8 double-quoted-argument' # "$(:)" + '3 3 double-quoted-argument' # "$(:)" + '8 8 double-quoted-argument' # "$(:)" '4 7 command-substitution-quoted' # $(:) '4 5 command-substitution-delimiter-quoted' # $( '6 6 builtin' # : '7 7 command-substitution-delimiter-quoted' # ) + '10 20 default' # "foo$(:)bar + '10 13 double-quoted-argument-unclosed' # "foo + '18 20 double-quoted-argument-unclosed' # bar + '14 17 command-substitution-quoted' # $(:) + '14 15 command-substitution-delimiter-quoted' # $( + '16 16 builtin' # : + '17 17 command-substitution-delimiter-quoted' # ) ) diff --git a/highlighters/main/test-data/quoted-command-substitution-empty.zsh b/highlighters/main/test-data/quoted-command-substitution-empty.zsh new file mode 100644 index 0000000..099f3b0 --- /dev/null +++ b/highlighters/main/test-data/quoted-command-substitution-empty.zsh @@ -0,0 +1,43 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2018 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='echo "foo$(' + +expected_region_highlight=( + '1 4 builtin' # echo + '6 11 default' # "foo$( + '6 9 double-quoted-argument-unclosed' # "foo + '10 11 command-substitution-quoted' # $( + '10 11 command-substitution-delimiter-quoted' # $( +) + +if [[ ${(z):-'$('} == '$( ' ]]; then # ignore zsh 5.0.8 bug + expected_region_highlight[2]='6 12 default' # "foo$( +fi