Skip to content

Commit 22c4705

Browse files
committed
'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.)
1 parent e6264af commit 22c4705

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,11 @@ _zsh_highlight_main_calculate_fallback() {
152152
_zsh_highlight_main__type() {
153153
integer -r aliases_allowed=${2-1}
154154
integer -r resword_allowed=${3-1}
155-
# We won't cache replies of anything that exists as an alias at all, to
156-
# ensure the cached value is correct regardless of $aliases_allowed.
157-
#
158-
# ### We probably _should_ cache them in a cache that's keyed on the value of
159-
# ### $aliases_allowed, on the assumption that aliases are the common case.
160-
integer may_cache=1
155+
readonly cache_key="${1}:$(( aliases_allowed ? 1 : 0)):$((resword_allowed ? 1 : 0))"
161156

162157
# Cache lookup
163158
if (( $+_zsh_highlight_main__command_type_cache )); then
164-
REPLY=$_zsh_highlight_main__command_type_cache[(e)$1]
159+
REPLY=$_zsh_highlight_main__command_type_cache[(e)$cache_key]
165160
if [[ -n "$REPLY" ]]; then
166161
return
167162
fi
@@ -173,9 +168,6 @@ _zsh_highlight_main__type() {
173168
fi
174169
unset REPLY
175170
if zmodload -e zsh/parameter; then
176-
if (( $+aliases[(e)$1] )); then
177-
may_cache=0
178-
fi
179171
if (( aliases_allowed )) && (( $+aliases[(e)$1] )) &&; then
180172
REPLY=alias
181173
elif [[ $1 == *.* && -n ${1%.*} ]] && (( $+saliases[(e)${1##*.}] )); then
@@ -217,14 +209,11 @@ _zsh_highlight_main__type() {
217209
# «$(…)»], which is area that has had some parsing bugs before 5.6
218210
# (approximately).
219211
REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }"
220-
if [[ $REPLY == 'alias' ]]; then
221-
may_cache=0
222-
fi
223212
fi
224213

225214
# Cache population
226-
if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then
227-
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY
215+
if (( $+_zsh_highlight_main__command_type_cache )); then
216+
_zsh_highlight_main__command_type_cache[(e)$cache_key]=$REPLY
228217
fi
229218
[[ -n $REPLY ]]
230219
return $?
@@ -627,8 +616,10 @@ _zsh_highlight_main_highlighter_highlight_list()
627616
continue
628617
else
629618
_zsh_highlight_main_highlighter_expand_path $arg
630-
_zsh_highlight_main__type "$REPLY" 0 0
631-
res="$REPLY"
619+
if [[ $arg != $REPLY ]]; then
620+
_zsh_highlight_main__type "$REPLY" 0 0
621+
res="$REPLY"
622+
fi
632623
fi
633624
fi
634625

0 commit comments

Comments
 (0)