Skip to content

Commit 1b8f49a

Browse files
committed
Perf: backport recent changes to older zsh versions
1 parent 818e2f2 commit 1b8f49a

File tree

2 files changed

+32
-55
lines changed

2 files changed

+32
-55
lines changed

highlighters/main/main-highlighter.zsh

+29-53
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ _zsh_highlight_main_calculate_styles() {
9999
local config="${(pj:\0:)${(@kv)ZSH_HIGHLIGHT_STYLES}}".
100100
[[ $config == $_zsh_highlight_main__config ]] && return
101101
_zsh_highlight_main__config=$config
102-
typeset -gA _zsh_highlight_main__styles=("${(@kv)ZSH_HIGHLIGHT_STYLES}")
102+
typeset -gA _zsh_highlight_main__styles
103+
_zsh_highlight_main__styles=("${(@kv)ZSH_HIGHLIGHT_STYLES}")
103104

104105
integer finished
105106
local key val
@@ -131,16 +132,11 @@ _zsh_highlight_main_calculate_fallback() {
131132
#
132133
# If $2 is 0, do not consider aliases.
133134
#
134-
# The result will be stored in REPLY.
135+
# The result will be stored in REPLY. It's guaranteed to be non-empty.
135136
_zsh_highlight_main__type() {
136137
# Cache lookup
137138
if (( $+_zsh_highlight_main__command_type_cache )); then
138-
REPLY=$_zsh_highlight_main__command_type_cache[$1]
139-
if [[ -n "$REPLY" ]]; then
140-
REPLY[-1]=
141-
[[ -n $REPLY ]]
142-
return
143-
fi
139+
[[ -n ${REPLY::=$_zsh_highlight_main__command_type_cache[$1]} ]] && return
144140
fi
145141

146142
integer -r aliases_allowed=${2-1}
@@ -152,9 +148,6 @@ _zsh_highlight_main__type() {
152148
integer may_cache=1
153149

154150
# Main logic
155-
if (( $#options_to_set )); then
156-
setopt localoptions $options_to_set;
157-
fi
158151
unset REPLY
159152
if zmodload -e zsh/parameter; then
160153
if (( $+aliases[$1] )); then
@@ -174,62 +167,44 @@ _zsh_highlight_main__type() {
174167
REPLY=builtin
175168
elif (( $+commands[$1] )); then
176169
REPLY=command
177-
# None of the special hashes had a match, so fall back to 'type -w', for
178-
# forward compatibility with future versions of zsh that may add new command
179-
# types.
180-
#
181-
# zsh 5.2 and older have a bug whereby running 'type -w ./sudo' implicitly
182-
# runs 'hash ./sudo=/usr/local/bin/./sudo' (assuming /usr/local/bin/sudo
183-
# exists and is in $PATH). Avoid triggering the bug, at the expense of
184-
# falling through to the $() below, incurring a fork. (Issue #354.)
185-
#
186-
# The first disjunct mimics the isrelative() C call from the zsh bug.
187-
elif [[ $1 == */* || $ZSH_VERSION != (5.<9->*|<6->.*) ]]; then
188-
if [[ -n $1(#qN.*) || -o path_dirs && -n ${^path}/$1(#qN.*) ]]; then
170+
# ZSH_VERSION >= 5.1 allows the use of #q. ZSH_VERSION <= 5.8 allows skipping
171+
# 'type -w' calls that are necessary for forward compatibility.
172+
elif [[ $ZSH_VERSION == 5.<1-8>(|.*) ]]; then
173+
if [[ -n $1(#qN-.*) ||
174+
$1 == [^/]*/* && $zsyh_user_options[pathdirs] == on && -n ${^path}/$1(#q-N.*) ]]; then
189175
REPLY=command
190176
else
191177
REPLY=none
192178
fi
193-
elif { [[ $1 != */* ]] || is-at-least 5.3 } &&
194-
# Add a subshell to avoid a zsh upstream bug; see issue #606.
195-
# ### Remove the subshell when we stop supporting zsh 5.7.1 (I assume 5.8 will have the bugfix).
196-
! (builtin type -w -- "$1") >/dev/null 2>&1; then
197-
REPLY=none
198179
fi
199180
fi
200-
if ! (( $+REPLY )); then
181+
if (( ! $+REPLY )); then
201182
# zsh/parameter not available or had no matches.
202183
#
203184
# Note that 'type -w' will run 'rehash' implicitly.
204185
#
205186
# We 'unalias' in a subshell, so the parent shell is not affected.
206-
#
207-
# The colon command is there just to avoid a command substitution that
208-
# starts with an arithmetic expression [«((…))» as the first thing inside
209-
# «$(…)»], which is area that has had some parsing bugs before 5.6
210-
# (approximately).
211-
REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }"
187+
REPLY="${${$(
188+
[[ $zsyh_user_options[pathdirs] == on ]] && setopt pathdirs
189+
(( aliases_allowed )) || unalias -- "$1" 2>/dev/null
190+
LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }:-none}"
212191
if [[ $REPLY == 'alias' ]]; then
213192
may_cache=0
214193
fi
215194
fi
216195

217196
# Cache population
218197
if (( may_cache && $+_zsh_highlight_main__command_type_cache )); then
219-
_zsh_highlight_main__command_type_cache[$1]=$REPLY.
198+
_zsh_highlight_main__command_type_cache[$1]=$REPLY
220199
fi
221-
[[ -n $REPLY ]]
222200
}
223201

224202
# Checks whether $1 is something that can be run.
225203
#
226-
# Return 0 if runnable, 1 if not runnable, 2 if trouble.
204+
# Return 0 if runnable, 1 if not runnable.
227205
_zsh_highlight_main__is_runnable() {
228-
if _zsh_highlight_main__type "$1"; then
229-
[[ $REPLY != none ]]
230-
else
231-
return 2
232-
fi
206+
_zsh_highlight_main__type "$1"
207+
[[ $REPLY != none ]]
233208
}
234209

235210
# Check whether the first argument is a redirection operator token.
@@ -301,7 +276,7 @@ _zsh_highlight_highlighter_main_paint()
301276
return
302277
fi
303278

304-
local -a options_to_set reply # used in callees
279+
local -a reply # used in callees
305280
local REPLY
306281

307282
# $flags_with_argument is a set of letters, corresponding to the option letters
@@ -327,10 +302,6 @@ _zsh_highlight_highlighter_main_paint()
327302
local -i right_brace_is_recognised_everywhere=1
328303
fi
329304

330-
if [[ $zsyh_user_options[pathdirs] == on ]]; then
331-
options_to_set+=( PATH_DIRS )
332-
fi
333-
334305
if (( $+X_ZSH_HIGHLIGHT_DIRS_BLACKLIST )); then
335306
print >&2 'zsh-syntax-highlighting: X_ZSH_HIGHLIGHT_DIRS_BLACKLIST is deprecated. Please use ZSH_HIGHLIGHT_DIRS_BLACKLIST.'
336307
ZSH_HIGHLIGHT_DIRS_BLACKLIST=($X_ZSH_HIGHLIGHT_DIRS_BLACKLIST)
@@ -1227,7 +1198,8 @@ _zsh_highlight_main_highlighter_highlight_argument()
12271198
{
12281199
if (( $+_zsh_highlight_main__arg_cache )); then
12291200
local cache_key=$1$'\0'$2$'\0'$arg$'\0'$last_arg$'\0'$has_end$'\0'$highlight_glob$'\0'$in_redirection$'\0'$zsyh_user_options[multios]
1230-
local -a cache_val=(${(@0)_zsh_highlight_main__arg_cache[$cache_key]})
1201+
local -a cache_val
1202+
cache_val=(${(@0)_zsh_highlight_main__arg_cache[$cache_key]})
12311203
if (( $#cache_val )); then
12321204
integer offset=$(( start_pos - $cache_val[-1] ))
12331205
local start end_ style
@@ -1788,7 +1760,8 @@ else
17881760
fi
17891761
typeset -ga ZSH_HIGHLIGHT_DIRS_BLACKLIST
17901762

1791-
typeset -gA _zsh_highlight_main__precommand_options=(
1763+
typeset -gA _zsh_highlight_main__precommand_options
1764+
_zsh_highlight_main__precommand_options=(
17921765
# Precommand modifiers as of zsh 5.6.2 cf. zshmisc(1).
17931766
'-' ''
17941767
'builtin' ''
@@ -1825,7 +1798,8 @@ typeset -gA _zsh_highlight_main__precommand_options=(
18251798
# flock
18261799
# ssh
18271800

1828-
typeset -ga _zsh_highlight_main__tokens_commandseparator=(
1801+
typeset -ga _zsh_highlight_main__tokens_commandseparator
1802+
_zsh_highlight_main__tokens_commandseparator=(
18291803
'|' '||' ';' '&' '&&'
18301804
$'\n' # ${(z)} returns ';' but we convert it to $'\n'
18311805
'|&'
@@ -1836,7 +1810,8 @@ typeset -ga _zsh_highlight_main__tokens_commandseparator=(
18361810

18371811
# Tokens that, at (naively-determined) "command position", are followed by
18381812
# a de jure command position. All of these are reserved words.
1839-
typeset -ga _zsh_highlight_main__tokens_control_flow=(
1813+
typeset -ga _zsh_highlight_main__tokens_control_flow
1814+
_zsh_highlight_main__tokens_control_flow=(
18401815
$'\x7b' # block
18411816
$'\x28' # subshell
18421817
'()' # anonymous function
@@ -1852,7 +1827,8 @@ typeset -ga _zsh_highlight_main__tokens_control_flow=(
18521827
'!' # reserved word; unrelated to $histchars[1]
18531828
)
18541829

1855-
typeset -gA _zsh_highlight_main__fallback_of=(
1830+
typeset -gA _zsh_highlight_main__fallback_of
1831+
_zsh_highlight_main__fallback_of=(
18561832
alias arg0
18571833
suffix-alias arg0
18581834
global-alias dollar-double-quoted-argument

zsh-syntax-highlighting.zsh

+3-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ _zsh_highlight()
204204
# copying all options.
205205
zsyh_user_options=(
206206
ignorebraces "${options[ignorebraces]}"
207-
ignoreclosebraces "${options[ignoreclosebraces]}"
207+
ignoreclosebraces "${options[ignoreclosebraces]-off}"
208208
pathdirs "${options[pathdirs]}"
209209
interactivecomments "${options[interactivecomments]}"
210210
globassign "${options[globassign]}"
@@ -237,7 +237,8 @@ _zsh_highlight()
237237
new_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
238238
fi
239239

240-
emulate -L zsh -o warncreateglobal -o nobashrematch
240+
emulate -L zsh
241+
setopt warncreateglobal nobashrematch
241242
local REPLY # don't leak $REPLY into global scope
242243

243244
{

0 commit comments

Comments
 (0)