This commit is contained in:
Daniel Shahaf 2020-03-19 01:49:17 -04:00 committed by GitHub
commit 244889c146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 21 deletions

View File

@ -57,6 +57,9 @@
- Highlight global aliases - Highlight global aliases
[#700] [#700]
- Fix highlighting of `\local`, `\typeset`, etc (escaped)
[#701]
# Changes in version 0.7.1 # Changes in version 0.7.1
- Remove out-of-date information from the 0.7.0 changelog. - Remove out-of-date information from the 0.7.0 changelog.

View File

@ -153,19 +153,17 @@ _zsh_highlight_main_calculate_fallback() {
# #
# If $2 is 0, do not consider aliases. # If $2 is 0, do not consider aliases.
# #
# If $3 is 0, do not consider reserved words.
#
# The result will be stored in REPLY. # The result will be stored in REPLY.
_zsh_highlight_main__type() { _zsh_highlight_main__type() {
integer -r aliases_allowed=${2-1} integer -r aliases_allowed=${2-1}
# We won't cache replies of anything that exists as an alias at all, to integer -r resword_allowed=${3-1}
# ensure the cached value is correct regardless of $aliases_allowed. readonly cache_key="${1}:$(( aliases_allowed ? 1 : 0)):$((resword_allowed ? 1 : 0))"
#
# ### 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
# Cache lookup # Cache lookup
if (( $+_zsh_highlight_main__command_type_cache )); then 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 if [[ -n "$REPLY" ]]; then
return return
fi fi
@ -177,16 +175,13 @@ _zsh_highlight_main__type() {
fi fi
unset REPLY unset REPLY
if zmodload -e zsh/parameter; then if zmodload -e zsh/parameter; then
if (( $+aliases[(e)$1] )); then if (( aliases_allowed )) && (( ${+galiases[(e)$1]} )); then
may_cache=0
fi
if (( ${+galiases[(e)$1]} )) && (( aliases_allowed )); then
REPLY='global alias' REPLY='global alias'
elif (( $+aliases[(e)$1] )) && (( aliases_allowed )); then elif (( aliases_allowed )) && (( $+aliases[(e)$1] )) &&; then
REPLY=alias REPLY=alias
elif [[ $1 == *.* && -n ${1%.*} ]] && (( $+saliases[(e)${1##*.}] )); then elif [[ $1 == *.* && -n ${1%.*} ]] && (( $+saliases[(e)${1##*.}] )); then
REPLY='suffix alias' REPLY='suffix alias'
elif (( $reswords[(Ie)$1] )); then elif (( resword_allowed )) && (( $reswords[(Ie)$1] )); then
REPLY=reserved REPLY=reserved
elif (( $+functions[(e)$1] )); then elif (( $+functions[(e)$1] )); then
REPLY=function REPLY=function
@ -223,14 +218,11 @@ _zsh_highlight_main__type() {
# «$(…)»], which is area that has had some parsing bugs before 5.6 # «$(…)»], which is area that has had some parsing bugs before 5.6
# (approximately). # (approximately).
REPLY="${$(:; (( aliases_allowed )) || unalias -- "$1" 2>/dev/null; LC_ALL=C builtin type -w -- "$1" 2>/dev/null)##*: }" 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 fi
# Cache population # Cache population
if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then if (( $+_zsh_highlight_main__command_type_cache )); then
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY _zsh_highlight_main__command_type_cache[(e)$cache_key]=$REPLY
fi fi
[[ -n $REPLY ]] [[ -n $REPLY ]]
return $? return $?
@ -616,7 +608,7 @@ _zsh_highlight_main_highlighter_highlight_list()
if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then
# Expand aliases. # Expand aliases.
# An alias is ineligible for expansion while it's being expanded (see #652/#653). # An alias is ineligible for expansion while it's being expanded (see #652/#653).
_zsh_highlight_main__type "$arg" "$(( ! ${+seen_alias[$arg]} ))" _zsh_highlight_main__type "$arg" "$(( ! ${+seen_alias[$arg]} ))" 1
local res="$REPLY" local res="$REPLY"
if [[ $res == "alias" ]]; then if [[ $res == "alias" ]]; then
# Mark insane aliases as unknown-token (cf. #263). # Mark insane aliases as unknown-token (cf. #263).
@ -648,10 +640,15 @@ _zsh_highlight_main_highlighter_highlight_list()
continue continue
else else
_zsh_highlight_main_highlighter_expand_path $arg _zsh_highlight_main_highlighter_expand_path $arg
_zsh_highlight_main__type "$REPLY" 0 if [[ $arg != $REPLY ]]; then
# TODO: Is this right? Shouldn't we simply check whether it's
# executable? It's not just aliases and reserved words that aren't
# considered here; builtins should likewise be excluded.
_zsh_highlight_main__type "$REPLY" 0 0
res="$REPLY" res="$REPLY"
fi fi
fi fi
fi
# Analyse the current word. # Analyse the current word.
if _zsh_highlight_main__is_redirection $arg ; then if _zsh_highlight_main__is_redirection $arg ; then

View File

@ -0,0 +1,37 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
BUFFER=$'\\local a=( * )'
expected_region_highlight=(
'1 6 builtin' # \\local
'8 14 default' # a=( * )
'12 12 globbing' # *
)