Compare commits

...

4 Commits

Author SHA1 Message Date
Oliver Lin d2c31b0430
Merge 30f68ad896 into 2fc57d6306 2026-08-22 09:20:15 +07:00
Manish Tiwari 2fc57d6306 tests: fix relative links in tests/README.md
test-highlighting.zsh and test-perfs.zsh were linked as tests/test-*.zsh,
but this file already lives in tests/, so the links resolved to
tests/tests/test-*.zsh and 404'd. Found by pointing a markdown
link-checker at the repo.
2026-08-21 21:19:05 -05:00
oliver 30f68ad896
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.
2026-06-06 22:22:28 +08:00
oliver 0faf67ce9a
feat(main): highlight invalid apt subcommands distinctly
Detect apt subcommands after runnable apt invocations and mark known
subcommands as commands while styling unknown ones as unknown tokens.

Load apt subcommands lazily from apt help with a built-in fallback list so
highlighting works across apt versions. Add tests for valid and invalid apt
subcommand handling.feat(main): highlight invalid apt subcommands distinctly

Detect apt subcommands after runnable apt invocations and mark known
subcommands as commands while styling unknown ones as unknown tokens.

Load apt subcommands lazily from apt help with a built-in fallback list so
highlighting works across apt versions. Add tests for valid and invalid apt
subcommand handling.
2026-06-06 22:18:09 +08:00
5 changed files with 265 additions and 2 deletions

View File

@ -249,6 +249,88 @@ _zsh_highlight_main__is_runnable() {
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.
# Report result via the exit code.
_zsh_highlight_main__is_redirection() {
@ -1103,6 +1185,11 @@ _zsh_highlight_main_highlighter_highlight_list()
continue
;;
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
next_word=':start::start_of_pipeline:'
@ -1162,6 +1249,24 @@ _zsh_highlight_main_highlighter_highlight_list()
:
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
_zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection ))
continue
@ -1845,4 +1950,8 @@ else
# Make sure the cache is unset
unset _zsh_highlight_main__command_type_cache
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

@ -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
)

View 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
)

View 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
)

View File

@ -86,7 +86,7 @@ grow ways to set `$PREBUFFER` to inject free-form code into the generated file.
Highlighting test
-----------------
[`test-highlighting.zsh`](tests/test-highlighting.zsh) tests the correctness of
[`test-highlighting.zsh`](test-highlighting.zsh) tests the correctness of
the highlighting. Usage:
```zsh
@ -110,7 +110,7 @@ results (tests that failed but were expected to succeed, or vice-versa), run
Performance test
----------------
[`test-perfs.zsh`](tests/test-perfs.zsh) measures the time spent doing the
[`test-perfs.zsh`](test-perfs.zsh) measures the time spent doing the
highlighting. Usage:
```zsh