Skip to content

Commit

Permalink
'main': Highlight pipes inside array assignments as errors
Browse files Browse the repository at this point in the history
  • Loading branch information
danielshahaf committed Dec 26, 2019
1 parent 6647e88 commit 3c5302c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
21 changes: 19 additions & 2 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand Down
39 changes: 39 additions & 0 deletions highlighters/main/test-data/array-cmdsep1.zsh
Original file line number Diff line number Diff line change
@@ -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' # )
)
39 changes: 39 additions & 0 deletions highlighters/main/test-data/array-cmdsep2.zsh
Original file line number Diff line number Diff line change
@@ -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' # )
)
39 changes: 39 additions & 0 deletions highlighters/main/test-data/array-cmdsep3.zsh
Original file line number Diff line number Diff line change
@@ -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' # )
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c5302c

Please sign in to comment.