Skip to content

Commit 256913c

Browse files
committed
Perf: memoize _zsh_highlight_main_highlighter_highlight_argument
1 parent cb97bde commit 256913c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,19 @@ _zsh_highlight_main_highlighter_check_path()
12341234
# This function currently assumes it's never called for the command word.
12351235
_zsh_highlight_main_highlighter_highlight_argument()
12361236
{
1237+
if (( $+_zsh_highlight_main__arg_cache )); then
1238+
local cache_key=$1$'\0'$2$'\0'$arg$'\0'$last_arg$'\0'$has_end$'\0'$highlight_glob$'\0'$in_redirection$'\0'$zsyh_user_options[multios]
1239+
local -a cache_val=(${(@0)_zsh_highlight_main__arg_cache[$cache_key]})
1240+
if (( $#cache_val )); then
1241+
integer offset=$(( start_pos - $cache_val[-1] ))
1242+
local start end_ style
1243+
for start end_ style in $cache_val[1,-2]; do
1244+
_zsh_highlight_main_add_region_highlight $(( start + offset )) $(( end_ + offset )) $style
1245+
done
1246+
return
1247+
fi
1248+
fi
1249+
12371250
local base_style=default i=$1 option_eligible=${2:-1} path_eligible=1 ret start style
12381251
local -a highlights
12391252

@@ -1381,6 +1394,11 @@ _zsh_highlight_main_highlighter_highlight_argument()
13811394

13821395
highlights=($(( start_pos + $1 - 1 )) $end_pos $base_style $highlights)
13831396
_zsh_highlight_main_add_many_region_highlights $highlights
1397+
1398+
if (( $+_zsh_highlight_main__arg_cache )); then
1399+
highlights+=($start_pos)
1400+
_zsh_highlight_main__arg_cache[$cache_key]=${(pj:\0:)highlights}
1401+
fi
13841402
}
13851403

13861404
# Quote Helper Functions
@@ -1761,6 +1779,7 @@ _zsh_highlight_main__precmd_hook() {
17611779

17621780
_zsh_highlight_main__command_type_cache=()
17631781
_zsh_highlight_main__path_cache=()
1782+
_zsh_highlight_main__arg_cache=()
17641783

17651784
if [[ $ZSH_VERSION != (5.<9->*|<6->.*) ]]; then
17661785
_zsh_highlight_main_calculate_styles
@@ -1770,11 +1789,11 @@ _zsh_highlight_main__precmd_hook() {
17701789
autoload -Uz add-zsh-hook
17711790
if add-zsh-hook precmd _zsh_highlight_main__precmd_hook 2>/dev/null; then
17721791
# Initialize caches
1773-
typeset -gA _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache
1792+
typeset -gA _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache _zsh_highlight_main__arg_cache
17741793
else
17751794
print -r -- >&2 'zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.'
17761795
# Make sure the caches are unset
1777-
unset _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache
1796+
unset _zsh_highlight_main__command_type_cache _zsh_highlight_main__path_cache _zsh_highlight_main__arg_cache
17781797
fi
17791798
typeset -ga ZSH_HIGHLIGHT_DIRS_BLACKLIST
17801799

0 commit comments

Comments
 (0)