'main': Support the arithmetic for loop.

This commit is contained in:
Daniel Shahaf 2020-03-19 20:44:32 +00:00
parent 1000da306a
commit 1bd1dec74a
3 changed files with 100 additions and 0 deletions

View File

@ -452,6 +452,7 @@ _zsh_highlight_main_highlighter_highlight_list()
# "D" for do/done
# "$" for 'end' (matches 'foreach' always; also used with cshjunkiequotes in repeat/while)
# "?" for 'if'/'fi'; also checked by 'elif'/'else'
# "4" for the arithmetic 'for'
# ":" for 'then'
local braces_stack=$2
@ -472,6 +473,8 @@ _zsh_highlight_main_highlighter_highlight_list()
# - :regular: "Not a command word", and command delimiters are permitted.
# Mainly used to detect premature termination of commands.
# - :always: The word 'always' in the «{ foo } always { bar }» syntax.
# - :arithmetic_for:
# The double opening parenthesis of an arithmetic 'for' loop.
#
# When the kind of a word is not yet known, $this_word / $next_word may contain
# multiple states. For example, after "sudo -i", the next word may be either
@ -812,6 +815,8 @@ _zsh_highlight_main_highlighter_highlight_list()
style=commandseparator
elif [[ $this_word == *':start:'* ]] && [[ $arg == $'\n' ]]; then
style=commandseparator
elif [[ $braces_stack == '4'* ]]; then
style=commandseparator
else
# This highlights empty commands (semicolon follows nothing) as an error.
# Zsh accepts them, though.
@ -840,6 +845,8 @@ _zsh_highlight_main_highlighter_highlight_list()
highlight_glob=true
saw_assignment=false
next_word=':start::start_of_pipeline:' # only left brace is allowed, apparently
elif [[ $arg == $'\x29\x29' ]] && _zsh_highlight_main__stack_pop '4' reserved-word; then
next_word+=':start:'
elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word
if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then
style=precommand
@ -908,6 +915,9 @@ _zsh_highlight_main_highlighter_highlight_list()
('fi')
_zsh_highlight_main__stack_pop '?'
;;
('for')
next_word+=':arithmetic_for:'
;;
('foreach')
braces_stack='$'"$braces_stack"
;;
@ -1033,6 +1043,9 @@ _zsh_highlight_main_highlighter_highlight_list()
fi
elif _zsh_highlight_main__type "$arg"; [[ $REPLY == 'global alias' ]]; then # $arg is a global alias that isn't in command position
style=global-alias
elif [[ $this_word == *':arithmetic_for:'* && $arg == $'\x28\x28' ]]; then # arithmetic for loop
braces_stack='4'"$braces_stack"
style=reserved-word
else # $arg is a non-command word
case $arg in
($'\x29')

View File

@ -0,0 +1,44 @@
#!/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=$'for (( i = 0; i < 3; ++i )) do pwd; done'
expected_region_highlight=(
'1 3 reserved-word' # for
'5 6 reserved-word' # ((
'8 13 default' # i = 0;
'15 20 default' # i < 3;
'22 25 default' # ++i
'26 27 reserved-word' # ))
'29 30 reserved-word' # do
'32 34 builtin' # pwd
'35 35 commandseparator' # ;
'37 40 reserved-word' # done
)

View File

@ -0,0 +1,43 @@
#!/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=$'for (( ; ; )) do pwd; done'
expected_region_highlight=(
'1 3 reserved-word' # for
'5 6 reserved-word' # ((
'8 8 commandseparator' # ;
'10 10 commandseparator' # ;
'12 13 reserved-word' # ))
'15 16 reserved-word' # do
'18 20 builtin' # pwd
'21 21 commandseparator' # ;
'23 26 reserved-word' # done
)