mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2025-04-17 11:35:32 +08:00
Perf: speed up _zsh_highlight
This commit is contained in:
parent
edf433081e
commit
f063829450
@ -173,26 +173,48 @@ _zsh_highlight()
|
|||||||
region_highlight[-1]=()
|
region_highlight[-1]=()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Reset region_highlight to build it from scratch
|
|
||||||
if (( zsh_highlight__memo_feature )); then
|
|
||||||
region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
|
|
||||||
else
|
|
||||||
# Legacy codepath. Not very interoperable with other plugins (issue #418).
|
|
||||||
region_highlight=()
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
|
# Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
|
||||||
# For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
|
# For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
|
||||||
# This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
|
# This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
|
||||||
# and doesn't have the pattern matching bug
|
# and doesn't have the pattern matching bug
|
||||||
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) }; then
|
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) } ||
|
||||||
|
# Do not highlight if there are more than 300 chars in the buffer. It's most
|
||||||
|
# likely a pasted command or a huge list of files in that case..
|
||||||
|
[[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && (( ${#BUFFER} > ZSH_HIGHLIGHT_MAXLENGTH )) ||
|
||||||
|
# Do not highlight if there are pending inputs (copy/paste).
|
||||||
|
(( PENDING )); then
|
||||||
|
# Reset region_highlight to build it from scratch
|
||||||
|
if (( zsh_highlight__memo_feature )); then
|
||||||
|
region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
|
||||||
|
else
|
||||||
|
# Legacy codepath. Not very interoperable with other plugins (issue #418).
|
||||||
|
region_highlight=()
|
||||||
|
fi
|
||||||
return $ret
|
return $ret
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Before we 'emulate -L', save the user's options
|
# Before we 'emulate -L', save the user's options
|
||||||
local -A zsyh_user_options
|
local -A zsyh_user_options
|
||||||
if zmodload -e zsh/parameter; then
|
if zmodload -e zsh/parameter; then
|
||||||
zsyh_user_options=("${(kv)options[@]}")
|
if [[ -n ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#(brackets|cursor|line|main|pattern|regexp|root)} ]]; then
|
||||||
|
# Copy all options if there are user-defined highlighters
|
||||||
|
zsyh_user_options=("${(kv)options[@]}")
|
||||||
|
else
|
||||||
|
# Copy a subset of options used by the bundled highlighters. This is faster than
|
||||||
|
# copying all options.
|
||||||
|
zsyh_user_options=(
|
||||||
|
ignorebraces "${options[ignorebraces]}"
|
||||||
|
ignoreclosebraces "${options[ignoreclosebraces]}"
|
||||||
|
pathdirs "${options[pathdirs]}"
|
||||||
|
interactivecomments "${options[interactivecomments]}"
|
||||||
|
globassign "${options[globassign]}"
|
||||||
|
multifuncdef "${options[multifuncdef]}"
|
||||||
|
autocd "${options[autocd]}"
|
||||||
|
equals "${options[equals]}"
|
||||||
|
multios "${options[multios]}"
|
||||||
|
rcquotes "${options[rcquotes]}"
|
||||||
|
)
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
local canonical_options onoff option raw_options
|
local canonical_options onoff option raw_options
|
||||||
raw_options=(${(f)"$(emulate -R zsh; set -o)"})
|
raw_options=(${(f)"$(emulate -R zsh; set -o)"})
|
||||||
@ -210,20 +232,16 @@ _zsh_highlight()
|
|||||||
fi
|
fi
|
||||||
typeset -r zsyh_user_options
|
typeset -r zsyh_user_options
|
||||||
|
|
||||||
emulate -L zsh
|
local -a new_highlight
|
||||||
setopt localoptions warncreateglobal nobashrematch
|
if (( zsh_highlight__memo_feature )); then
|
||||||
|
new_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
|
||||||
|
fi
|
||||||
|
|
||||||
|
emulate -L zsh -o warncreateglobal -o nobashrematch
|
||||||
local REPLY # don't leak $REPLY into global scope
|
local REPLY # don't leak $REPLY into global scope
|
||||||
|
|
||||||
# Do not highlight if there are more than 300 chars in the buffer. It's most
|
|
||||||
# likely a pasted command or a huge list of files in that case..
|
|
||||||
[[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return $ret
|
|
||||||
|
|
||||||
# Do not highlight if there are pending inputs (copy/paste).
|
|
||||||
[[ $PENDING -gt 0 ]] && return $ret
|
|
||||||
|
|
||||||
{
|
{
|
||||||
local cache_place
|
local cache_place pred
|
||||||
local -a region_highlight_copy
|
|
||||||
|
|
||||||
# Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
|
# Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
|
||||||
local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do
|
local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do
|
||||||
@ -233,38 +251,37 @@ _zsh_highlight()
|
|||||||
typeset -ga ${cache_place}
|
typeset -ga ${cache_place}
|
||||||
|
|
||||||
# If highlighter needs to be invoked
|
# If highlighter needs to be invoked
|
||||||
if ! type "_zsh_highlight_highlighter_${highlighter}_predicate" >&/dev/null; then
|
pred="_zsh_highlight_highlighter_${highlighter}_predicate"
|
||||||
echo "zsh-syntax-highlighting: warning: disabling the ${(qq)highlighter} highlighter as it has not been loaded" >&2
|
if (( ! $pred )); then
|
||||||
# TODO: use ${(b)} rather than ${(q)} if supported
|
if type $pred >&/dev/null; then
|
||||||
ZSH_HIGHLIGHT_HIGHLIGHTERS=( ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#${highlighter}} )
|
typeset -gri $pred=1
|
||||||
elif "_zsh_highlight_highlighter_${highlighter}_predicate"; then
|
else
|
||||||
|
echo "zsh-syntax-highlighting: warning: disabling the ${(qq)highlighter} highlighter as it has not been loaded" >&2
|
||||||
# save a copy, and cleanup region_highlight
|
# TODO: use ${(b)} rather than ${(q)} if supported
|
||||||
region_highlight_copy=("${region_highlight[@]}")
|
ZSH_HIGHLIGHT_HIGHLIGHTERS=( ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#${highlighter}} )
|
||||||
region_highlight=()
|
continue
|
||||||
|
fi
|
||||||
# Execute highlighter and save result
|
|
||||||
{
|
|
||||||
"_zsh_highlight_highlighter_${highlighter}_paint"
|
|
||||||
} always {
|
|
||||||
: ${(AP)cache_place::="${region_highlight[@]}"}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Restore saved region_highlight
|
|
||||||
region_highlight=("${region_highlight_copy[@]}")
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Use value form cache if any cached
|
if $pred; then
|
||||||
region_highlight+=("${(@P)cache_place}")
|
# Execute highlighter and save result
|
||||||
|
region_highlight=()
|
||||||
|
"_zsh_highlight_highlighter_${highlighter}_paint"
|
||||||
|
: ${(AP)cache_place::="${region_highlight[@]}"}
|
||||||
|
new_highlight+=($region_highlight)
|
||||||
|
else
|
||||||
|
# Use value form cache if any cached
|
||||||
|
new_highlight+=("${(@P)cache_place}")
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
region_highlight=($new_highlight)
|
||||||
|
|
||||||
# Re-apply zle_highlight settings
|
# Re-apply zle_highlight settings
|
||||||
|
|
||||||
# region
|
# region
|
||||||
() {
|
(( REGION_ACTIVE )) && () {
|
||||||
(( REGION_ACTIVE )) || return
|
|
||||||
integer min max
|
integer min max
|
||||||
if (( MARK > CURSOR )) ; then
|
if (( MARK > CURSOR )) ; then
|
||||||
min=$CURSOR max=$MARK
|
min=$CURSOR max=$MARK
|
||||||
@ -284,13 +301,13 @@ _zsh_highlight()
|
|||||||
}
|
}
|
||||||
|
|
||||||
# yank / paste (zsh-5.1.1 and newer)
|
# yank / paste (zsh-5.1.1 and newer)
|
||||||
(( $+YANK_ACTIVE )) && (( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END"
|
(( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END"
|
||||||
|
|
||||||
# isearch
|
# isearch
|
||||||
(( $+ISEARCHMATCH_ACTIVE )) && (( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END"
|
(( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END"
|
||||||
|
|
||||||
# suffix
|
# suffix
|
||||||
(( $+SUFFIX_ACTIVE )) && (( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END"
|
(( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END"
|
||||||
|
|
||||||
|
|
||||||
return $ret
|
return $ret
|
||||||
|
Loading…
Reference in New Issue
Block a user