Improvements and fixes to zsh completion

* Use correct return status so that approximate completion isn't broken
* Follow zsh conventions on description forms
* Some options can be used multiple times, e.g. -H, -r, --file-name
* Set completion context correctly for the cache subcommand
* Better completion for --map-syntax argument
* Add --nonprintable-notation option
* Correct some of the mutual exclusion lists for options
This commit is contained in:
Oliver Kiddle 2023-11-12 14:26:56 +01:00 committed by Martin Nordholts
parent ad3ff26960
commit b43d31b75a
2 changed files with 64 additions and 52 deletions

View File

@ -10,6 +10,7 @@
- Refactor and cleanup build script #2756 (@cyqsimon) - Refactor and cleanup build script #2756 (@cyqsimon)
- Checks changelog has been written to for PRs in CI #2766 (@cyqsimon) - Checks changelog has been written to for PRs in CI #2766 (@cyqsimon)
- Minor benchmark script improvements #2768 (@cyqsimon) - Minor benchmark script improvements #2768 (@cyqsimon)
- Update and improve `zsh` completion, see #2772 (@okapia)
## Syntaxes ## Syntaxes

View File

@ -1,19 +1,20 @@
#compdef {{PROJECT_EXECUTABLE}} #compdef {{PROJECT_EXECUTABLE}}
local context state state_descr line local curcontext="$curcontext" ret=1
local -a state state_descr line
typeset -A opt_args typeset -A opt_args
(( $+functions[_{{PROJECT_EXECUTABLE}}_cache_subcommand] )) || (( $+functions[_{{PROJECT_EXECUTABLE}}_cache_subcommand] )) ||
_{{PROJECT_EXECUTABLE}}_cache_subcommand() { _{{PROJECT_EXECUTABLE}}_cache_subcommand() {
local -a args local -a args
args=( args=(
'(-b --build -c --clear)'{-b,--build}'[Initialize or update the syntax/theme cache]' '(-b --build -c --clear)'{-b,--build}'[initialize or update the syntax/theme cache]'
'(-b --build -c --clear)'{-c,--clear}'[Remove the cached syntax definitions and themes]' '(-b --build -c --clear)'{-c,--clear}'[remove the cached syntax definitions and themes]'
'(--source)'--source='[Use a different directory to load syntaxes and themes from]:directory:_files -/' --source='[specify directory to load syntaxes and themes from]:directory:_files -/'
'(--target)'--target='[Use a different directory to store the cached syntax and theme set]:directory:_files -/' --target='[specify directory to store the cached syntax and theme set in]:directory:_files -/'
'(--blank)'--blank'[Create completely new syntax and theme sets]' --blank'[create completely new syntax and theme sets]'
'(: -)'{-h,--help}'[Prints help information]' --acknowledgements'[build acknowledgements.bin]'
'*: :' '(: -)'{-h,--help}'[show help information]'
) )
_arguments -S -s $args _arguments -S -s $args
@ -23,69 +24,79 @@ _{{PROJECT_EXECUTABLE}}_cache_subcommand() {
_{{PROJECT_EXECUTABLE}}_main() { _{{PROJECT_EXECUTABLE}}_main() {
local -a args local -a args
args=( args=(
'(-A --show-all)'{-A,--show-all}'[Show non-printable characters (space, tab, newline, ..)]' '(-A --show-all)'{-A,--show-all}'[show non-printable characters (space, tab, newline, ..)]'
'*'{-p,--plain}'[Show plain style (alias for `--style=plain`), repeat twice to disable disable automatic paging (alias for `--paging=never`)]' --nonprintable-notation='[specify how to display non-printable characters when using --show-all]:notation:(caret unicode)'
'(-l --language)'{-l+,--language=}'[Set the language for syntax highlighting]:<language>:->language' \*{-p,--plain}'[show plain style (alias for `--style=plain`), repeat twice to disable disable automatic paging (alias for `--paging=never`)]'
'(-H --highlight-line)'{-H,--highlight-line}'[Highlight lines N through M]:<N\:M>...' '(-l --language)'{-l+,--language=}'[set the language for syntax highlighting]:language:->languages'
'(--file-name)'--file-name'[Specify the name to display for a file]:<name>...:_files' \*{-H+,--highlight-line=}'[highlight specified block of lines]:start\:end'
'(-d --diff)'--diff'[Only show lines that have been added/removed/modified]' \*--file-name='[specify the name to display for a file]:name:_files'
'(--diff-context)'--diff-context'[Include N lines of context around added/removed/modified lines when using `--diff`]:<N> (lines):()' '(-d --diff)'--diff'[only show lines that have been added/removed/modified]'
'(--tabs)'--tabs'[Set the tab width to T spaces]:<T> (tab width):()' --diff-context='[specify lines of context around added/removed/modified lines when using `--diff`]:lines'
'(--wrap)'--wrap='[Specify the text-wrapping mode]:<when>:(auto never character)' --tabs='[set the tab width]:tab width [4]'
'(--terminal-width)'--terminal-width'[Explicitly set the width of the terminal instead of determining it automatically]:<width>' --wrap='[specify the text-wrapping mode]:mode [auto]:(auto never character)'
'(-n --number)'{-n,--number}'[Show line numbers]' '!(--wrap)'{-S,--chop-long-lines}
'(--color)'--color='[When to use colors]:<when>:(auto never always)' --terminal-width='[explicitly set the width of the terminal instead of determining it automatically]:width'
'(--italic-text)'--italic-text='[Use italics in output]:<when>:(always never)' '(-n --number --diff --diff-context)'{-n,--number}'[show line numbers]'
'(--decorations)'--decorations='[When to show the decorations]:<when>:(auto never always)' --color='[specify when to use colors]:when:(auto never always)'
'(--paging)'--paging='[Specify when to use the pager]:<when>:(auto never always)' --italic-text='[use italics in output]:when:(always never)'
'(-m --map-syntax)'{-m+,--map-syntax=}'[Use the specified syntax for files matching the glob pattern]:<glob\:syntax>...' --decorations='[specify when to show the decorations]:when:(auto never always)'
'(--theme)'--theme='[Set the color theme for syntax highlighting]:<theme>:->theme' --paging='[specify when to use the pager]:when:(auto never always)'
'(: --list-themes --list-languages -L)'--list-themes'[Display all supported highlighting themes]' '(-m --map-syntax)'{-m+,--map-syntax=}'[map a glob pattern to an existing syntax name]: :->syntax-maps'
'(--style)'--style='[Comma-separated list of style elements to display]:<components>:->style' '(--theme)'--theme='[set the color theme for syntax highlighting]:theme:->themes'
'(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:<N\:M>...' '(: --list-themes --list-languages -L)'--list-themes'[show all supported highlighting themes]'
'(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]' --style='[comma-separated list of style elements to display]: : _values "style [default]"
'(: --no-config)'--no-config'[Do not use the configuration file]' default auto full plain changes header header-filename header-filesize grid rule numbers snip'
'(: --no-custom-assets)'--no-custom-assets'[Do not load custom assets]' \*{-r+,--line-range=}'[only print the specified line range]:start\:end'
'(: --lessopen)'--lessopen'[Enable the $LESSOPEN preprocessor]' '(* -)'{-L,--list-languages}'[display all supported languages]'
'(: --no-lessopen)'--no-lessopen'[Disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)]' "--no-config[don't use the configuration file]"
'(: --config-dir)'--config-dir'[Show bat'"'"'s configuration directory]' "--no-custom-assets[don't load custom assets]"
'(: --config-file)'--config-file'[Show path to the configuration file]' '(--no-lessopen)'--lessopen'[enable the $LESSOPEN preprocessor]'
'(: --generate-config-file)'--generate-config-file'[Generates a default configuration file]' '(--lessopen)'--no-lessopen'[disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)]'
'(: --cache-dir)'--cache-dir'[Show bat'"'"'s cache directory]' '(* -)'--config-dir"[show bat's configuration directory]"
'(: -)'{-h,--help}'[Print this help message]' '(* -)'--config-file'[show path to the configuration file]'
'(: -)'{-V,--version}'[Show version information]' '(* -)'--generate-config-file'[generate a default configuration file]'
'*: :_files' '(* -)'--cache-dir"[show bat's cache directory]"
'(* -)'{-h,--help}'[show help information]'
'(* -)'{-V,--version}'[show version information]'
'*: :{ _files || compadd cache }'
) )
_arguments -S -s $args _arguments -S -s $args && ret=0
case "$state" in case "$state" in
language) syntax-maps)
if ! compset -P '*:'; then
_message -e patterns 'glob pattern:language'
return
fi
;& # fall-through
languages)
local IFS=$'\n' local IFS=$'\n'
local -a languages local -a languages
languages=( $({{PROJECT_EXECUTABLE}} --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) languages=( $({{PROJECT_EXECUTABLE}} --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') )
_describe 'language' languages _describe 'language' languages && ret=0
;; ;;
theme) themes)
local IFS=$'\n' local -a themes expl
local -a themes themes=( ${(f)"$(_call_program themes {{PROJECT_EXECUTABLE}} --list-themes)"} )
themes=( $({{PROJECT_EXECUTABLE}} --list-themes | sort) )
_values 'theme' $themes _wanted themes expl 'theme' compadd -a themes && ret=0
;;
style)
_values -s , 'style' default auto full plain changes header header-filename header-filesize grid rule numbers snip
;; ;;
esac esac
return ret
} }
case $words[2] in case $words[2] in
cache) cache)
## Completion of the 'cache' command itself is removed for better UX ## Completion of the 'cache' command itself is removed for better UX
## See https://github.com/sharkdp/bat/issues/2085#issuecomment-1271646802 ## See https://github.com/sharkdp/bat/issues/2085#issuecomment-1271646802
shift words
(( CURRENT-- ))
curcontext="${curcontext%:*}-${words[1]}:"
_{{PROJECT_EXECUTABLE}}_cache_subcommand _{{PROJECT_EXECUTABLE}}_cache_subcommand
;; ;;