From b44964c5453324ec1aa71344572853147e995629 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 19 Mar 2020 19:45:02 +0000 Subject: [PATCH] 'main': Highlight reserved words following assignments as errors. Fixes #461. --- changelog.md | 3 +++ highlighters/main/main-highlighter.zsh | 11 ++++++++++- .../main/test-data/assignment-before-resword1.zsh | 2 +- .../main/test-data/assignment-before-resword2.zsh | 4 ++-- .../main/test-data/assignment-before-resword3.zsh | 3 +-- .../main/test-data/assignment-before-resword4.zsh | 2 +- 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index c75edb9..c89e362 100644 --- a/changelog.md +++ b/changelog.md @@ -60,6 +60,9 @@ - Highlight `: =nosuchcommand' as an error (when the `EQUALS` option hasn't been unset). [#430] +- Highlight reserved word after assignments as errors (e.g., `foo=bar (ls;)`) + [#461] + # Changes in version 0.7.1 - Remove out-of-date information from the 0.7.0 changelog. diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index c44b1c3..8f0f24b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -431,7 +431,7 @@ _zsh_highlight_main_highlighter_highlight_list() # Usually 'alias' but set to 'unknown-token' if any word expanded from # the alias would be highlighted as unknown-token # 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 ')' # 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. @@ -556,6 +556,7 @@ _zsh_highlight_main_highlighter_highlight_list() # $style how to highlight $arg # $in_array_assignment boolean flag for "between '(' and ')' of array assignment" # $highlight_glob boolean flag for "'noglob' is in effect" + # $saw_assignment boolean flag for "was preceded by an assignment" # style=unknown-token if [[ $this_word == *':start:'* ]]; then @@ -826,6 +827,7 @@ _zsh_highlight_main_highlighter_highlight_list() else next_word=':start:' highlight_glob=true + saw_assignment=false if [[ $arg != '|' && $arg != '|&' ]]; then next_word+=':start_of_pipeline:' fi @@ -835,6 +837,7 @@ _zsh_highlight_main_highlighter_highlight_list() # try-always construct style=reserved-word # de facto a reserved word, although not de jure highlight_glob=true + saw_assignment=false next_word=':start::start_of_pipeline:' # only left brace is allowed, apparently elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then @@ -930,6 +933,9 @@ _zsh_highlight_main_highlighter_highlight_list() fi ;; esac + if $saw_assignment && [[ $style != unknown-token ]]; then + style=unknown-token + fi ;; ('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 _zsh_highlight_main_add_region_highlight $start_pos $end_pos assign local i=$(( arg[(i)=] + 1 )) + saw_assignment=true if [[ $arg[i] == '(' ]]; then in_array_assignment=true else @@ -972,6 +979,7 @@ _zsh_highlight_main_highlighter_highlight_list() [[ $arg[0,1] == $histchars[2,2] ]]; then style=history-expansion elif (( ! in_param )) && + ! $saw_assignment && [[ $arg[1,2] == '((' ]]; then # Arithmetic evaluation. # @@ -992,6 +1000,7 @@ _zsh_highlight_main_highlighter_highlight_list() # anonymous function style=reserved-word elif (( ! in_param )) && + ! $saw_assignment && [[ $arg == $'\x28' ]]; then # subshell style=reserved-word diff --git a/highlighters/main/test-data/assignment-before-resword1.zsh b/highlighters/main/test-data/assignment-before-resword1.zsh index eb4a0c6..b271d4a 100644 --- a/highlighters/main/test-data/assignment-before-resword1.zsh +++ b/highlighters/main/test-data/assignment-before-resword1.zsh @@ -33,7 +33,7 @@ BUFFER=$'foo=bar { :; }' expected_region_highlight=( '1 7 assign' # foo=bar '5 7 default' # bar - '9 9 unknown-token "issue #461"' # { + '9 9 unknown-token' # { '11 11 builtin' # : '12 12 commandseparator' # ; '14 14 reserved-word' # } diff --git a/highlighters/main/test-data/assignment-before-resword2.zsh b/highlighters/main/test-data/assignment-before-resword2.zsh index 5324a66..247e41e 100644 --- a/highlighters/main/test-data/assignment-before-resword2.zsh +++ b/highlighters/main/test-data/assignment-before-resword2.zsh @@ -33,8 +33,8 @@ BUFFER=$'foo=bar ( :; )' expected_region_highlight=( '1 7 assign' # foo=bar '5 7 default' # bar - '9 9 unknown-token "issue #461"' # ( + '9 9 unknown-token' # ( '11 11 builtin' # : '12 12 commandseparator' # ; - '14 14 reserved-word' # ) + '14 14 unknown-token' # ) ) diff --git a/highlighters/main/test-data/assignment-before-resword3.zsh b/highlighters/main/test-data/assignment-before-resword3.zsh index 09a8f04..5204189 100644 --- a/highlighters/main/test-data/assignment-before-resword3.zsh +++ b/highlighters/main/test-data/assignment-before-resword3.zsh @@ -33,6 +33,5 @@ BUFFER=$'foo=bar (( foo ))' expected_region_highlight=( '1 7 assign' # foo=bar '5 7 default' # bar - '9 10 unknown-token "issue #461"' # (( - '16 17 reserved-word' # )) + '9 17 unknown-token' # (( foo )) ) diff --git a/highlighters/main/test-data/assignment-before-resword4.zsh b/highlighters/main/test-data/assignment-before-resword4.zsh index c0083cb..cc3d523 100644 --- a/highlighters/main/test-data/assignment-before-resword4.zsh +++ b/highlighters/main/test-data/assignment-before-resword4.zsh @@ -33,7 +33,7 @@ BUFFER=$'foo=bar [[ -n foo ]]' expected_region_highlight=( '1 7 assign' # foo=bar '5 7 default' # bar - '9 10 unknown-token "issue #461"' # [[ + '9 10 unknown-token' # [[ '12 13 single-hyphen-option' # -n '15 17 default' # foo '19 20 reserved-word' # ]]