Skip to content

Commit 2172930

Browse files
committed
Perf: speed up _zsh_highlight_main__type
1 parent d944106 commit 2172930

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ _zsh_highlight_main_calculate_fallback() {
157157
#
158158
# The result will be stored in REPLY.
159159
_zsh_highlight_main__type() {
160+
# Cache lookup
161+
if (( $+_zsh_highlight_main__command_type_cache )); then
162+
REPLY=$_zsh_highlight_main__command_type_cache[$1]
163+
if [[ -n "$REPLY" ]]; then
164+
REPLY[-1]=
165+
[[ -n $REPLY ]]
166+
return
167+
fi
168+
fi
169+
160170
integer -r aliases_allowed=${2-1}
161171
# We won't cache replies of anything that exists as an alias at all, to
162172
# ensure the cached value is correct regardless of $aliases_allowed.
@@ -165,36 +175,28 @@ _zsh_highlight_main__type() {
165175
# ### $aliases_allowed, on the assumption that aliases are the common case.
166176
integer may_cache=1
167177

168-
# Cache lookup
169-
if (( $+_zsh_highlight_main__command_type_cache )); then
170-
REPLY=$_zsh_highlight_main__command_type_cache[(e)$1]
171-
if [[ -n "$REPLY" ]]; then
172-
return
173-
fi
174-
fi
175-
176178
# Main logic
177179
if (( $#options_to_set )); then
178180
setopt localoptions $options_to_set;
179181
fi
180182
unset REPLY
181183
if zmodload -e zsh/parameter; then
182-
if (( $+aliases[(e)$1] )); then
184+
if (( $+aliases[$1] )); then
183185
may_cache=0
184186
fi
185-
if (( ${+galiases[(e)$1]} )) && (( aliases_allowed )); then
187+
if (( ${+galiases[$1]} )) && (( aliases_allowed )); then
186188
REPLY='global alias'
187-
elif (( $+aliases[(e)$1] )) && (( aliases_allowed )); then
189+
elif (( $+aliases[$1] )) && (( aliases_allowed )); then
188190
REPLY=alias
189-
elif [[ $1 == *.* && -n ${1%.*} ]] && (( $+saliases[(e)${1##*.}] )); then
191+
elif [[ $1 == *.* && -n ${1%.*} ]] && (( $+saliases[${1##*.}] )); then
190192
REPLY='suffix alias'
191193
elif (( $reswords[(Ie)$1] )); then
192194
REPLY=reserved
193-
elif (( $+functions[(e)$1] )); then
195+
elif (( $+functions[$1] )); then
194196
REPLY=function
195-
elif (( $+builtins[(e)$1] )); then
197+
elif (( $+builtins[$1] )); then
196198
REPLY=builtin
197-
elif (( $+commands[(e)$1] )); then
199+
elif (( $+commands[$1] )); then
198200
REPLY=command
199201
# None of the special hashes had a match, so fall back to 'type -w', for
200202
# forward compatibility with future versions of zsh that may add new command
@@ -206,6 +208,12 @@ _zsh_highlight_main__type() {
206208
# falling through to the $() below, incurring a fork. (Issue #354.)
207209
#
208210
# The first disjunct mimics the isrelative() C call from the zsh bug.
211+
elif [[ $1 == */* || $ZSH_VERSION != (5.<9->*|<6->.*) ]]; then
212+
if [[ -n $1(#qN.*) || -o path_dirs && -n ${^path}/$1(#qN.*) ]]; then
213+
REPLY=command
214+
else
215+
REPLY=none
216+
fi
209217
elif { [[ $1 != */* ]] || is-at-least 5.3 } &&
210218
# Add a subshell to avoid a zsh upstream bug; see issue #606.
211219
# ### Remove the subshell when we stop supporting zsh 5.7.1 (I assume 5.8 will have the bugfix).
@@ -231,11 +239,10 @@ _zsh_highlight_main__type() {
231239
fi
232240

233241
# Cache population
234-
if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then
235-
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY
242+
if (( may_cache && $+_zsh_highlight_main__command_type_cache )); then
243+
_zsh_highlight_main__command_type_cache[$1]=$REPLY.
236244
fi
237245
[[ -n $REPLY ]]
238-
return $?
239246
}
240247

241248
# Checks whether $1 is something that can be run.
@@ -1241,7 +1248,7 @@ _zsh_highlight_main_highlighter_check_path()
12411248
fi
12421249
return 0
12431250
elif [[ ! -d $expanded_path ]]; then
1244-
# ### This seems unreachable for the current callers
1251+
REPLY=command
12451252
return 0
12461253
fi
12471254
fi

0 commit comments

Comments
 (0)