mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2025-04-17 11:35:32 +08:00
Merge 4c5d44d5e4
into cf88b63523
This commit is contained in:
commit
1ed57d0e85
@ -127,20 +127,43 @@ _zsh_highlight_main_calculate_fallback() {
|
||||
#
|
||||
# Takes a single argument.
|
||||
#
|
||||
# Uses the following caller variables: [[ $this_word == *':alias:'* ]]
|
||||
#
|
||||
# The result will be stored in REPLY.
|
||||
_zsh_highlight_main__type() {
|
||||
# Our caller knows whether aliases are allowed at this point. Compare:
|
||||
# % ls
|
||||
# % sudo ls
|
||||
if [[ $this_word == *':alias:'* ]]; then
|
||||
integer -r aliases_allowed=1
|
||||
else
|
||||
integer -r aliases_allowed=0
|
||||
fi
|
||||
# For the same reason, we won't cache replies of anything that exists as an
|
||||
# alias at all, 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
|
||||
|
||||
# Cache lookup
|
||||
if (( $+_zsh_highlight_main__command_type_cache )); then
|
||||
REPLY=$_zsh_highlight_main__command_type_cache[(e)$1]
|
||||
if [[ -n "$REPLY" ]]; then
|
||||
return
|
||||
fi
|
||||
fi
|
||||
|
||||
# Main logic
|
||||
if (( $#options_to_set )); then
|
||||
setopt localoptions $options_to_set;
|
||||
fi
|
||||
unset REPLY
|
||||
if zmodload -e zsh/parameter; then
|
||||
if (( $+aliases[(e)$1] )); then
|
||||
may_cache=0
|
||||
fi
|
||||
if (( $+aliases[(e)$1] )) && (( aliases_allowed )); then
|
||||
REPLY=alias
|
||||
elif (( $+saliases[(e)${1##*.}] )); then
|
||||
REPLY='suffix alias'
|
||||
@ -165,9 +188,21 @@ _zsh_highlight_main__type() {
|
||||
fi
|
||||
if ! (( $+REPLY )); then
|
||||
# Note that 'type -w' will run 'rehash' implicitly.
|
||||
REPLY="${$(LC_ALL=C builtin type -w -- $1 2>/dev/null)##*: }"
|
||||
#
|
||||
# We 'unalias' in a subshell, so the parent shell is not affected.
|
||||
#
|
||||
# The colon command is there just to avoid a command substitution that
|
||||
# starts with an arithmetic expression [«((…))» as the first thing inside
|
||||
# «$(…)»], 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
|
||||
if (( $+_zsh_highlight_main__command_type_cache )); then
|
||||
|
||||
# Cache population
|
||||
if (( may_cache )) && (( $+_zsh_highlight_main__command_type_cache )); then
|
||||
_zsh_highlight_main__command_type_cache[(e)$1]=$REPLY
|
||||
fi
|
||||
}
|
||||
@ -319,6 +354,7 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
#
|
||||
# The states are:
|
||||
# - :start: Command word
|
||||
# - :alias: :start: and alias expansion is allowed
|
||||
# - :sudo_opt: A leading-dash option to sudo (such as "-u" or "-i")
|
||||
# - :sudo_arg: The argument to a sudo leading-dash option that takes one,
|
||||
# when given as a separate word; i.e., "foo" in "-u foo" (two
|
||||
@ -351,7 +387,7 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
# $in_redirection. The value of $next_word from the iteration that processed
|
||||
# the operator is discarded.
|
||||
#
|
||||
local this_word=':start:' next_word
|
||||
local this_word=':start::alias:' next_word
|
||||
integer in_redirection
|
||||
# Processing buffer
|
||||
local proc_buf="$buf"
|
||||
@ -386,7 +422,7 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
# the string's color.
|
||||
integer already_added=0
|
||||
style=unknown-token
|
||||
if [[ $this_word == *':start:'* ]]; then
|
||||
if [[ $this_word == *':alias:'* ]]; then
|
||||
in_array_assignment=false
|
||||
if [[ $arg == 'noglob' ]]; then
|
||||
highlight_glob=false
|
||||
@ -485,9 +521,11 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
case "$arg" in
|
||||
# Flag that requires an argument
|
||||
'-'[Cgprtu]) this_word=${this_word//:start:/};
|
||||
this_word=${this_word//:alias:/};
|
||||
next_word=':sudo_arg:';;
|
||||
# This prevents misbehavior with sudo -u -otherargument
|
||||
'-'*) this_word=${this_word//:start:/};
|
||||
this_word=${this_word//:alias:/};
|
||||
next_word+=':start:';
|
||||
next_word+=':sudo_opt:';;
|
||||
*) ;;
|
||||
@ -502,18 +540,22 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
if [[ $this_word == *':always:'* && $arg == 'always' ]]; then
|
||||
# try-always construct
|
||||
style=reserved-word # de facto a reserved word, although not de jure
|
||||
next_word=':start:'
|
||||
next_word=':start:' # :alias: will be added to $next_word when $this_word is \x7b.
|
||||
elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word
|
||||
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
|
||||
style=precommand
|
||||
elif [[ "$arg" = "sudo" ]] && { _zsh_highlight_main__type sudo; [[ -n $REPLY && $REPLY != "none" ]] }; then
|
||||
style=precommand
|
||||
next_word=${next_word//:regular:/}
|
||||
next_word=${next_word//:alias:/}
|
||||
next_word+=':sudo_opt:'
|
||||
next_word+=':start:'
|
||||
else
|
||||
_zsh_highlight_main_highlighter_expand_path $arg
|
||||
local expanded_arg="$REPLY"
|
||||
if [[ $arg != $expanded_arg ]]; then
|
||||
this_word=${this_word//:alias:/}
|
||||
fi
|
||||
_zsh_highlight_main__type ${expanded_arg}
|
||||
local res="$REPLY"
|
||||
() {
|
||||
@ -541,6 +583,7 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
case $arg in
|
||||
($'\x7b')
|
||||
braces_stack='Y'"$braces_stack"
|
||||
next_word+=':alias:'
|
||||
;;
|
||||
($'\x7d')
|
||||
# We're at command word, so no need to check $right_brace_is_recognised_everywhere
|
||||
@ -627,6 +670,7 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
# assignment to a scalar parameter.
|
||||
# (For array assignments, the command doesn't start until the ")" token.)
|
||||
next_word+=':start:'
|
||||
next_word+=':alias:'
|
||||
if (( start_pos + i <= end_pos )); then
|
||||
() {
|
||||
local highlight_glob=false
|
||||
@ -704,18 +748,21 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
style=assign
|
||||
in_array_assignment=false
|
||||
next_word+=':start:'
|
||||
next_word+=':alias:'
|
||||
else
|
||||
if _zsh_highlight_main__stack_pop 'S'; then
|
||||
REPLY=$start_pos
|
||||
reply=($list_highlights)
|
||||
return 0
|
||||
fi
|
||||
# TODO: next_word can only be a command separator now
|
||||
_zsh_highlight_main__stack_pop 'R' reserved-word
|
||||
fi;;
|
||||
$'\x28\x29') # possibly a function definition
|
||||
if [[ $zsyh_user_options[multifuncdef] == on ]] || false # TODO: or if the previous word was a command word
|
||||
then
|
||||
next_word+=':start:'
|
||||
next_word+=':alias:'
|
||||
fi
|
||||
style=reserved-word
|
||||
;;
|
||||
@ -755,12 +802,18 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
next_word=':regular:'
|
||||
else
|
||||
next_word=':start:'
|
||||
next_word+=':alias:'
|
||||
highlight_glob=true
|
||||
fi
|
||||
elif
|
||||
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] ||
|
||||
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then
|
||||
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]]; then
|
||||
next_word=':start:'
|
||||
next_word+=':alias:'
|
||||
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then
|
||||
next_word=':start:'
|
||||
# Special-case two reserved words that _can_ be followed by aliases
|
||||
if [[ $arg == ('noglob'|'nocorrect') ]]; then
|
||||
next_word+=':alias:'
|
||||
fi
|
||||
elif [[ $arg == "repeat" && $this_word == *':start:'* ]]; then
|
||||
# skip the repeat-count word
|
||||
in_redirection=2
|
||||
@ -771,7 +824,7 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||
# or a command separator (`repeat 2; ls` or `repeat 2; do ls; done`).
|
||||
#
|
||||
# The repeat-count word will be handled like a redirection target.
|
||||
this_word=':start::regular:'
|
||||
this_word=':start::alias::regular:'
|
||||
fi
|
||||
start_pos=$end_pos
|
||||
if (( in_redirection == 0 )); then
|
||||
|
42
highlighters/main/test-data/alias-after-precommand.zsh
Normal file
42
highlighters/main/test-data/alias-after-precommand.zsh
Normal file
@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env zsh
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
# Copyright (c) 2018 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
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
|
||||
alias ls='ls -l'
|
||||
BUFFER=$'ls; sudo ls; noglob ls'
|
||||
|
||||
expected_region_highlight=(
|
||||
'1 2 alias' # ls
|
||||
'3 3 commandseparator' # ;
|
||||
'5 8 precommand' # sudo
|
||||
'10 11 command' # ls
|
||||
'12 12 commandseparator' # ;
|
||||
'14 19 precommand' # noglob
|
||||
'21 22 alias' # ls
|
||||
)
|
38
highlighters/main/test-data/alias-escaped-command.zsh
Normal file
38
highlighters/main/test-data/alias-escaped-command.zsh
Normal file
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env zsh
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
# Copyright (c) 2018 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
|
||||
# -------------------------------------------------------------------------------------------------
|
||||
|
||||
alias ls='ls -l'
|
||||
BUFFER=$'ls; \\ls'
|
||||
|
||||
expected_region_highlight=(
|
||||
'1 2 alias' # ls
|
||||
'3 3 commandseparator' # ;
|
||||
'5 7 command' # \\ls
|
||||
)
|
Loading…
Reference in New Issue
Block a user