Skip to content

Commit f71a17c

Browse files
committed
main: Highlight closing brackets
Closes #226
1 parent d0c23a6 commit f71a17c

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

highlighters/main/main-highlighter.zsh

+15-2
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ _zsh_highlight_main_highlighter_highlight_list()
383383
# "R" for round
384384
# "Q" for square
385385
# "Y" for curly
386+
# "T" for [[ ]]
386387
# "S" for $( )
387388
# "D" for do/done
388389
# "$" for 'end' (matches 'foreach' always; also used with cshjunkiequotes in repeat/while)
@@ -655,7 +656,10 @@ _zsh_highlight_main_highlighter_highlight_list()
655656

656657
# The Great Fork: is this a command word? Is this a non-command word?
657658
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
658-
if [[ $this_word == *':regular:'* ]]; then
659+
if _zsh_highlight_main__stack_pop T || _zsh_highlight_main__stack_pop Q; then
660+
# Missing closing square bracket(s)
661+
style=unknown-token
662+
elif [[ $this_word == *':regular:'* ]]; then
659663
# This highlights empty commands (semicolon follows nothing) as an error.
660664
# Zsh accepts them, though.
661665
style=commandseparator
@@ -698,6 +702,9 @@ _zsh_highlight_main_highlighter_highlight_list()
698702
next_word+=':always:'
699703
fi
700704
;;
705+
($'\x5b\x5b')
706+
braces_stack='T'"$braces_stack"
707+
;;
701708
('do')
702709
braces_stack='D'"$braces_stack"
703710
;;
@@ -748,7 +755,9 @@ _zsh_highlight_main_highlighter_highlight_list()
748755
;;
749756
'suffix alias') style=suffix-alias;;
750757
alias) :;;
751-
builtin) style=builtin;;
758+
builtin) style=builtin
759+
[[ $arg == $'\x5b' ]] && braces_stack='Q'"$braces_stack"
760+
;;
752761
function) style=function;;
753762
command) style=command;;
754763
hashed) style=hashed-command;;
@@ -864,6 +873,10 @@ _zsh_highlight_main_highlighter_highlight_list()
864873
fi
865874
elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
866875
style=history-expansion
876+
elif [[ $arg == $'\x5d\x5d' ]] && _zsh_highlight_main__stack_pop 'T' reserved-word; then
877+
:
878+
elif [[ $arg == $'\x5d' ]] && _zsh_highlight_main__stack_pop 'Q' builtin; then
879+
:
867880
else
868881
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 - in_redirection ))
869882
continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2018 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
BUFFER='[[ -n foo ]]'
32+
33+
expected_region_highlight=(
34+
'1 2 reserved-word' # [[
35+
'4 5 single-hyphen-option' # -n
36+
'7 9 default' # foo
37+
'11 12 reserved-word' # ]]
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2018 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
BUFFER='[ -n foo ]'
32+
33+
expected_region_highlight=(
34+
'1 1 builtin' # [
35+
'3 4 single-hyphen-option' # -n
36+
'6 8 default' # foo
37+
'10 10 builtin' # ]
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2018 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
BUFFER='[[ -n foo; echo ]]'
32+
33+
expected_region_highlight=(
34+
'1 2 reserved-word' # [[
35+
'4 5 single-hyphen-option' # -n
36+
'7 9 default' # foo
37+
'10 10 unknown-token' # ;
38+
'12 15 builtin' # echo
39+
'17 18 default' # ]]
40+
)

0 commit comments

Comments
 (0)