main: Highlight command substitutions

This commit is contained in:
Matthew Martin 2018-03-10 15:03:02 -06:00
parent ce592bd5c7
commit b75169597e
4 changed files with 33 additions and 5 deletions

View File

@ -32,6 +32,7 @@ This highlighter defines the following styles:
* `path_prefix_pathseparator` - path separators in prefixes of existing filenames (`/`); if unset, `path_prefix` is used (default) * `path_prefix_pathseparator` - path separators in prefixes of existing filenames (`/`); if unset, `path_prefix` is used (default)
* `globbing` - globbing expressions (`*.txt`) * `globbing` - globbing expressions (`*.txt`)
* `history-expansion` - history expansion expressions (`!foo` and `^foo^bar`) * `history-expansion` - history expansion expressions (`!foo` and `^foo^bar`)
* `command-substitution` - command substitutions (`$(echo foo)`)
* `single-hyphen-option` - single-hyphen options (`-o`) * `single-hyphen-option` - single-hyphen options (`-o`)
* `double-hyphen-option` - double-hyphen options (`--option`) * `double-hyphen-option` - double-hyphen options (`--option`)
* `back-quoted-argument` - backtick command substitution (`` `foo` ``) * `back-quoted-argument` - backtick command substitution (`` `foo` ``)

View File

@ -40,6 +40,7 @@
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[command-substitution]:=fg=magenta}
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none} : ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
: ${ZSH_HIGHLIGHT_STYLES[double-hyphen-option]:=none} : ${ZSH_HIGHLIGHT_STYLES[double-hyphen-option]:=none}
: ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none} : ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none}
@ -653,7 +654,12 @@ _zsh_highlight_main_highlighter_highlight_list()
style=reserved-word style=reserved-word
braces_stack='R'"$braces_stack" braces_stack='R'"$braces_stack"
elif [[ $arg == $'\x29' ]]; then elif [[ $arg == $'\x29' ]]; then
# end of subshell # end of subshell or command substitution
if _zsh_highlight_main__stack_pop 'S'; then
REPLY=$start_pos
reply=($list_highlights)
return
fi
_zsh_highlight_main__stack_pop 'R' reserved-word _zsh_highlight_main__stack_pop 'R' reserved-word
else else
if _zsh_highlight_main_highlighter_check_path; then if _zsh_highlight_main_highlighter_check_path; then
@ -679,6 +685,11 @@ _zsh_highlight_main_highlighter_highlight_list()
in_array_assignment=false in_array_assignment=false
next_word+=':start:' next_word+=':start:'
else else
if _zsh_highlight_main__stack_pop 'S'; then
REPLY=$start_pos
reply=($list_highlights)
return
fi
_zsh_highlight_main__stack_pop 'R' reserved-word _zsh_highlight_main__stack_pop 'R' reserved-word
fi;; fi;;
$'\x28\x29') # possibly a function definition $'\x28\x29') # possibly a function definition
@ -829,8 +840,8 @@ _zsh_highlight_main_highlighter_check_path()
# This command will at least highlight start_pos to end_pos with the default style # This command will at least highlight start_pos to end_pos with the default style
_zsh_highlight_main_highlighter_highlight_argument() _zsh_highlight_main_highlighter_highlight_argument()
{ {
local base_style=default i path_eligible=1 style local base_style=default i path_eligible=1 start style
local -a highlights reply local -a highlights
local -a match mbegin mend local -a match mbegin mend
local MATCH; integer MBEGIN MEND local MATCH; integer MBEGIN MEND
@ -870,6 +881,13 @@ _zsh_highlight_main_highlighter_highlight_argument()
(( i = REPLY )) (( i = REPLY ))
highlights+=($reply) highlights+=($reply)
continue continue
elif [[ $arg[i+1] == $'\x28' ]]; then
start=$i
(( i += 2 ))
_zsh_highlight_main_highlighter_highlight_list $(( start_pos + i - 1 )) S $arg[i,end_pos]
(( i += REPLY ))
highlights+=($(( start_pos + start - 1)) $(( start_pos + i )) command-substitution $reply)
continue
fi fi
while [[ $arg[i+1] == [\^=~#+] ]]; do while [[ $arg[i+1] == [\^=~#+] ]]; do
(( i += 1 )) (( i += 1 ))
@ -969,7 +987,12 @@ _zsh_highlight_main_highlighter_highlight_double_quote()
(( k += 1 )) # highlight both dollar signs (( k += 1 )) # highlight both dollar signs
(( i += 1 )) # don't consider the second one as introducing another parameter expansion (( i += 1 )) # don't consider the second one as introducing another parameter expansion
elif [[ $arg[i+1] == $'\x28' ]]; then elif [[ $arg[i+1] == $'\x28' ]]; then
# Highlight just the '$'. (( i += 2 ))
saved_reply=($reply)
_zsh_highlight_main_highlighter_highlight_list $(( start_pos + i - 1 )) S $arg[i,end_pos]
(( i += REPLY ))
reply=($saved_reply $j $(( start_pos + i )) command-substitution $reply)
continue
else else
continue continue
fi fi

View File

@ -34,5 +34,6 @@ expected_region_highlight=(
'1 1 builtin' # : '1 1 builtin' # :
'3 8 default' # "$(:)" '3 8 default' # "$(:)"
'3 8 double-quoted-argument' # "$(:)" '3 8 double-quoted-argument' # "$(:)"
'4 4 dollar-double-quoted-argument' # $ '4 7 command-substitution' # $(:)
'6 6 builtin' # :
) )

View File

@ -33,4 +33,7 @@ BUFFER=$': $(<foo)'
expected_region_highlight=( expected_region_highlight=(
'1 1 builtin' # : '1 1 builtin' # :
'3 9 default' # $(<foo) '3 9 default' # $(<foo)
'3 9 command-substitution' # $(<foo)
'5 5 redirection' # <
'6 8 default' # foo
) )