Skip to content

Commit c27470a

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 e0d1bc5 commit c27470a

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
@@ -159,16 +159,11 @@ _zsh_highlight_main_calculate_fallback() {
159159
_zsh_highlight_main__type() {
160160
integer -r aliases_allowed=${2-1}
161161
integer -r resword_allowed=${3-1}
162-
# We won't cache replies of anything that exists as an alias at all, to
163-
# ensure the cached value is correct regardless of $aliases_allowed.
164-
#
165-
# ### We probably _should_ cache them in a cache that's keyed on the value of
166-
# ### $aliases_allowed, on the assumption that aliases are the common case.
167-
integer may_cache=1
162+
readonly cache_key="${1}:$(( aliases_allowed ? 1 : 0)):$((resword_allowed ? 1 : 0))"
168163

169164
# Cache lookup
170165
if (( $+_zsh_highlight_main__command_type_cache )); then
171-
REPLY=$_zsh_highlight_main__command_type_cache[(e)$1]
166+
REPLY=$_zsh_highlight_main__command_type_cache[(e)$cache_key]
172167
if [[ -n "$REPLY" ]]; then
173168
return
174169
fi
@@ -180,9 +175,6 @@ _zsh_highlight_main__type() {
180175
fi
181176
unset REPLY
182177
if zmodload -e zsh/parameter; then
183-
if (( $+aliases[(e)$1] )); then
184-
may_cache=0
185-
fi
186178
if (( aliases_allowed )) && (( ${+galiases[(e)$1]} )); then
187179
REPLY='global alias'
188180
elif (( aliases_allowed )) && (( $+aliases[(e)$1] )) &&; then
@@ -226,14 +218,11 @@ _zsh_highlight_main__type() {
226218
# «$(…)»], which is area that has had some parsing bugs before 5.6
227219
# (approximately).
228220
REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }"
229-
if [[ $REPLY == 'alias' ]]; then
230-
may_cache=0
231-
fi
232221
fi
233222

234223
# Cache population
235-
if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then
236-
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY
224+
if (( $+_zsh_highlight_main__command_type_cache )); then
225+
_zsh_highlight_main__command_type_cache[(e)$cache_key]=$REPLY
237226
fi
238227
[[ -n $REPLY ]]
239228
return $?
@@ -651,8 +640,10 @@ _zsh_highlight_main_highlighter_highlight_list()
651640
continue
652641
else
653642
_zsh_highlight_main_highlighter_expand_path $arg
654-
_zsh_highlight_main__type "$REPLY" 0 0
655-
res="$REPLY"
643+
if [[ $arg != $REPLY ]]; then
644+
_zsh_highlight_main__type "$REPLY" 0 0
645+
res="$REPLY"
646+
fi
656647
fi
657648
fi
658649

0 commit comments

Comments
 (0)