From 3c5302ccf327fa7e5481ba80f16508f63cc7b54b Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 26 Dec 2019 16:15:24 +0000 Subject: [PATCH] 'main': Highlight pipes inside array assignments as errors --- highlighters/main/main-highlighter.zsh | 21 +++++++++- highlighters/main/test-data/array-cmdsep1.zsh | 39 +++++++++++++++++++ highlighters/main/test-data/array-cmdsep2.zsh | 39 +++++++++++++++++++ highlighters/main/test-data/array-cmdsep3.zsh | 39 +++++++++++++++++++ .../test-data/multiline-array-assignment1.zsh | 2 +- 5 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 highlighters/main/test-data/array-cmdsep1.zsh create mode 100644 highlighters/main/test-data/array-cmdsep2.zsh create mode 100644 highlighters/main/test-data/array-cmdsep3.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 484918168..61365041e 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -685,18 +685,35 @@ _zsh_highlight_main_highlighter_highlight_list() # The Great Fork: is this a command word? Is this a non-command word? if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then + # First, determine the style of the command separator itself. if _zsh_highlight_main__stack_pop T || _zsh_highlight_main__stack_pop Q; then # Missing closing square bracket(s) style=unknown-token - elif [[ $this_word == *':regular:'* ]]; then + elif $in_array_assignment && [[ $arg == ';' ]] && [[ $this_word == *':regular:'* ]]; then + # Okay, this is hairy. + # + # Literal newlines inside array assignment are just fine. + # + # Literal semicolons inside array assignments are accepted by zsh (don't ask), + # but we want to highlight them as errors anyway, because _nobody_ expects + # them to behave as they do. + # + # However, BOTH cases are reported as ";" by ${(z)}, so we have to + # actually check the buffer: + case "${buf[start_pos+1,end_pos]}" in + (';') style=unknown-token;; + ($'\n') style=default;; + esac + elif [[ $this_word == *':regular:'* ]] && ! $in_array_assignment; then # This highlights empty commands (semicolon follows nothing) as an error. # Zsh accepts them, though. style=commandseparator else style=unknown-token fi + # Second, determine the style of next_word. if [[ $arg == ';' ]] && $in_array_assignment; then - # literal newline inside an array assignment + # Literal semicolon or literal newline inside an array assignment next_word=':regular:' else next_word=':start:' diff --git a/highlighters/main/test-data/array-cmdsep1.zsh b/highlighters/main/test-data/array-cmdsep1.zsh new file mode 100644 index 000000000..3ffe43d45 --- /dev/null +++ b/highlighters/main/test-data/array-cmdsep1.zsh @@ -0,0 +1,39 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2019 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=$'a=( foo | bar )' + +expected_region_highlight=( + '1 3 assign' # a=( + '5 7 default' # foo + '9 9 unknown-token' # | + '11 13 unknown-token' # bar + '15 15 unknown-token' # ) +) diff --git a/highlighters/main/test-data/array-cmdsep2.zsh b/highlighters/main/test-data/array-cmdsep2.zsh new file mode 100644 index 000000000..6574451c6 --- /dev/null +++ b/highlighters/main/test-data/array-cmdsep2.zsh @@ -0,0 +1,39 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2019 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=$'a=( foo ; bar )' + +expected_region_highlight=( + '1 3 assign' # a=( + '5 7 default' # foo + '9 9 unknown-token' # ; + '11 13 default' # bar + '15 15 assign' # ) +) diff --git a/highlighters/main/test-data/array-cmdsep3.zsh b/highlighters/main/test-data/array-cmdsep3.zsh new file mode 100644 index 000000000..76e19dcbd --- /dev/null +++ b/highlighters/main/test-data/array-cmdsep3.zsh @@ -0,0 +1,39 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2019 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=$'a=( foo \n bar )' + +expected_region_highlight=( + '1 3 assign' # a=( + '5 7 default' # foo + '9 9 default' # \n + '11 13 default' # bar + '15 15 assign' # ) +) diff --git a/highlighters/main/test-data/multiline-array-assignment1.zsh b/highlighters/main/test-data/multiline-array-assignment1.zsh index 3734c7a7b..3bb847fc5 100644 --- a/highlighters/main/test-data/multiline-array-assignment1.zsh +++ b/highlighters/main/test-data/multiline-array-assignment1.zsh @@ -32,7 +32,7 @@ BUFFER=$'foo=(\nbar) env' expected_region_highlight=( '1 5 assign' # foo=( - '6 6 commandseparator' # \n + '6 6 default' # \n '7 9 default' # bar '10 10 assign' # ) '12 14 command' # env