From c27470a08da6b133679c091e699070f53b3b8c7c Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 17 Mar 2020 02:17:11 +0000 Subject: [PATCH] 'main': Let aliases be type-cached, and don't run type-checking twice (with slightly different settings) if path expansion returned the input unchanged. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.) --- highlighters/main/main-highlighter.zsh | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index fc12366..7333631 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -159,16 +159,11 @@ _zsh_highlight_main_calculate_fallback() { _zsh_highlight_main__type() { integer -r aliases_allowed=${2-1} integer -r resword_allowed=${3-1} - # We won't cache replies of anything that exists as an alias at all, to - # ensure the cached value is correct regardless of $aliases_allowed. - # - # ### We probably _should_ cache them in a cache that's keyed on the value of - # ### $aliases_allowed, on the assumption that aliases are the common case. - integer may_cache=1 + readonly cache_key="${1}:$(( aliases_allowed ? 1 : 0)):$((resword_allowed ? 1 : 0))" # Cache lookup if (( $+_zsh_highlight_main__command_type_cache )); then - REPLY=$_zsh_highlight_main__command_type_cache[(e)$1] + REPLY=$_zsh_highlight_main__command_type_cache[(e)$cache_key] if [[ -n "$REPLY" ]]; then return fi @@ -180,9 +175,6 @@ _zsh_highlight_main__type() { fi unset REPLY if zmodload -e zsh/parameter; then - if (( $+aliases[(e)$1] )); then - may_cache=0 - fi if (( aliases_allowed )) && (( ${+galiases[(e)$1]} )); then REPLY='global alias' elif (( aliases_allowed )) && (( $+aliases[(e)$1] )) &&; then @@ -226,14 +218,11 @@ _zsh_highlight_main__type() { # «$(…)»], which is area that has had some parsing bugs before 5.6 # (approximately). REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }" - if [[ $REPLY == 'alias' ]]; then - may_cache=0 - fi fi # Cache population - if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then - _zsh_highlight_main__command_type_cache[(e)$1]=$REPLY + if (( $+_zsh_highlight_main__command_type_cache )); then + _zsh_highlight_main__command_type_cache[(e)$cache_key]=$REPLY fi [[ -n $REPLY ]] return $? @@ -651,8 +640,10 @@ _zsh_highlight_main_highlighter_highlight_list() continue else _zsh_highlight_main_highlighter_expand_path $arg - _zsh_highlight_main__type "$REPLY" 0 0 - res="$REPLY" + if [[ $arg != $REPLY ]]; then + _zsh_highlight_main__type "$REPLY" 0 0 + res="$REPLY" + fi fi fi