'main': Let aliases be type-cached, and don't run type-checking twice (with slightly different settings) if path expansion returned the input unchanged.

In particular, note that applying all but the last hunk of this diff
would cause «time» in command position to be highlighted as «command»
rather than «reserved».  (This is covered by tests.)
This commit is contained in:
Daniel Shahaf 2020-03-17 02:17:11 +00:00 committed by Daniel Shahaf
parent e0d1bc5c86
commit c27470a08d

View File

@ -159,16 +159,11 @@ _zsh_highlight_main_calculate_fallback() {
_zsh_highlight_main__type() { _zsh_highlight_main__type() {
integer -r aliases_allowed=${2-1} integer -r aliases_allowed=${2-1}
integer -r resword_allowed=${3-1} integer -r resword_allowed=${3-1}
# We won't cache replies of anything that exists as an alias at all, to readonly cache_key="${1}:$(( aliases_allowed ? 1 : 0)):$((resword_allowed ? 1 : 0))"
# ensure the cached value is correct regardless of $aliases_allowed.
#
# ### We probably _should_ cache them in a cache that's keyed on the value of
# ### $aliases_allowed, on the assumption that aliases are the common case.
integer may_cache=1
# Cache lookup # Cache lookup
if (( $+_zsh_highlight_main__command_type_cache )); then if (( $+_zsh_highlight_main__command_type_cache )); then
REPLY=$_zsh_highlight_main__command_type_cache[(e)$1] REPLY=$_zsh_highlight_main__command_type_cache[(e)$cache_key]
if [[ -n "$REPLY" ]]; then if [[ -n "$REPLY" ]]; then
return return
fi fi
@ -180,9 +175,6 @@ _zsh_highlight_main__type() {
fi fi
unset REPLY unset REPLY
if zmodload -e zsh/parameter; then if zmodload -e zsh/parameter; then
if (( $+aliases[(e)$1] )); then
may_cache=0
fi
if (( aliases_allowed )) && (( ${+galiases[(e)$1]} )); then if (( aliases_allowed )) && (( ${+galiases[(e)$1]} )); then
REPLY='global alias' REPLY='global alias'
elif (( aliases_allowed )) && (( $+aliases[(e)$1] )) &&; then elif (( aliases_allowed )) && (( $+aliases[(e)$1] )) &&; then
@ -226,14 +218,11 @@ _zsh_highlight_main__type() {
# «$(…)»], which is area that has had some parsing bugs before 5.6 # «$(…)»], which is area that has had some parsing bugs before 5.6
# (approximately). # (approximately).
REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }" REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }"
if [[ $REPLY == 'alias' ]]; then
may_cache=0
fi
fi fi
# Cache population # Cache population
if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then if (( $+_zsh_highlight_main__command_type_cache )); then
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY _zsh_highlight_main__command_type_cache[(e)$cache_key]=$REPLY
fi fi
[[ -n $REPLY ]] [[ -n $REPLY ]]
return $? return $?
@ -651,10 +640,12 @@ _zsh_highlight_main_highlighter_highlight_list()
continue continue
else else
_zsh_highlight_main_highlighter_expand_path $arg _zsh_highlight_main_highlighter_expand_path $arg
if [[ $arg != $REPLY ]]; then
_zsh_highlight_main__type "$REPLY" 0 0 _zsh_highlight_main__type "$REPLY" 0 0
res="$REPLY" res="$REPLY"
fi fi
fi fi
fi
# Analyse the current word. # Analyse the current word.
if _zsh_highlight_main__is_redirection $arg ; then if _zsh_highlight_main__is_redirection $arg ; then