mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2026-06-16 19:52:58 +08:00
Merge 30f68ad896 into 1d85c69261
This commit is contained in:
commit
c4f16cfbb8
@ -249,6 +249,88 @@ _zsh_highlight_main__is_runnable() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Check whether $1 is an Apt subcommand.
|
||||||
|
#
|
||||||
|
# Return 0 if it is, 1 if it is not, and 2 if the subcommand list could not be
|
||||||
|
# obtained. The list is loaded once, on first use.
|
||||||
|
_zsh_highlight_main__is_apt_subcommand() {
|
||||||
|
if (( ! _zsh_highlight_main__apt_subcommands_loaded )); then
|
||||||
|
local line
|
||||||
|
local -a words
|
||||||
|
local output
|
||||||
|
|
||||||
|
# `apt help` only lists the most commonly used commands, so start with
|
||||||
|
# established commands that it omits and merge the current version's list.
|
||||||
|
_zsh_highlight_main__apt_subcommands=(
|
||||||
|
build-dep 1
|
||||||
|
changelog 1
|
||||||
|
check 1
|
||||||
|
clean 1
|
||||||
|
autoclean 1
|
||||||
|
depends 1
|
||||||
|
dist-upgrade 1
|
||||||
|
download 1
|
||||||
|
dump 1
|
||||||
|
dumpavail 1
|
||||||
|
help 1
|
||||||
|
indextargets 1
|
||||||
|
moo 1
|
||||||
|
pkgnames 1
|
||||||
|
policy 1
|
||||||
|
purge 1
|
||||||
|
rdepends 1
|
||||||
|
showpkg 1
|
||||||
|
showsrc 1
|
||||||
|
source 1
|
||||||
|
stats 1
|
||||||
|
unmet 1
|
||||||
|
)
|
||||||
|
output="$(LC_ALL=C command apt help 2>/dev/null)" || return 2
|
||||||
|
|
||||||
|
for line in ${(f)output}; do
|
||||||
|
if [[ $line == ' '*' - '* ]]; then
|
||||||
|
words=(${=line})
|
||||||
|
(( $#words )) && _zsh_highlight_main__apt_subcommands[$words[1]]=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
_zsh_highlight_main__apt_subcommands_loaded=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
(( ${+_zsh_highlight_main__apt_subcommands[$1]} ))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check whether $1 is an apt-get subcommand.
|
||||||
|
#
|
||||||
|
# Return 0 if it is, 1 if it is not, and 2 if the subcommand list could not be
|
||||||
|
# obtained. The list is loaded once, on first use.
|
||||||
|
_zsh_highlight_main__is_apt_get_subcommand() {
|
||||||
|
if (( ! _zsh_highlight_main__apt_get_subcommands_loaded )); then
|
||||||
|
local line
|
||||||
|
local -a words
|
||||||
|
local output
|
||||||
|
|
||||||
|
_zsh_highlight_main__apt_get_subcommands=(
|
||||||
|
help 1
|
||||||
|
indextargets 1
|
||||||
|
markauto 1
|
||||||
|
unmarkauto 1
|
||||||
|
)
|
||||||
|
output="$(LC_ALL=C command apt-get help 2>/dev/null)" || return 2
|
||||||
|
|
||||||
|
for line in ${(f)output}; do
|
||||||
|
if [[ $line == ' '*' - '* ]]; then
|
||||||
|
words=(${=line})
|
||||||
|
(( $#words )) && _zsh_highlight_main__apt_get_subcommands[$words[1]]=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
_zsh_highlight_main__apt_get_subcommands_loaded=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
(( ${+_zsh_highlight_main__apt_get_subcommands[$1]} ))
|
||||||
|
}
|
||||||
|
|
||||||
# Check whether the first argument is a redirection operator token.
|
# Check whether the first argument is a redirection operator token.
|
||||||
# Report result via the exit code.
|
# Report result via the exit code.
|
||||||
_zsh_highlight_main__is_redirection() {
|
_zsh_highlight_main__is_redirection() {
|
||||||
@ -1102,6 +1184,11 @@ _zsh_highlight_main_highlighter_highlight_list()
|
|||||||
continue
|
continue
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
if [[ $arg == apt && $res == (command|hashed) ]]; then
|
||||||
|
next_word+=':apt-subcommand:'
|
||||||
|
elif [[ $arg == apt-get && $res == (command|hashed) ]]; then
|
||||||
|
next_word+=':apt-get-subcommand:'
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
|
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
|
||||||
next_word=':start::start_of_pipeline:'
|
next_word=':start::start_of_pipeline:'
|
||||||
@ -1161,6 +1248,24 @@ _zsh_highlight_main_highlighter_highlight_list()
|
|||||||
:
|
:
|
||||||
elif [[ $arg == $'\x5d' ]] && _zsh_highlight_main__stack_pop 'Q' builtin; then
|
elif [[ $arg == $'\x5d' ]] && _zsh_highlight_main__stack_pop 'Q' builtin; then
|
||||||
:
|
:
|
||||||
|
elif [[ $this_word == *':apt-subcommand:'* ]]; then
|
||||||
|
if _zsh_highlight_main__is_apt_subcommand ${(Q)arg}; then
|
||||||
|
style=command
|
||||||
|
elif (( $? == 1 )); then
|
||||||
|
style=unknown-token
|
||||||
|
else
|
||||||
|
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
elif [[ $this_word == *':apt-get-subcommand:'* ]]; then
|
||||||
|
if _zsh_highlight_main__is_apt_get_subcommand ${(Q)arg}; then
|
||||||
|
style=command
|
||||||
|
elif (( $? == 1 )); then
|
||||||
|
style=unknown-token
|
||||||
|
else
|
||||||
|
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
|
||||||
|
continue
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
|
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
|
||||||
continue
|
continue
|
||||||
@ -1844,4 +1949,8 @@ else
|
|||||||
# Make sure the cache is unset
|
# Make sure the cache is unset
|
||||||
unset _zsh_highlight_main__command_type_cache
|
unset _zsh_highlight_main__command_type_cache
|
||||||
fi
|
fi
|
||||||
|
typeset -gA _zsh_highlight_main__apt_subcommands
|
||||||
|
typeset -gi _zsh_highlight_main__apt_subcommands_loaded=0
|
||||||
|
typeset -gA _zsh_highlight_main__apt_get_subcommands
|
||||||
|
typeset -gi _zsh_highlight_main__apt_get_subcommands_loaded=0
|
||||||
typeset -ga ZSH_HIGHLIGHT_DIRS_BLACKLIST
|
typeset -ga ZSH_HIGHLIGHT_DIRS_BLACKLIST
|
||||||
|
|||||||
39
highlighters/main/test-data/apt-invalid-subcommand.zsh
Normal file
39
highlighters/main/test-data/apt-invalid-subcommand.zsh
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
# Copyright (c) 2026 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
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
_zsh_highlight_main__apt_subcommands=(install 1)
|
||||||
|
_zsh_highlight_main__apt_subcommands_loaded=1
|
||||||
|
|
||||||
|
BUFFER='apt isntall'
|
||||||
|
|
||||||
|
expected_region_highlight=(
|
||||||
|
'1 3 command' # apt
|
||||||
|
'5 11 unknown-token' # isntall
|
||||||
|
)
|
||||||
54
highlighters/main/test-data/apt-other-commands.zsh
Normal file
54
highlighters/main/test-data/apt-other-commands.zsh
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
# Copyright (c) 2026 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
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function apt-cache() {}
|
||||||
|
hash apt-get=/bin/true
|
||||||
|
|
||||||
|
_zsh_highlight_main__apt_get_subcommands=(
|
||||||
|
install 1
|
||||||
|
dselect-upgrade 1
|
||||||
|
)
|
||||||
|
_zsh_highlight_main__apt_get_subcommands_loaded=1
|
||||||
|
|
||||||
|
BUFFER='apt-get install; apt-get dselect-upgrade; apt-get isntall; apt-cache isntall'
|
||||||
|
|
||||||
|
expected_region_highlight=(
|
||||||
|
'1 7 command' # apt-get
|
||||||
|
'9 15 command' # install
|
||||||
|
'16 16 commandseparator' # ;
|
||||||
|
'18 24 command' # apt-get
|
||||||
|
'26 40 command' # dselect-upgrade
|
||||||
|
'41 41 commandseparator' # ;
|
||||||
|
'43 49 command' # apt-get
|
||||||
|
'51 57 unknown-token' # isntall
|
||||||
|
'58 58 commandseparator' # ;
|
||||||
|
'60 68 function' # apt-cache
|
||||||
|
'70 76 default' # isntall
|
||||||
|
)
|
||||||
61
highlighters/main/test-data/apt-valid-subcommand.zsh
Normal file
61
highlighters/main/test-data/apt-valid-subcommand.zsh
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
# Copyright (c) 2026 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
|
||||||
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
_zsh_highlight_main__apt_subcommands=(
|
||||||
|
list 1
|
||||||
|
install 1
|
||||||
|
update 1
|
||||||
|
full-upgrade 1
|
||||||
|
download 1
|
||||||
|
policy 1
|
||||||
|
)
|
||||||
|
_zsh_highlight_main__apt_subcommands_loaded=1
|
||||||
|
|
||||||
|
BUFFER='apt list; apt install; apt update; apt full-upgrade; apt download; apt policy'
|
||||||
|
|
||||||
|
expected_region_highlight=(
|
||||||
|
'1 3 command' # apt
|
||||||
|
'5 8 command' # list
|
||||||
|
'9 9 commandseparator' # ;
|
||||||
|
'11 13 command' # apt
|
||||||
|
'15 21 command' # install
|
||||||
|
'22 22 commandseparator' # ;
|
||||||
|
'24 26 command' # apt
|
||||||
|
'28 33 command' # update
|
||||||
|
'34 34 commandseparator' # ;
|
||||||
|
'36 38 command' # apt
|
||||||
|
'40 51 command' # full-upgrade
|
||||||
|
'52 52 commandseparator' # ;
|
||||||
|
'54 56 command' # apt
|
||||||
|
'58 65 command' # download
|
||||||
|
'66 66 commandseparator' # ;
|
||||||
|
'68 70 command' # apt
|
||||||
|
'72 77 command' # policy
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue
Block a user