'main': Highlight named fd redirections.
Merge remote-tracking branch 'danielsh/i238-named-fd-redirection-v1' * danielsh/i238-named-fd-redirection-v1: 'main': Tighten condition. noop: Tweak condition at Matthew's suggestion 'main': Highlight named fd redirections.
This commit is contained in:
		
						commit
						6539f0d419
					
				|  | @ -58,6 +58,7 @@ This highlighter defines the following styles: | ||||||
| * `assign` - parameter assignments (`x=foo` and `x=( )`) | * `assign` - parameter assignments (`x=foo` and `x=( )`) | ||||||
| * `redirection` - redirection operators (`<`, `>`, etc) | * `redirection` - redirection operators (`<`, `>`, etc) | ||||||
| * `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`) | * `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`) | ||||||
|  | * `named-fd` - named file descriptor (`echo foo {fd}>&2`) | ||||||
| * `arg0` - a command word other than one of those enumerated above (other than a command, precommand, alias, function, or shell builtin command). | * `arg0` - a command word other than one of those enumerated above (other than a command, precommand, alias, function, or shell builtin command). | ||||||
| * `default` - everything else | * `default` - everything else | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -58,6 +58,7 @@ | ||||||
| : ${ZSH_HIGHLIGHT_STYLES[assign]:=none} | : ${ZSH_HIGHLIGHT_STYLES[assign]:=none} | ||||||
| : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none} | : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none} | ||||||
| : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} | : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} | ||||||
|  | : ${ZSH_HIGHLIGHT_STYLES[named-fd]:=none} | ||||||
| : ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green} | : ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green} | ||||||
| 
 | 
 | ||||||
| # Whether the highlighter should be called or not. | # Whether the highlighter should be called or not. | ||||||
|  | @ -378,6 +379,8 @@ _zsh_highlight_main_highlighter_highlight_list() | ||||||
|   local -a match mbegin mend list_highlights |   local -a match mbegin mend list_highlights | ||||||
|   # seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a |   # seen_alias is a map of aliases already seen to avoid loops like alias a=b b=a | ||||||
|   local -A seen_alias |   local -A seen_alias | ||||||
|  |   # Pattern for parameter names | ||||||
|  |   readonly parameter_name_pattern='([A-Za-z_][A-Za-z0-9_]*|[0-9]+)' | ||||||
|   list_highlights=() |   list_highlights=() | ||||||
| 
 | 
 | ||||||
|   # "R" for round |   # "R" for round | ||||||
|  | @ -552,13 +555,19 @@ _zsh_highlight_main_highlighter_highlight_list() | ||||||
| 
 | 
 | ||||||
|     # Analyse the current word. |     # Analyse the current word. | ||||||
|     if _zsh_highlight_main__is_redirection $arg ; then |     if _zsh_highlight_main__is_redirection $arg ; then | ||||||
|       if (( in_redirection )); then |       if (( in_redirection == 1 )); then | ||||||
|  |         # The condition excludes the case that BUFFER='{foo}>&2' and we're on the '>&'. | ||||||
|         _zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token |         _zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token | ||||||
|       else |       else | ||||||
|         in_redirection=2 |         in_redirection=2 | ||||||
|         _zsh_highlight_main_add_region_highlight $start_pos $end_pos redirection |         _zsh_highlight_main_add_region_highlight $start_pos $end_pos redirection | ||||||
|       fi |       fi | ||||||
|       continue |       continue | ||||||
|  |     elif [[ $arg == '{'${~parameter_name_pattern}'}' ]] && _zsh_highlight_main__is_redirection $args[1]; then | ||||||
|  |       # named file descriptor: {foo}>&2 | ||||||
|  |       in_redirection=3 | ||||||
|  |       _zsh_highlight_main_add_region_highlight $start_pos $end_pos named-fd | ||||||
|  |       continue | ||||||
|     fi |     fi | ||||||
| 
 | 
 | ||||||
|     # Expand parameters. |     # Expand parameters. | ||||||
|  | @ -580,7 +589,7 @@ _zsh_highlight_main_highlighter_highlight_list() | ||||||
|         parameter_name=${arg:1} |         parameter_name=${arg:1} | ||||||
|       fi |       fi | ||||||
|       if [[ $res == none ]] && zmodload -e zsh/parameter && |       if [[ $res == none ]] && zmodload -e zsh/parameter && | ||||||
|          [[ ${parameter_name} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && |          [[ ${parameter_name} =~ ^${~parameter_name_pattern}$ ]] && | ||||||
|          (( ${+parameters[(e)${MATCH}]} )) && [[ ${parameters[(e)$MATCH]} != *special* ]] |          (( ${+parameters[(e)${MATCH}]} )) && [[ ${parameters[(e)$MATCH]} != *special* ]] | ||||||
|          then |          then | ||||||
|         # Set $arg. |         # Set $arg. | ||||||
|  | @ -864,7 +873,7 @@ _zsh_highlight_main_highlighter_highlight_list() | ||||||
|                  elif [[ $arg == $'\x5d' ]] && _zsh_highlight_main__stack_pop 'Q' builtin; then |                  elif [[ $arg == $'\x5d' ]] && _zsh_highlight_main__stack_pop 'Q' builtin; then | ||||||
|                    : |                    : | ||||||
|                  else |                  else | ||||||
|                    _zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 - in_redirection )) |                    _zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection )) | ||||||
|                    continue |                    continue | ||||||
|                  fi |                  fi | ||||||
|                  ;; |                  ;; | ||||||
|  |  | ||||||
|  | @ -31,8 +31,8 @@ BUFFER='exec {foo}>&/tmp ls' | ||||||
| 
 | 
 | ||||||
| expected_region_highlight=( | expected_region_highlight=( | ||||||
|   "1 4 precommand" # exec |   "1 4 precommand" # exec | ||||||
|   "6 10 redirection 'issue #238'" # {foo} |   "6 10 named-fd" # {foo} | ||||||
|   "11 12 redirection" # >& |   "11 12 redirection" # >& | ||||||
|   "13 16 path" # /tmp |   "13 16 path" # /tmp | ||||||
|   "18 19 command 'issue #238'" # ls |   "18 19 command" # ls | ||||||
| ) | ) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue