Skip to content

Commit 3b67e65

Browse files
committed
'main': Implement simple command type cache
1 parent 12b879c commit 3b67e65

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

highlighters/main/main-highlighter.zsh

+27
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ _zsh_highlight_main_add_region_highlight() {
8989
#
9090
# The result will be stored in REPLY.
9191
_zsh_highlight_main__type() {
92+
if (( $+_zsh_highlight_main__command_type_cache )); then
93+
REPLY=$_zsh_highlight_main__command_type_cache[(e)$1]
94+
if [[ -n "$REPLY" ]]; then
95+
return
96+
fi
97+
fi
9298
if (( $#options_to_set )); then
9399
setopt localoptions $options_to_set;
94100
fi
@@ -113,6 +119,9 @@ _zsh_highlight_main__type() {
113119
if ! (( $+REPLY )); then
114120
REPLY="${$(LC_ALL=C builtin type -w -- $1 2>/dev/null)#*: }"
115121
fi
122+
if (( $+_zsh_highlight_main__command_type_cache )); then
123+
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY
124+
fi
116125
}
117126

118127
# Check whether the first argument is a redirection operator token.
@@ -662,3 +671,21 @@ _zsh_highlight_main_highlighter_expand_path()
662671
unset REPLY
663672
: ${REPLY:=${(Q)~1}}
664673
}
674+
675+
# -------------------------------------------------------------------------------------------------
676+
# Main highlighter initialization
677+
# -------------------------------------------------------------------------------------------------
678+
679+
_zsh_highlight_main__precmd_hook() {
680+
_zsh_highlight_main__command_type_cache=()
681+
}
682+
683+
autoload -U add-zsh-hook
684+
if add-zsh-hook precmd _zsh_highlight_main__precmd_hook 2>/dev/null; then
685+
# Initialize command type cache
686+
typeset -gA _zsh_highlight_main__command_type_cache
687+
else
688+
echo 'zsh-syntax-highlighting: Failed to load add-zsh-hook. Some speed optimizations will not be used.' >&2
689+
# Make sure the cache is unset
690+
unset _zsh_highlight_main__command_type_cache
691+
fi

0 commit comments

Comments
 (0)