Skip to content

Commit d5b6bb2

Browse files
committed
Perf: speed up _zsh_highlight
1 parent 2172930 commit d5b6bb2

File tree

1 file changed

+64
-47
lines changed

1 file changed

+64
-47
lines changed

zsh-syntax-highlighting.zsh

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,48 @@ _zsh_highlight()
173173
region_highlight[-1]=()
174174
}
175175

176-
# Reset region_highlight to build it from scratch
177-
if (( zsh_highlight__memo_feature )); then
178-
region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
179-
else
180-
# Legacy codepath. Not very interoperable with other plugins (issue #418).
181-
region_highlight=()
182-
fi
183-
184176
# Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
185177
# For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
186178
# This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
187179
# and doesn't have the pattern matching bug
188-
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) }; then
180+
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) } ||
181+
# Do not highlight if there are more than 300 chars in the buffer. It's most
182+
# likely a pasted command or a huge list of files in that case..
183+
[[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && (( ${#BUFFER} > ZSH_HIGHLIGHT_MAXLENGTH )) ||
184+
# Do not highlight if there are pending inputs (copy/paste).
185+
(( PENDING )); then
186+
# Reset region_highlight to build it from scratch
187+
if (( zsh_highlight__memo_feature )); then
188+
region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
189+
else
190+
# Legacy codepath. Not very interoperable with other plugins (issue #418).
191+
region_highlight=()
192+
fi
189193
return $ret
190194
fi
191195

192196
# Before we 'emulate -L', save the user's options
193197
local -A zsyh_user_options
194198
if zmodload -e zsh/parameter; then
195-
zsyh_user_options=("${(kv)options[@]}")
199+
if [[ -n ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#(brackets|cursor|line|main|pattern|regexp|root)} ]]; then
200+
# Copy all options if there are user-defined highlighters
201+
zsyh_user_options=("${(kv)options[@]}")
202+
else
203+
# Copy a subset of options used by the bundled highlighters. This is faster than
204+
# copying all options.
205+
zsyh_user_options=(
206+
ignorebraces "${options[ignorebraces]}"
207+
ignoreclosebraces "${options[ignoreclosebraces]}"
208+
pathdirs "${options[pathdirs]}"
209+
interactivecomments "${options[interactivecomments]}"
210+
globassign "${options[globassign]}"
211+
multifuncdef "${options[multifuncdef]}"
212+
autocd "${options[autocd]}"
213+
equals "${options[equals]}"
214+
multios "${options[multios]}"
215+
rcquotes "${options[rcquotes]}"
216+
)
217+
fi
196218
else
197219
local canonical_options onoff option raw_options
198220
raw_options=(${(f)"$(emulate -R zsh; set -o)"})
@@ -210,20 +232,16 @@ _zsh_highlight()
210232
fi
211233
typeset -r zsyh_user_options
212234

213-
emulate -L zsh
214-
setopt localoptions warncreateglobal nobashrematch
215-
local REPLY # don't leak $REPLY into global scope
216-
217-
# Do not highlight if there are more than 300 chars in the buffer. It's most
218-
# likely a pasted command or a huge list of files in that case..
219-
[[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return $ret
235+
local -a new_highlight
236+
if (( zsh_highlight__memo_feature )); then
237+
new_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
238+
fi
220239

221-
# Do not highlight if there are pending inputs (copy/paste).
222-
[[ $PENDING -gt 0 ]] && return $ret
240+
emulate -L zsh -o warncreateglobal -o nobashrematch
241+
local REPLY # don't leak $REPLY into global scope
223242

224243
{
225-
local cache_place
226-
local -a region_highlight_copy
244+
local cache_place pred
227245

228246
# Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
229247
local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do
@@ -233,38 +251,37 @@ _zsh_highlight()
233251
typeset -ga ${cache_place}
234252

235253
# If highlighter needs to be invoked
236-
if ! type "_zsh_highlight_highlighter_${highlighter}_predicate" >&/dev/null; then
237-
echo "zsh-syntax-highlighting: warning: disabling the ${(qq)highlighter} highlighter as it has not been loaded" >&2
238-
# TODO: use ${(b)} rather than ${(q)} if supported
239-
ZSH_HIGHLIGHT_HIGHLIGHTERS=( ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#${highlighter}} )
240-
elif "_zsh_highlight_highlighter_${highlighter}_predicate"; then
241-
242-
# save a copy, and cleanup region_highlight
243-
region_highlight_copy=("${region_highlight[@]}")
244-
region_highlight=()
254+
pred="_zsh_highlight_highlighter_${highlighter}_predicate"
255+
if (( ! $pred )); then
256+
if type $pred >&/dev/null; then
257+
typeset -gri $pred=1
258+
else
259+
echo "zsh-syntax-highlighting: warning: disabling the ${(qq)highlighter} highlighter as it has not been loaded" >&2
260+
# TODO: use ${(b)} rather than ${(q)} if supported
261+
ZSH_HIGHLIGHT_HIGHLIGHTERS=( ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#${highlighter}} )
262+
continue
263+
fi
264+
fi
245265

266+
if $pred; then
246267
# Execute highlighter and save result
247-
{
248-
"_zsh_highlight_highlighter_${highlighter}_paint"
249-
} always {
250-
: ${(AP)cache_place::="${region_highlight[@]}"}
251-
}
252-
253-
# Restore saved region_highlight
254-
region_highlight=("${region_highlight_copy[@]}")
255-
268+
region_highlight=()
269+
"_zsh_highlight_highlighter_${highlighter}_paint"
270+
: ${(AP)cache_place::="${region_highlight[@]}"}
271+
new_highlight+=($region_highlight)
272+
else
273+
# Use value form cache if any cached
274+
new_highlight+=("${(@P)cache_place}")
256275
fi
257276

258-
# Use value form cache if any cached
259-
region_highlight+=("${(@P)cache_place}")
260-
261277
done
262278

279+
region_highlight=($new_highlight)
280+
263281
# Re-apply zle_highlight settings
264282

265283
# region
266-
() {
267-
(( REGION_ACTIVE )) || return
284+
(( REGION_ACTIVE )) && () {
268285
integer min max
269286
if (( MARK > CURSOR )) ; then
270287
min=$CURSOR max=$MARK
@@ -284,13 +301,13 @@ _zsh_highlight()
284301
}
285302

286303
# yank / paste (zsh-5.1.1 and newer)
287-
(( $+YANK_ACTIVE )) && (( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END"
304+
(( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END"
288305

289306
# isearch
290-
(( $+ISEARCHMATCH_ACTIVE )) && (( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END"
307+
(( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END"
291308

292309
# suffix
293-
(( $+SUFFIX_ACTIVE )) && (( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END"
310+
(( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END"
294311

295312

296313
return $ret

0 commit comments

Comments
 (0)