瀏覽代碼

main: Highlight the ! precommand as an error when not at the start of a pipeline.

Fixes #511.
pull/566/head
Daniel Shahaf 6 年之前
父節點
當前提交
298ef6a2fa
共有 2 個文件被更改,包括 55 次插入5 次删除
  1. +16
    -5
      highlighters/main/main-highlighter.zsh
  2. +39
    -0
      highlighters/main/test-data/bang-pipeline.zsh

+ 16
- 5
highlighters/main/main-highlighter.zsh 查看文件

@ -398,6 +398,8 @@ _zsh_highlight_main_highlighter_highlight_list()
#
# The states are:
# - :start: Command word
# - :start_of_pipeline: Start of a 'pipeline' as defined in zshmisc(1).
# Only valid when :start: is present
# - :sudo_opt: A leading-dash option to a precommand, whether it takes an
# argument or not. (Example: sudo's "-u" or "-i".)
# - :sudo_arg: The argument to a precommand's leading-dash option,
@ -431,7 +433,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 next_word=':start:'
local this_word next_word=':start::start_of_pipeline:'
integer in_redirection
# Processing buffer
local proc_buf="$buf"
@ -667,11 +669,14 @@ _zsh_highlight_main_highlighter_highlight_list()
else
next_word=':start:'
highlight_glob=true
if [[ $arg != '|' && $arg != '|&' ]]; then
next_word+=':start_of_pipeline:'
fi
fi
elif ! (( in_redirection)) && [[ $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:' # only left brace is allowed, apparently
elif ! (( in_redirection)) && [[ $this_word == *':start:'* ]]; then # $arg is the command word
if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then
style=precommand
@ -684,6 +689,9 @@ _zsh_highlight_main_highlighter_highlight_list()
case $res in
reserved) # reserved word
style=reserved-word
if [[ $arg == '!' && $this_word != *':start_of_pipeline:'* ]]; then
style=unknown-token
fi
#
# Match braces.
case $arg in
@ -764,6 +772,9 @@ _zsh_highlight_main_highlighter_highlight_list()
else
# assignment to a scalar parameter.
# (For array assignments, the command doesn't start until the ")" token.)
#
# Discard :start_of_pipeline:, if present, as '!' is not valid
# after assignments.
next_word+=':start:'
if (( start_pos + i <= end_pos )); then
() {
@ -822,7 +833,7 @@ _zsh_highlight_main_highlighter_highlight_list()
esac
fi
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
next_word=':start:'
next_word=':start::start_of_pipeline:'
fi
else # $arg is a non-command word
case $arg in
@ -830,7 +841,7 @@ _zsh_highlight_main_highlighter_highlight_list()
if $in_array_assignment; then
style=assign
in_array_assignment=false
next_word+=':start:'
next_word+=':start::start_of_pipeline:'
elif (( in_redirection )); then
style=unknown-token
else
@ -847,7 +858,7 @@ _zsh_highlight_main_highlighter_highlight_list()
else
if [[ $zsyh_user_options[multifuncdef] == on ]] || false # TODO: or if the previous word was a command word
then
next_word+=':start:'
next_word+=':start::start_of_pipeline:'
fi
style=reserved-word
fi

+ 39
- 0
highlighters/main/test-data/bang-pipeline.zsh 查看文件

@ -0,0 +1,39 @@
#!/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
# -------------------------------------------------------------------------------------------------
BUFFER=$'\! ls | \! ls'
expected_region_highlight=(
'1 1 reserved-word' # \!
'3 4 command' # ls
'6 6 commandseparator' # |
'8 8 unknown-token' # \!
'10 11 command' # ls
)

Loading…
取消
儲存