mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2025-04-17 11:35:32 +08:00
Perf: memoize _zsh_highlight_main_highlighter_check_path
This commit is contained in:
parent
4cf464f843
commit
826aa7312d
@ -1182,6 +1182,19 @@ _zsh_highlight_main_highlighter_highlight_path_separators()
|
||||
# $2 should be non-zero iff we're in command position.
|
||||
_zsh_highlight_main_highlighter_check_path()
|
||||
{
|
||||
if (( $+_zsh_highlight_main__path_cache )); then
|
||||
local cache_key=$1$'\0'$2
|
||||
if (( has_end && len == end_pos && !in_alias )) && [[ $WIDGET != zle-line-finish ]]; then
|
||||
cache_key+=$'\0'
|
||||
fi
|
||||
local cache_val=$_zsh_highlight_main__path_cache[$cache_key]
|
||||
if [[ -n $cache_val ]]; then
|
||||
REPLY=${cache_val:1}
|
||||
return $cache_val[1]
|
||||
fi
|
||||
fi
|
||||
|
||||
{
|
||||
_zsh_highlight_main_highlighter_expand_path "$1"
|
||||
local expanded_path="$REPLY" tmp_path
|
||||
integer in_command_position=$2
|
||||
@ -1274,6 +1287,12 @@ _zsh_highlight_main_highlighter_check_path()
|
||||
|
||||
# It's not a path.
|
||||
return 1
|
||||
} always {
|
||||
local -i ret=$((!!$?))
|
||||
if (( $+_zsh_highlight_main__path_cache )); then
|
||||
_zsh_highlight_main__path_cache[$cache_key]=$ret$REPLY
|
||||
fi
|
||||
}
|
||||
}
|
||||
|
||||
# Highlight an argument and possibly special chars in quotes starting at $1 in $arg
|
||||
@ -1809,15 +1828,16 @@ _zsh_highlight_main__precmd_hook() {
|
||||
fi
|
||||
|
||||
_zsh_highlight_main__command_type_cache=()
|
||||
_zsh_highlight_main__path_cache=()
|
||||
}
|
||||
|
||||
autoload -Uz add-zsh-hook
|
||||
if add-zsh-hook precmd _zsh_highlight_main__precmd_hook 2>/dev/null; then
|
||||
# Initialize command type cache
|
||||
typeset -gA _zsh_highlight_main__command_type_cache
|
||||
# Initialize caches
|
||||
typeset -gA _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache
|
||||
else
|
||||
print -r -- >&2 'zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.'
|
||||
# Make sure the cache is unset
|
||||
unset _zsh_highlight_main__command_type_cache
|
||||
# Make sure the caches are unset
|
||||
unset _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache
|
||||
fi
|
||||
typeset -ga ZSH_HIGHLIGHT_DIRS_BLACKLIST
|
||||
|
Loading…
Reference in New Issue
Block a user