highlighters: Pass around the style key instead of the style itself
This commit is contained in:
parent
a830613467
commit
089329660b
@ -82,11 +82,11 @@ _zsh_highlight_brackets_highlighter()
|
|||||||
if [[ -n $matching[$pos] ]] && [[ $typepos[$pos] == $typepos[$matching[$pos]] ]]; then
|
if [[ -n $matching[$pos] ]] && [[ $typepos[$pos] == $typepos[$matching[$pos]] ]]; then
|
||||||
local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]}
|
local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]}
|
||||||
local bracket_color_level=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 ))
|
local bracket_color_level=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 ))
|
||||||
local style=$ZSH_HIGHLIGHT_STYLES[$bracket_color_level]
|
local style=$bracket_color_level
|
||||||
region_highlight+=("$pos $((pos + 1)) $style")
|
region_highlight+=("$pos $((pos + 1)) $ZSH_HIGHLIGHT_STYLES[$style]")
|
||||||
else
|
else
|
||||||
local style=$ZSH_HIGHLIGHT_STYLES[bracket-error]
|
local style=bracket-error
|
||||||
region_highlight+=("$pos $((pos + 1)) $style")
|
region_highlight+=("$pos $((pos + 1)) $ZSH_HIGHLIGHT_STYLES[$style]")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -94,8 +94,8 @@ _zsh_highlight_brackets_highlighter()
|
|||||||
pos=$CURSOR
|
pos=$CURSOR
|
||||||
if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then
|
if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then
|
||||||
local otherpos=$matching[$pos]
|
local otherpos=$matching[$pos]
|
||||||
local style=$ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]
|
local style=cursor-matchingbracket
|
||||||
region_highlight+=("$otherpos $((otherpos + 1)) $style")
|
region_highlight+=("$otherpos $((otherpos + 1)) $ZSH_HIGHLIGHT_STYLES[$style]")
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ _zsh_highlight_main_highlighter_predicate()
|
|||||||
# Helper to deal with tokens crossing line boundaries.
|
# Helper to deal with tokens crossing line boundaries.
|
||||||
_zsh_highlight_main_add_region_highlight() {
|
_zsh_highlight_main_add_region_highlight() {
|
||||||
integer start=$1 end=$2
|
integer start=$1 end=$2
|
||||||
local style=$3
|
local style=$ZSH_HIGHLIGHT_STYLES[$3]
|
||||||
|
|
||||||
# The calculation was relative to $PREBUFFER$BUFFER, but region_highlight is
|
# The calculation was relative to $PREBUFFER$BUFFER, but region_highlight is
|
||||||
# relative to $BUFFER.
|
# relative to $BUFFER.
|
||||||
@ -245,9 +245,9 @@ _zsh_highlight_main_highlighter()
|
|||||||
|
|
||||||
if [[ -n ${interactive_comments+'set'} && $arg[1] == $histchars[3] ]]; then
|
if [[ -n ${interactive_comments+'set'} && $arg[1] == $histchars[3] ]]; then
|
||||||
if [[ $this_word == *(':regular:'|':start:')* ]]; then
|
if [[ $this_word == *(':regular:'|':start:')* ]]; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[comment]
|
style=comment
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token] # prematurely terminated
|
style=unknown-token # prematurely terminated
|
||||||
fi
|
fi
|
||||||
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
||||||
already_added=1
|
already_added=1
|
||||||
@ -275,9 +275,9 @@ _zsh_highlight_main_highlighter()
|
|||||||
|
|
||||||
if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word
|
if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word
|
||||||
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
|
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[precommand]
|
style=precommand
|
||||||
elif [[ "$arg" = "sudo" ]]; then
|
elif [[ "$arg" = "sudo" ]]; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[precommand]
|
style=precommand
|
||||||
next_word=${next_word//:regular:/}
|
next_word=${next_word//:regular:/}
|
||||||
next_word+=':sudo_opt:'
|
next_word+=':sudo_opt:'
|
||||||
next_word+=':start:'
|
next_word+=':start:'
|
||||||
@ -300,9 +300,9 @@ _zsh_highlight_main_highlighter()
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
case $res in
|
case $res in
|
||||||
*': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];;
|
*': reserved') style=reserved-word;;
|
||||||
*': suffix alias')
|
*': suffix alias')
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[suffix-alias]
|
style=suffix-alias
|
||||||
;;
|
;;
|
||||||
*': alias') () {
|
*': alias') () {
|
||||||
integer insane_alias
|
integer insane_alias
|
||||||
@ -319,20 +319,20 @@ _zsh_highlight_main_highlighter()
|
|||||||
(*) :;;
|
(*) :;;
|
||||||
esac
|
esac
|
||||||
if (( insane_alias )); then
|
if (( insane_alias )); then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
style=unknown-token
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[alias]
|
style=alias
|
||||||
local aliased_command="${"$(alias -- $arg)"#*=}"
|
local aliased_command="${"$(alias -- $arg)"#*=}"
|
||||||
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$aliased_command"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg)
|
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$aliased_command"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg)
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
;;
|
;;
|
||||||
*': builtin') style=$ZSH_HIGHLIGHT_STYLES[builtin];;
|
*': builtin') style=builtin;;
|
||||||
*': function') style=$ZSH_HIGHLIGHT_STYLES[function];;
|
*': function') style=function;;
|
||||||
*': command') style=$ZSH_HIGHLIGHT_STYLES[command];;
|
*': command') style=command;;
|
||||||
*': hashed') style=$ZSH_HIGHLIGHT_STYLES[hashed-command];;
|
*': hashed') style=hashed-command;;
|
||||||
*) if _zsh_highlight_main_highlighter_check_assign; then
|
*) if _zsh_highlight_main_highlighter_check_assign; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[assign]
|
style=assign
|
||||||
if [[ $arg[-1] == '(' ]]; then
|
if [[ $arg[-1] == '(' ]]; then
|
||||||
in_array_assignment=true
|
in_array_assignment=true
|
||||||
else
|
else
|
||||||
@ -341,20 +341,20 @@ _zsh_highlight_main_highlighter()
|
|||||||
next_word+=':start:'
|
next_word+=':start:'
|
||||||
fi
|
fi
|
||||||
elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
|
elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
|
style=history-expansion
|
||||||
elif [[ $arg[0,1] == $histchars[2,2] ]]; then
|
elif [[ $arg[0,1] == $histchars[2,2] ]]; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
|
style=history-expansion
|
||||||
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
|
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
|
||||||
if [[ $this_word == *':regular:'* ]]; then
|
if [[ $this_word == *':regular:'* ]]; then
|
||||||
# This highlights empty commands (semicolon follows nothing) as an error.
|
# This highlights empty commands (semicolon follows nothing) as an error.
|
||||||
# Zsh accepts them, though.
|
# Zsh accepts them, though.
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[commandseparator]
|
style=commandseparator
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
style=unknown-token
|
||||||
fi
|
fi
|
||||||
elif [[ $arg == (<0-9>|)(\<|\>)* ]]; then
|
elif [[ $arg == (<0-9>|)(\<|\>)* ]]; then
|
||||||
# A '<' or '>', possibly followed by a digit
|
# A '<' or '>', possibly followed by a digit
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[redirection]
|
style=redirection
|
||||||
(( in_redirection=2 ))
|
(( in_redirection=2 ))
|
||||||
elif [[ $arg[1,2] == '((' ]]; then
|
elif [[ $arg[1,2] == '((' ]]; then
|
||||||
# Arithmetic evaluation.
|
# Arithmetic evaluation.
|
||||||
@ -366,7 +366,7 @@ _zsh_highlight_main_highlighter()
|
|||||||
#
|
#
|
||||||
# We highlight just the opening parentheses, as a reserved word; this
|
# We highlight just the opening parentheses, as a reserved word; this
|
||||||
# is how [[ ... ]] is highlighted, too.
|
# is how [[ ... ]] is highlighted, too.
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[reserved-word]
|
style=reserved-word
|
||||||
_zsh_highlight_main_add_region_highlight $start_pos $((start_pos + 2)) $style
|
_zsh_highlight_main_add_region_highlight $start_pos $((start_pos + 2)) $style
|
||||||
already_added=1
|
already_added=1
|
||||||
if [[ $arg[-2,-1] == '))' ]]; then
|
if [[ $arg[-2,-1] == '))' ]]; then
|
||||||
@ -376,12 +376,12 @@ _zsh_highlight_main_highlighter()
|
|||||||
elif [[ $arg == '()' || $arg == $'\x28' ]]; then
|
elif [[ $arg == '()' || $arg == $'\x28' ]]; then
|
||||||
# anonymous function
|
# anonymous function
|
||||||
# subshell
|
# subshell
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[reserved-word]
|
style=reserved-word
|
||||||
else
|
else
|
||||||
if _zsh_highlight_main_highlighter_check_path; then
|
if _zsh_highlight_main_highlighter_check_path; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[path]
|
style=path
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
style=unknown-token
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
@ -391,52 +391,52 @@ _zsh_highlight_main_highlighter()
|
|||||||
case $arg in
|
case $arg in
|
||||||
$'\x29') # subshell or end of array assignment
|
$'\x29') # subshell or end of array assignment
|
||||||
if $in_array_assignment; then
|
if $in_array_assignment; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[assign]
|
style=assign
|
||||||
in_array_assignment=false
|
in_array_assignment=false
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[reserved-word]
|
style=reserved-word
|
||||||
fi;;
|
fi;;
|
||||||
$'\x7d') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];; # block
|
$'\x7d') style=reserved-word;; # block
|
||||||
'--'*) style=$ZSH_HIGHLIGHT_STYLES[double-hyphen-option];;
|
'--'*) style=double-hyphen-option;;
|
||||||
'-'*) style=$ZSH_HIGHLIGHT_STYLES[single-hyphen-option];;
|
'-'*) style=single-hyphen-option;;
|
||||||
"'"*) style=$ZSH_HIGHLIGHT_STYLES[single-quoted-argument];;
|
"'"*) style=single-quoted-argument;;
|
||||||
'"'*) style=$ZSH_HIGHLIGHT_STYLES[double-quoted-argument]
|
'"'*) style=double-quoted-argument
|
||||||
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
||||||
_zsh_highlight_main_highlighter_highlight_string
|
_zsh_highlight_main_highlighter_highlight_string
|
||||||
already_added=1
|
already_added=1
|
||||||
;;
|
;;
|
||||||
\$\'*) style=$ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]
|
\$\'*) style=dollar-quoted-argument
|
||||||
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
||||||
_zsh_highlight_main_highlighter_highlight_dollar_string
|
_zsh_highlight_main_highlighter_highlight_dollar_string
|
||||||
already_added=1
|
already_added=1
|
||||||
;;
|
;;
|
||||||
'`'*) style=$ZSH_HIGHLIGHT_STYLES[back-quoted-argument];;
|
'`'*) style=back-quoted-argument;;
|
||||||
[*?]*|*[^\\][*?]*)
|
[*?]*|*[^\\][*?]*)
|
||||||
$highlight_glob && style=$ZSH_HIGHLIGHT_STYLES[globbing] || style=$ZSH_HIGHLIGHT_STYLES[default];;
|
$highlight_glob && style=globbing || style=default;;
|
||||||
*) if false; then
|
*) if false; then
|
||||||
elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
|
elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
|
style=history-expansion
|
||||||
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
|
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
|
||||||
if [[ $this_word == *':regular:'* ]]; then
|
if [[ $this_word == *':regular:'* ]]; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[commandseparator]
|
style=commandseparator
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
style=unknown-token
|
||||||
fi
|
fi
|
||||||
elif [[ $arg == (<0-9>|)(\<|\>)* ]]; then
|
elif [[ $arg == (<0-9>|)(\<|\>)* ]]; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[redirection]
|
style=redirection
|
||||||
(( in_redirection=2 ))
|
(( in_redirection=2 ))
|
||||||
else
|
else
|
||||||
if _zsh_highlight_main_highlighter_check_path; then
|
if _zsh_highlight_main_highlighter_check_path; then
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[path]
|
style=path
|
||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[default]
|
style=default
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
|
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
|
||||||
[[ -n $style_override ]] && style=$ZSH_HIGHLIGHT_STYLES[$style_override]
|
[[ -n $style_override ]] && style=$style_override
|
||||||
(( already_added )) || _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
(( already_added )) || _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
|
||||||
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
|
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
|
||||||
next_word=':start:'
|
next_word=':start:'
|
||||||
@ -508,7 +508,7 @@ _zsh_highlight_main_highlighter_highlight_string()
|
|||||||
(( j = i + start_pos - 1 ))
|
(( j = i + start_pos - 1 ))
|
||||||
(( k = j + 1 ))
|
(( k = j + 1 ))
|
||||||
case "$arg[$i]" in
|
case "$arg[$i]" in
|
||||||
'$' ) style=$ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]
|
'$' ) style=dollar-double-quoted-argument
|
||||||
# Look for an alphanumeric parameter name.
|
# Look for an alphanumeric parameter name.
|
||||||
if [[ ${arg:$i} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+) ]] ; then
|
if [[ ${arg:$i} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+) ]] ; then
|
||||||
(( k += $#MATCH )) # highlight the parameter name
|
(( k += $#MATCH )) # highlight the parameter name
|
||||||
@ -520,7 +520,7 @@ _zsh_highlight_main_highlighter_highlight_string()
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]
|
"\\") style=back-double-quoted-argument
|
||||||
if [[ \\\`\"\$ == *$arg[$i+1]* ]]; then
|
if [[ \\\`\"\$ == *$arg[$i+1]* ]]; then
|
||||||
(( k += 1 )) # Color following char too.
|
(( k += 1 )) # Color following char too.
|
||||||
(( i += 1 )) # Skip parsing the escaped char.
|
(( i += 1 )) # Skip parsing the escaped char.
|
||||||
@ -549,7 +549,7 @@ _zsh_highlight_main_highlighter_highlight_dollar_string()
|
|||||||
(( j = i + start_pos - 1 ))
|
(( j = i + start_pos - 1 ))
|
||||||
(( k = j + 1 ))
|
(( k = j + 1 ))
|
||||||
case "$arg[$i]" in
|
case "$arg[$i]" in
|
||||||
"\\") style=$ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]
|
"\\") style=back-dollar-quoted-argument
|
||||||
for (( c = i + 1 ; c <= end_pos - start_pos ; c += 1 )); do
|
for (( c = i + 1 ; c <= end_pos - start_pos ; c += 1 )); do
|
||||||
[[ "$arg[$c]" != ([0-9xXuUa-fA-F]) ]] && break
|
[[ "$arg[$c]" != ([0-9xXuUa-fA-F]) ]] && break
|
||||||
done
|
done
|
||||||
@ -565,7 +565,7 @@ _zsh_highlight_main_highlighter_highlight_dollar_string()
|
|||||||
else
|
else
|
||||||
if (( $#arg > $i+1 )) && [[ $arg[$i+1] == [xXuU] ]]; then
|
if (( $#arg > $i+1 )) && [[ $arg[$i+1] == [xXuU] ]]; then
|
||||||
# \x not followed by hex digits is probably an error
|
# \x not followed by hex digits is probably an error
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
style=unknown-token
|
||||||
fi
|
fi
|
||||||
(( k += 1 )) # Color following char too.
|
(( k += 1 )) # Color following char too.
|
||||||
(( i += 1 )) # Skip parsing the escaped char.
|
(( i += 1 )) # Skip parsing the escaped char.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user