From b75169597e1ad4ca1371dd7ea055c50304ab99c2 Mon Sep 17 00:00:00 2001 From: Matthew Martin Date: Sat, 10 Mar 2018 15:03:02 -0600 Subject: [PATCH] main: Highlight command substitutions --- docs/highlighters/main.md | 1 + highlighters/main/main-highlighter.zsh | 31 ++++++++++++++++--- highlighters/main/test-data/dollar-paren.zsh | 3 +- .../test-data/redirection-in-cmdsubst.zsh | 3 ++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/docs/highlighters/main.md b/docs/highlighters/main.md index c14b32f..71c8ec3 100644 --- a/docs/highlighters/main.md +++ b/docs/highlighters/main.md @@ -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) * `globbing` - globbing expressions (`*.txt`) * `history-expansion` - history expansion expressions (`!foo` and `^foo^bar`) +* `command-substitution` - command substitutions (`$(echo foo)`) * `single-hyphen-option` - single-hyphen options (`-o`) * `double-hyphen-option` - double-hyphen options (`--option`) * `back-quoted-argument` - backtick command substitution (`` `foo` ``) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 6d4062f..c80a9e6 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -40,6 +40,7 @@ : ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=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[double-hyphen-option]:=none} : ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none} @@ -653,7 +654,12 @@ _zsh_highlight_main_highlighter_highlight_list() style=reserved-word braces_stack='R'"$braces_stack" 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 else if _zsh_highlight_main_highlighter_check_path; then @@ -679,6 +685,11 @@ _zsh_highlight_main_highlighter_highlight_list() in_array_assignment=false next_word+=':start:' 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 fi;; $'\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 _zsh_highlight_main_highlighter_highlight_argument() { - local base_style=default i path_eligible=1 style - local -a highlights reply + local base_style=default i path_eligible=1 start style + local -a highlights local -a match mbegin mend local MATCH; integer MBEGIN MEND @@ -870,6 +881,13 @@ _zsh_highlight_main_highlighter_highlight_argument() (( i = REPLY )) highlights+=($reply) 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 while [[ $arg[i+1] == [\^=~#+] ]]; do (( i += 1 )) @@ -969,7 +987,12 @@ _zsh_highlight_main_highlighter_highlight_double_quote() (( k += 1 )) # highlight both dollar signs (( i += 1 )) # don't consider the second one as introducing another parameter expansion 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 continue fi diff --git a/highlighters/main/test-data/dollar-paren.zsh b/highlighters/main/test-data/dollar-paren.zsh index 2a1aecd..c64a764 100644 --- a/highlighters/main/test-data/dollar-paren.zsh +++ b/highlighters/main/test-data/dollar-paren.zsh @@ -34,5 +34,6 @@ expected_region_highlight=( '1 1 builtin' # : '3 8 default' # "$(:)" '3 8 double-quoted-argument' # "$(:)" - '4 4 dollar-double-quoted-argument' # $ + '4 7 command-substitution' # $(:) + '6 6 builtin' # : ) diff --git a/highlighters/main/test-data/redirection-in-cmdsubst.zsh b/highlighters/main/test-data/redirection-in-cmdsubst.zsh index ac7a365..d5020f0 100644 --- a/highlighters/main/test-data/redirection-in-cmdsubst.zsh +++ b/highlighters/main/test-data/redirection-in-cmdsubst.zsh @@ -33,4 +33,7 @@ BUFFER=$': $(