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