'main': Highlight reserved words following assignments as errors.

Fixes #461.
This commit is contained in:
Daniel Shahaf 2020-03-19 19:45:02 +00:00
parent f54d829f03
commit b44964c545
6 changed files with 18 additions and 7 deletions

View File

@ -60,6 +60,9 @@
- Highlight `: =nosuchcommand' as an error (when the `EQUALS` option hasn't been unset). - Highlight `: =nosuchcommand' as an error (when the `EQUALS` option hasn't been unset).
[#430] [#430]
- Highlight reserved word after assignments as errors (e.g., `foo=bar (ls;)`)
[#461]
# Changes in version 0.7.1 # Changes in version 0.7.1
- Remove out-of-date information from the 0.7.0 changelog. - Remove out-of-date information from the 0.7.0 changelog.

View File

@ -431,7 +431,7 @@ _zsh_highlight_main_highlighter_highlight_list()
# Usually 'alias' but set to 'unknown-token' if any word expanded from # Usually 'alias' but set to 'unknown-token' if any word expanded from
# the alias would be highlighted as unknown-token # the alias would be highlighted as unknown-token
# param_style is analogous for parameter expansions # param_style is analogous for parameter expansions
local alias_style param_style last_arg arg buf=$4 highlight_glob=true style local alias_style param_style last_arg arg buf=$4 highlight_glob=true saw_assignment=false style
local in_array_assignment=false # true between 'a=(' and the matching ')' local in_array_assignment=false # true between 'a=(' and the matching ')'
# in_alias is equal to the number of shifts needed until arg=args[1] pops an # in_alias is equal to the number of shifts needed until arg=args[1] pops an
# arg from BUFFER and not added by an alias. # arg from BUFFER and not added by an alias.
@ -556,6 +556,7 @@ _zsh_highlight_main_highlighter_highlight_list()
# $style how to highlight $arg # $style how to highlight $arg
# $in_array_assignment boolean flag for "between '(' and ')' of array assignment" # $in_array_assignment boolean flag for "between '(' and ')' of array assignment"
# $highlight_glob boolean flag for "'noglob' is in effect" # $highlight_glob boolean flag for "'noglob' is in effect"
# $saw_assignment boolean flag for "was preceded by an assignment"
# #
style=unknown-token style=unknown-token
if [[ $this_word == *':start:'* ]]; then if [[ $this_word == *':start:'* ]]; then
@ -826,6 +827,7 @@ _zsh_highlight_main_highlighter_highlight_list()
else else
next_word=':start:' next_word=':start:'
highlight_glob=true highlight_glob=true
saw_assignment=false
if [[ $arg != '|' && $arg != '|&' ]]; then if [[ $arg != '|' && $arg != '|&' ]]; then
next_word+=':start_of_pipeline:' next_word+=':start_of_pipeline:'
fi fi
@ -835,6 +837,7 @@ _zsh_highlight_main_highlighter_highlight_list()
# try-always construct # try-always construct
style=reserved-word # de facto a reserved word, although not de jure style=reserved-word # de facto a reserved word, although not de jure
highlight_glob=true highlight_glob=true
saw_assignment=false
next_word=':start::start_of_pipeline:' # only left brace is allowed, apparently next_word=':start::start_of_pipeline:' # only left brace is allowed, apparently
elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word
if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then
@ -930,6 +933,9 @@ _zsh_highlight_main_highlighter_highlight_list()
fi fi
;; ;;
esac esac
if $saw_assignment && [[ $style != unknown-token ]]; then
style=unknown-token
fi
;; ;;
('suffix alias') ('suffix alias')
style=suffix-alias style=suffix-alias
@ -947,6 +953,7 @@ _zsh_highlight_main_highlighter_highlight_list()
(none) if (( ! in_param )) && _zsh_highlight_main_highlighter_check_assign; then (none) if (( ! in_param )) && _zsh_highlight_main_highlighter_check_assign; then
_zsh_highlight_main_add_region_highlight $start_pos $end_pos assign _zsh_highlight_main_add_region_highlight $start_pos $end_pos assign
local i=$(( arg[(i)=] + 1 )) local i=$(( arg[(i)=] + 1 ))
saw_assignment=true
if [[ $arg[i] == '(' ]]; then if [[ $arg[i] == '(' ]]; then
in_array_assignment=true in_array_assignment=true
else else
@ -972,6 +979,7 @@ _zsh_highlight_main_highlighter_highlight_list()
[[ $arg[0,1] == $histchars[2,2] ]]; then [[ $arg[0,1] == $histchars[2,2] ]]; then
style=history-expansion style=history-expansion
elif (( ! in_param )) && elif (( ! in_param )) &&
! $saw_assignment &&
[[ $arg[1,2] == '((' ]]; then [[ $arg[1,2] == '((' ]]; then
# Arithmetic evaluation. # Arithmetic evaluation.
# #
@ -992,6 +1000,7 @@ _zsh_highlight_main_highlighter_highlight_list()
# anonymous function # anonymous function
style=reserved-word style=reserved-word
elif (( ! in_param )) && elif (( ! in_param )) &&
! $saw_assignment &&
[[ $arg == $'\x28' ]]; then [[ $arg == $'\x28' ]]; then
# subshell # subshell
style=reserved-word style=reserved-word

View File

@ -33,7 +33,7 @@ BUFFER=$'foo=bar { :; }'
expected_region_highlight=( expected_region_highlight=(
'1 7 assign' # foo=bar '1 7 assign' # foo=bar
'5 7 default' # bar '5 7 default' # bar
'9 9 unknown-token "issue #461"' # { '9 9 unknown-token' # {
'11 11 builtin' # : '11 11 builtin' # :
'12 12 commandseparator' # ; '12 12 commandseparator' # ;
'14 14 reserved-word' # } '14 14 reserved-word' # }

View File

@ -33,8 +33,8 @@ BUFFER=$'foo=bar ( :; )'
expected_region_highlight=( expected_region_highlight=(
'1 7 assign' # foo=bar '1 7 assign' # foo=bar
'5 7 default' # bar '5 7 default' # bar
'9 9 unknown-token "issue #461"' # ( '9 9 unknown-token' # (
'11 11 builtin' # : '11 11 builtin' # :
'12 12 commandseparator' # ; '12 12 commandseparator' # ;
'14 14 reserved-word' # ) '14 14 unknown-token' # )
) )

View File

@ -33,6 +33,5 @@ BUFFER=$'foo=bar (( foo ))'
expected_region_highlight=( expected_region_highlight=(
'1 7 assign' # foo=bar '1 7 assign' # foo=bar
'5 7 default' # bar '5 7 default' # bar
'9 10 unknown-token "issue #461"' # (( '9 17 unknown-token' # (( foo ))
'16 17 reserved-word' # ))
) )

View File

@ -33,7 +33,7 @@ BUFFER=$'foo=bar [[ -n foo ]]'
expected_region_highlight=( expected_region_highlight=(
'1 7 assign' # foo=bar '1 7 assign' # foo=bar
'5 7 default' # bar '5 7 default' # bar
'9 10 unknown-token "issue #461"' # [[ '9 10 unknown-token' # [[
'12 13 single-hyphen-option' # -n '12 13 single-hyphen-option' # -n
'15 17 default' # foo '15 17 default' # foo
'19 20 reserved-word' # ]] '19 20 reserved-word' # ]]