From 0faf67ce9a8c643ac05b657ee44aa7ddf0cf2c7c Mon Sep 17 00:00:00 2001 From: oliver Date: Sat, 6 Jun 2026 22:18:09 +0800 Subject: [PATCH 1/2] 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. --- highlighters/main/main-highlighter.zsh | 65 +++++++++++++++++++ .../main/test-data/apt-invalid-subcommand.zsh | 39 +++++++++++ .../main/test-data/apt-other-commands.zsh | 42 ++++++++++++ .../main/test-data/apt-valid-subcommand.zsh | 61 +++++++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 highlighters/main/test-data/apt-invalid-subcommand.zsh create mode 100644 highlighters/main/test-data/apt-other-commands.zsh create mode 100644 highlighters/main/test-data/apt-valid-subcommand.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index e6f5e35..974a0eb 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -249,6 +249,57 @@ _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 the first argument is a redirection operator token. # Report result via the exit code. _zsh_highlight_main__is_redirection() { @@ -1102,6 +1153,9 @@ _zsh_highlight_main_highlighter_highlight_list() continue ;; esac + if [[ $arg == apt && $res == (command|hashed) ]]; then + next_word+=':apt-subcommand:' + fi fi if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} ]]; then next_word=':start::start_of_pipeline:' @@ -1161,6 +1215,15 @@ _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 else _zsh_highlight_main_highlighter_highlight_argument 1 $(( 1 != in_redirection )) continue @@ -1844,4 +1907,6 @@ 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_DIRS_BLACKLIST diff --git a/highlighters/main/test-data/apt-invalid-subcommand.zsh b/highlighters/main/test-data/apt-invalid-subcommand.zsh new file mode 100644 index 0000000..1da8d4d --- /dev/null +++ b/highlighters/main/test-data/apt-invalid-subcommand.zsh @@ -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 +) diff --git a/highlighters/main/test-data/apt-other-commands.zsh b/highlighters/main/test-data/apt-other-commands.zsh new file mode 100644 index 0000000..a3f93dd --- /dev/null +++ b/highlighters/main/test-data/apt-other-commands.zsh @@ -0,0 +1,42 @@ +#!/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-get() {} +function apt-cache() {} + +BUFFER='apt-get isntall; apt-cache isntall' + +expected_region_highlight=( + '1 7 function' # apt-get + '9 15 default' # isntall + '16 16 commandseparator' # ; + '18 26 function' # apt-cache + '28 34 default' # isntall +) diff --git a/highlighters/main/test-data/apt-valid-subcommand.zsh b/highlighters/main/test-data/apt-valid-subcommand.zsh new file mode 100644 index 0000000..a82196e --- /dev/null +++ b/highlighters/main/test-data/apt-valid-subcommand.zsh @@ -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 +) From 30f68ad896bfe19a2829c249fb4ac1f528043076 Mon Sep 17 00:00:00 2001 From: oliver Date: Sat, 6 Jun 2026 22:22:28 +0800 Subject: [PATCH 2/2] 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. --- highlighters/main/main-highlighter.zsh | 44 +++++++++++++++++++ .../main/test-data/apt-other-commands.zsh | 24 +++++++--- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 974a0eb..784e301 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -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 diff --git a/highlighters/main/test-data/apt-other-commands.zsh b/highlighters/main/test-data/apt-other-commands.zsh index a3f93dd..7774499 100644 --- a/highlighters/main/test-data/apt-other-commands.zsh +++ b/highlighters/main/test-data/apt-other-commands.zsh @@ -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 )