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:
parent
0faf67ce9a
commit
30f68ad896
|
|
@ -300,6 +300,37 @@ _zsh_highlight_main__is_apt_subcommand() {
|
||||||
(( ${+_zsh_highlight_main__apt_subcommands[$1]} ))
|
(( ${+_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() {
|
||||||
|
|
@ -1155,6 +1186,8 @@ _zsh_highlight_main_highlighter_highlight_list()
|
||||||
esac
|
esac
|
||||||
if [[ $arg == apt && $res == (command|hashed) ]]; then
|
if [[ $arg == apt && $res == (command|hashed) ]]; then
|
||||||
next_word+=':apt-subcommand:'
|
next_word+=':apt-subcommand:'
|
||||||
|
elif [[ $arg == apt-get && $res == (command|hashed) ]]; then
|
||||||
|
next_word+=':apt-get-subcommand:'
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then
|
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 ))
|
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
|
||||||
continue
|
continue
|
||||||
fi
|
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
|
||||||
|
|
@ -1909,4 +1951,6 @@ else
|
||||||
fi
|
fi
|
||||||
typeset -gA _zsh_highlight_main__apt_subcommands
|
typeset -gA _zsh_highlight_main__apt_subcommands
|
||||||
typeset -gi _zsh_highlight_main__apt_subcommands_loaded=0
|
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
|
||||||
|
|
|
||||||
|
|
@ -28,15 +28,27 @@
|
||||||
# vim: ft=zsh sw=2 ts=2 et
|
# vim: ft=zsh sw=2 ts=2 et
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
function apt-get() {}
|
|
||||||
function apt-cache() {}
|
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=(
|
expected_region_highlight=(
|
||||||
'1 7 function' # apt-get
|
'1 7 command' # apt-get
|
||||||
'9 15 default' # isntall
|
'9 15 command' # install
|
||||||
'16 16 commandseparator' # ;
|
'16 16 commandseparator' # ;
|
||||||
'18 26 function' # apt-cache
|
'18 24 command' # apt-get
|
||||||
'28 34 default' # isntall
|
'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
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue