feat(main): highlight apt-get subcommands

Add apt-get subcommand detection alongside apt handling, including cached
lookup from apt-get help output and fallback built-in subcommands.

Update tests to verify valid apt-get subcommands are highlighted as commands
and invalid ones as unknown tokens.feat(main): highlight apt-get subcommands

Add apt-get subcommand detection alongside apt handling, including cached
lookup from apt-get help output and fallback built-in subcommands.

Update tests to verify valid apt-get subcommands are highlighted as commands
and invalid ones as unknown tokens.
This commit is contained in:
oliver 2026-06-06 22:22:28 +08:00
parent 0faf67ce9a
commit 30f68ad896
No known key found for this signature in database
GPG Key ID: CCA57C9E86D7E3F8
2 changed files with 62 additions and 6 deletions

View File

@ -300,6 +300,37 @@ _zsh_highlight_main__is_apt_subcommand() {
(( ${+_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.
# Report result via the exit code.
_zsh_highlight_main__is_redirection() {
@ -1155,6 +1186,8 @@ _zsh_highlight_main_highlighter_highlight_list()
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
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
@ -1224,6 +1257,15 @@ _zsh_highlight_main_highlighter_highlight_list()
_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
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
continue
@ -1909,4 +1951,6 @@ else
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

View File

@ -28,15 +28,27 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
function apt-get() {}
function apt-cache() {}
hash apt-get=/bin/true
BUFFER='apt-get isntall; apt-cache isntall'
_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 function' # apt-get
'9 15 default' # isntall
'1 7 command' # apt-get
'9 15 command' # install
'16 16 commandseparator' # ;
'18 26 function' # apt-cache
'28 34 default' # isntall
'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
)