Skip to content

Commit b44964c

Browse files
committed
'main': Highlight reserved words following assignments as errors.
Fixes #461.
1 parent f54d829 commit b44964c

6 files changed

+18
-7
lines changed

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
- Highlight `: =nosuchcommand' as an error (when the `EQUALS` option hasn't been unset).
6161
[#430]
6262

63+
- Highlight reserved word after assignments as errors (e.g., `foo=bar (ls;)`)
64+
[#461]
65+
6366
# Changes in version 0.7.1
6467

6568
- Remove out-of-date information from the 0.7.0 changelog.

highlighters/main/main-highlighter.zsh

+10-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ _zsh_highlight_main_highlighter_highlight_list()
431431
# Usually 'alias' but set to 'unknown-token' if any word expanded from
432432
# the alias would be highlighted as unknown-token
433433
# param_style is analogous for parameter expansions
434-
local alias_style param_style last_arg arg buf=$4 highlight_glob=true style
434+
local alias_style param_style last_arg arg buf=$4 highlight_glob=true saw_assignment=false style
435435
local in_array_assignment=false # true between 'a=(' and the matching ')'
436436
# in_alias is equal to the number of shifts needed until arg=args[1] pops an
437437
# arg from BUFFER and not added by an alias.
@@ -556,6 +556,7 @@ _zsh_highlight_main_highlighter_highlight_list()
556556
# $style how to highlight $arg
557557
# $in_array_assignment boolean flag for "between '(' and ')' of array assignment"
558558
# $highlight_glob boolean flag for "'noglob' is in effect"
559+
# $saw_assignment boolean flag for "was preceded by an assignment"
559560
#
560561
style=unknown-token
561562
if [[ $this_word == *':start:'* ]]; then
@@ -826,6 +827,7 @@ _zsh_highlight_main_highlighter_highlight_list()
826827
else
827828
next_word=':start:'
828829
highlight_glob=true
830+
saw_assignment=false
829831
if [[ $arg != '|' && $arg != '|&' ]]; then
830832
next_word+=':start_of_pipeline:'
831833
fi
@@ -835,6 +837,7 @@ _zsh_highlight_main_highlighter_highlight_list()
835837
# try-always construct
836838
style=reserved-word # de facto a reserved word, although not de jure
837839
highlight_glob=true
840+
saw_assignment=false
838841
next_word=':start::start_of_pipeline:' # only left brace is allowed, apparently
839842
elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word
840843
if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then
@@ -930,6 +933,9 @@ _zsh_highlight_main_highlighter_highlight_list()
930933
fi
931934
;;
932935
esac
936+
if $saw_assignment && [[ $style != unknown-token ]]; then
937+
style=unknown-token
938+
fi
933939
;;
934940
('suffix alias')
935941
style=suffix-alias
@@ -947,6 +953,7 @@ _zsh_highlight_main_highlighter_highlight_list()
947953
(none) if (( ! in_param )) && _zsh_highlight_main_highlighter_check_assign; then
948954
_zsh_highlight_main_add_region_highlight $start_pos $end_pos assign
949955
local i=$(( arg[(i)=] + 1 ))
956+
saw_assignment=true
950957
if [[ $arg[i] == '(' ]]; then
951958
in_array_assignment=true
952959
else
@@ -972,6 +979,7 @@ _zsh_highlight_main_highlighter_highlight_list()
972979
[[ $arg[0,1] == $histchars[2,2] ]]; then
973980
style=history-expansion
974981
elif (( ! in_param )) &&
982+
! $saw_assignment &&
975983
[[ $arg[1,2] == '((' ]]; then
976984
# Arithmetic evaluation.
977985
#
@@ -992,6 +1000,7 @@ _zsh_highlight_main_highlighter_highlight_list()
9921000
# anonymous function
9931001
style=reserved-word
9941002
elif (( ! in_param )) &&
1003+
! $saw_assignment &&
9951004
[[ $arg == $'\x28' ]]; then
9961005
# subshell
9971006
style=reserved-word

highlighters/main/test-data/assignment-before-resword1.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BUFFER=$'foo=bar { :; }'
3333
expected_region_highlight=(
3434
'1 7 assign' # foo=bar
3535
'5 7 default' # bar
36-
'9 9 unknown-token "issue #461"' # {
36+
'9 9 unknown-token' # {
3737
'11 11 builtin' # :
3838
'12 12 commandseparator' # ;
3939
'14 14 reserved-word' # }

highlighters/main/test-data/assignment-before-resword2.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ BUFFER=$'foo=bar ( :; )'
3333
expected_region_highlight=(
3434
'1 7 assign' # foo=bar
3535
'5 7 default' # bar
36-
'9 9 unknown-token "issue #461"' # (
36+
'9 9 unknown-token' # (
3737
'11 11 builtin' # :
3838
'12 12 commandseparator' # ;
39-
'14 14 reserved-word' # )
39+
'14 14 unknown-token' # )
4040
)

highlighters/main/test-data/assignment-before-resword3.zsh

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ BUFFER=$'foo=bar (( foo ))'
3333
expected_region_highlight=(
3434
'1 7 assign' # foo=bar
3535
'5 7 default' # bar
36-
'9 10 unknown-token "issue #461"' # ((
37-
'16 17 reserved-word' # ))
36+
'9 17 unknown-token' # (( foo ))
3837
)

highlighters/main/test-data/assignment-before-resword4.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BUFFER=$'foo=bar [[ -n foo ]]'
3333
expected_region_highlight=(
3434
'1 7 assign' # foo=bar
3535
'5 7 default' # bar
36-
'9 10 unknown-token "issue #461"' # [[
36+
'9 10 unknown-token' # [[
3737
'12 13 single-hyphen-option' # -n
3838
'15 17 default' # foo
3939
'19 20 reserved-word' # ]]

0 commit comments

Comments
 (0)