Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@
- Fix `echo >&p` highlighting the `p` as a filename if a file by that name happened to exist
[part of #645]

- Fix `: $((42))` being highlighted as a subshell.
[part of #607]

- Regress highlighting of `: $((ls); (ls))`: is a subshell, but will now be
incorrectly highlighted as an arithmetic expansion.
[#704]

- Fix wrong highlighting of unquoted parameter expansions under zsh 5.2 and older
[e165f18c758e]

Expand Down Expand Up @@ -89,6 +82,8 @@
(such as `;`, `|`, `&&`) before a newline
[#677; had regressed in 0.7.0]

- Highlight arithmetic expansions (e.g., `$(( 42 ))`)
[#607 #649 #704]

# Changes in version 0.7.1

Expand Down
1 change: 1 addition & 0 deletions docs/highlighters/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This highlighter defines the following styles:
* `command-substitution-delimiter-quoted` - a quoted command substitution delimiters (`"$(` and `)"`)
* `process-substitution` - process substitutions (`<(echo foo)`)
* `process-substitution-delimiter` - process substitution delimiters (`<(` and `)`)
* `arithmetic-expansion` - arithmetic expansion `$(( 42 ))`)
* `single-hyphen-option` - single-hyphen options (`-o`)
* `double-hyphen-option` - double-hyphen options (`--option`)
* `back-quoted-argument` - backtick command substitution (`` `foo` ``)
Expand Down
115 changes: 106 additions & 9 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -1350,8 +1350,13 @@ _zsh_highlight_main_highlighter_highlight_argument()
(( i = REPLY ))
highlights+=($reply)
continue
elif [[ $arg[i+1] == $'\x28' && ${arg:$i} != $'\x28\x28'*$'\x29\x29'* ]]; then
# command substitution that doesn't look like an arithmetic expansion
elif [[ $arg[i+1] == $'\x28' ]]; then
if [[ $arg[i+2] == $'\x28' ]] && _zsh_highlight_main_highlighter_highlight_arithmetic $i; then
# Arithmetic expansion
(( i = REPLY ))
highlights+=($reply)
continue
fi
start=$i
(( i += 2 ))
_zsh_highlight_main_highlighter_highlight_list $(( start_pos + i - 1 )) S $has_end $arg[i,-1]
Expand All @@ -1366,10 +1371,6 @@ _zsh_highlight_main_highlighter_highlight_argument()
highlights+=($(( start_pos + i - 1)) $(( start_pos + i )) command-substitution-delimiter-unquoted)
fi
continue
else
# TODO: if it's an arithmetic expansion, skip past it, to prevent
# multiplications from being highlighted as globbing (issue #607,
# test-data/arith1.zsh)
fi
while [[ $arg[i+1] == [=~#+'^'] ]]; do
(( i += 1 ))
Expand Down Expand Up @@ -1497,11 +1498,17 @@ _zsh_highlight_main_highlighter_highlight_double_quote()
# $#, $*, $@, $?, $- - like $$ above
(( k += 1 )) # highlight both dollar signs
(( i += 1 )) # don't consider the second one as introducing another parameter expansion
elif [[ $arg[i+1] == $'\x28' && ${arg:$i} != $'\x28\x28'*$'\x29\x29'* ]]; then
# command substitution that doesn't look like an arithmetic expansion
elif [[ $arg[i+1] == $'\x28' ]]; then
saved_reply=($reply)
if [[ $arg[i+2] == $'\x28' ]] && _zsh_highlight_main_highlighter_highlight_arithmetic $i; then
# Arithmetic expansion
(( i = REPLY ))
reply=($saved_reply $reply)
continue
fi

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,-1]
ret=$?
(( i += REPLY ))
Expand Down Expand Up @@ -1682,6 +1689,96 @@ _zsh_highlight_main_highlighter_highlight_backtick()
REPLY=$i
}

# Highlight special chars inside arithmetic expansions
_zsh_highlight_main_highlighter_highlight_arithmetic()
{
local -a saved_reply
local style
integer i j k paren_depth ret
reply=()

for (( i = $1 + 3 ; i <= end_pos - start_pos ; i += 1 )) ; do
(( j = i + start_pos - 1 ))
(( k = j + 1 ))
case "$arg[$i]" in
[\'\"\\@{}])
style=unknown-token
;;
'(')
(( paren_depth++ ))
continue
;;
')')
if (( paren_depth )); then
(( paren_depth-- ))
continue
fi
[[ $arg[i+1] == ')' ]] && { (( i++ )); break; }
# Special case ) at the end of the buffer to avoid flashing command substitution for a character
(( has_end && (len == k) )) && break
# This is a single paren and there are no open parens, so this isn't an arithmetic expansion
return 1
;;
'`')
saved_reply=($reply)
_zsh_highlight_main_highlighter_highlight_backtick $i
(( i = REPLY ))
reply=($saved_reply $reply)
continue
;;
'$' )
if [[ $arg[i+1] == $'\x28' ]]; then
saved_reply=($reply)
if [[ $arg[i+2] == $'\x28' ]] && _zsh_highlight_main_highlighter_highlight_arithmetic $i; then
# Arithmetic expansion
(( i = REPLY ))
reply=($saved_reply $reply)
continue
fi

(( i += 2 ))
_zsh_highlight_main_highlighter_highlight_list $(( start_pos + i - 1 )) S $has_end $arg[i,end_pos]
ret=$?
(( i += REPLY ))
reply=(
$saved_reply
$j $(( start_pos + i )) command-substitution-quoted
$j $(( j + 2 )) command-substitution-delimiter-quoted
$reply
)
if (( ret == 0 )); then
reply+=($(( start_pos + i - 1 )) $(( start_pos + i )) command-substitution-delimiter)
fi
continue
else
continue
fi
;;
($histchars[1]) # ! - may be a history expansion
if [[ $arg[i+1] != ('='|$'\x28'|$'\x7b'|[[:blank:]]) ]]; then
style=history-expansion
else
continue
fi
;;
*)
continue
;;

esac
reply+=($j $k $style)
done

if [[ $arg[i] != ')' ]]; then
# If unclosed, i points past the end
(( i-- ))
fi
style=arithmetic-expansion
reply=($(( start_pos + $1 - 1)) $(( start_pos + i )) arithmetic-expansion $reply)
REPLY=$i
}


# Called with a single positional argument.
# Perform filename expansion (tilde expansion) on the argument and set $REPLY to the expanded value.
#
Expand Down
20 changes: 10 additions & 10 deletions highlighters/main/test-data/arith-cmdsubst-mess.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ BUFFER=$': $((ls); (ls))'
expected_region_highlight=(
'1 1 builtin' # :
'3 15 default' # $((ls); (ls))
'3 15 command-substitution-unquoted "issue #704"' # $((ls); (ls))
'3 4 command-substitution-delimiter-unquoted "issue #704"' # $(
'5 5 reserved-word "issue #704"' # (
'6 7 command "issue #704"' # ls
'8 8 reserved-word "issue #704"' # )
'9 9 commandseparator "issue #704"' # ;
'11 11 reserved-word "issue #704"' # (
'12 13 command "issue #704"' # ls
'14 14 reserved-word "issue #704"' # )
'15 15 command-substitution-delimiter-unquoted "issue #704"' # )
'3 15 command-substitution-unquoted' # $((ls); (ls))
'3 4 command-substitution-delimiter-unquoted' # $(
'5 5 reserved-word' # (
'6 7 command' # ls
'8 8 reserved-word' # )
'9 9 commandseparator' # ;
'11 11 reserved-word' # (
'12 13 command' # ls
'14 14 reserved-word' # )
'15 15 command-substitution-delimiter-unquoted' # )
)
2 changes: 1 addition & 1 deletion highlighters/main/test-data/arith1.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ BUFFER=$': $(( 6 * 9 ))'
expected_region_highlight=(
'1 1 builtin' # :
'3 14 default' # $(( 6 * 9 ))
'3 14 arithmetic-expansion' # $(( 6 * 9 ))
)
expected_mismatch="currently the actual highlighting has one superfluous group that highlights the asterisk is highlighted as 'globbing'"
1 change: 1 addition & 0 deletions highlighters/main/test-data/arith2.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ expected_region_highlight=(
'1 1 builtin' # :
'3 16 default' # "$(( 6 * 9 ))"
'3 16 double-quoted-argument' # "$(( 6 * 9 ))"
'4 15 arithmetic-expansion' # $(( 6 * 9 ))
)
42 changes: 42 additions & 0 deletions highlighters/main/test-data/arithmetic-command-substitution.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/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
# -------------------------------------------------------------------------------------------------

BUFFER=$': $(( $(echo 2) + 2 ))'

expected_region_highlight=(
'1 1 builtin' # :
'3 22 default' # $(( $(echo 2) + 2 ))
'3 22 arithmetic-expansion' # $(( $(echo 2) + 2 ))
'7 15 command-substitution-quoted' # $(echo 2)
'7 8 command-substitution-delimiter-quoted' # $(
'9 12 builtin' # echo
'14 14 default' # 2
'15 15 command-substitution-delimiter' # )
)
37 changes: 37 additions & 0 deletions highlighters/main/test-data/arithmetic-doubled-parens.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/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
# -------------------------------------------------------------------------------------------------

BUFFER=$': $(( ((42)) ))'

expected_region_highlight=(
'1 1 builtin' # :
'3 15 default' # $(( ((42)) ))
'3 15 arithmetic-expansion' # $(( ((42)) ))
)
39 changes: 39 additions & 0 deletions highlighters/main/test-data/arithmetic-empty.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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
# -------------------------------------------------------------------------------------------------

BUFFER=$': "foo"$(())"bar"'

expected_region_highlight=(
'1 1 builtin' # :
'3 17 default' # "foo"$(())"bar"
'3 7 double-quoted-argument' # "foo"
'8 12 arithmetic-expansion' # $(())
'13 17 double-quoted-argument' # "bar"
)
38 changes: 38 additions & 0 deletions highlighters/main/test-data/arithmetic-hist-expn.zsh
Original file line number Diff line number Diff line change
@@ -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
# -------------------------------------------------------------------------------------------------

BUFFER=$': $(( \!\! ))'

expected_region_highlight=(
'1 1 builtin' # :
'3 11 default' # $(( !! ))
'3 11 arithmetic-expansion' # $(( !! ))
'7 8 history-expansion "issue #713"' # !!
)
Loading