From bf055f7df2f2e9c825ebb6f306eb171233d3cfcf Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 22:41:23 +0000 Subject: [PATCH] 'main': Fix the last commit's issue concerning parameter expansion at command word. --- highlighters/main/main-highlighter.zsh | 37 ++++++++++--------- .../main/test-data/commmand-parameter.zsh | 4 +- .../main/test-data/path-dollared-word3.zsh | 4 +- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 616d59b..667b0ac 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -499,6 +499,26 @@ _zsh_highlight_main_highlighter_highlight_list() } fi + # Expand parameters. + # + # ### For now, expand just '$foo', like that, without braces or anything. + () { + # That's not entirely correct --- if the parameter's value happens to be a reserved + # word, the parameter expansion will be highlighted as a reserved word --- but that + # incorrectness is outweighed by the usability improvement of permitting the use of + # parameters that refer to commands, functions, and builtins. + local -a match mbegin mend + local MATCH; integer MBEGIN MEND + if [[ $res == none ]] && (( ${+parameters} )) && + [[ ${arg[1]} == \$ ]] && [[ ${arg:1} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && + (( ${+parameters[(e)${MATCH}]} )) && [[ ${parameters[(e)$MATCH]} != *special* ]] + then + arg=${(P)MATCH} + _zsh_highlight_main__type "$arg" + res=$REPLY + fi + } + # Special-case the first word after 'sudo'. if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then @@ -539,23 +559,6 @@ _zsh_highlight_main_highlighter_highlight_list() next_word+=':sudo_opt:' next_word+=':start:' else - () { - # Special-case: command word is '$foo', like that, without braces or anything. - # - # That's not entirely correct --- if the parameter's value happens to be a reserved - # word, the parameter expansion will be highlighted as a reserved word --- but that - # incorrectness is outweighed by the usability improvement of permitting the use of - # parameters that refer to commands, functions, and builtins. - local -a match mbegin mend - local MATCH; integer MBEGIN MEND - if [[ $res == none ]] && (( ${+parameters} )) && - [[ ${arg[1]} == \$ ]] && [[ ${arg:1} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && - (( ${+parameters[(e)${MATCH}]} )) && [[ ${parameters[(e)$MATCH]} != *special* ]] - then - _zsh_highlight_main__type ${(P)MATCH} - res=$REPLY - fi - } case $res in reserved) # reserved word style=reserved-word diff --git a/highlighters/main/test-data/commmand-parameter.zsh b/highlighters/main/test-data/commmand-parameter.zsh index 5cded9b..afa9bc5 100644 --- a/highlighters/main/test-data/commmand-parameter.zsh +++ b/highlighters/main/test-data/commmand-parameter.zsh @@ -29,6 +29,8 @@ local x=/usr/bin/env local y=sudo +sudo(){} + BUFFER='$x "argument"; $y' expected_region_highlight=( @@ -36,5 +38,5 @@ expected_region_highlight=( "4 13 default" # "argument" "4 13 double-quoted-argument" # "argument" "14 14 commandseparator" # ; - "16 17 precommand 'parameter expansion precedes precommand recognition'" # $y (sudo) + "16 17 precommand" # $y (sudo) ) diff --git a/highlighters/main/test-data/path-dollared-word3.zsh b/highlighters/main/test-data/path-dollared-word3.zsh index a9a5fa3..911ea81 100644 --- a/highlighters/main/test-data/path-dollared-word3.zsh +++ b/highlighters/main/test-data/path-dollared-word3.zsh @@ -29,10 +29,12 @@ # «/usr» at this point would be highlighted as path_prefix; so should # a parameter that expands to an equivalent string be highlighted. +# +# More complicated parameter substitutions aren't eval'd; issue #328. BUFFER='$PWD; ${PWD}' expected_region_highlight=( - "1 4 unknown-token" # $PWD - not eval'd; issue #328 + "1 4 path" # $PWD "5 5 commandseparator" # ; "7 12 unknown-token" # ${PWD} )