Apply formatting and refactor ghc-pkg completion

This commit is contained in:
Shohei YOSHIDA 2025-07-23 16:29:51 +09:00
parent 10147cfb99
commit c16614f37e
No known key found for this signature in database
GPG Key ID: C9A1BB11BB940CF2
1 changed files with 529 additions and 515 deletions

164
src/_ghc
View File

@ -46,7 +46,9 @@ local WORDS
# ghci
#
_ghc_ghci () { _ghc_compiler }
_ghc_ghci() {
_ghc_compiler
}
#
# ghc
@ -180,7 +182,6 @@ _ghc_compiler ()
esac
}
_ghc_include_directory()
{
compset -P '*:'
@ -386,7 +387,6 @@ _ghc_debugging_options ()
_describe -t flags 'ghc' _ghc_debugging_options_list || compadd "$@"
}
_ghc_language_flags ()
{
local _ghc_language_flags_list
@ -479,93 +479,105 @@ _ghc_language_flags ()
# ghc-pkg
#
_ghc_pkg ()
{
WORDS=()
for w in $words[1,(($CURRENT - 1))]; do
if [[ $w != --* ]]; then WORDS+=$w; fi
done
_arguments '*:command:_ghc_pkg_command'
_ghc_pkg() {
typeset -A opt_args
local context state line
local curcontext="$curcontext"
local ret=1
_arguments -C \
'(- *)'{-h,--help}'[Print this usage information]' \
'1: :_ghc_pkg_subcommands' \
'*:: :->arg' \
&& ret=0
case $state in
(arg)
local -a options=(
"--user[use current user's package database]"
'--global[user the global package database]'
{-f,--package-db=}'[use the specified package database]:database:_files'
'--global-package-db[location of the global package database]:dir:_files -/'
'(--no-user-package-db --user-package-db)--no-user-package-db[never read the user package database]'
'(--no-user-package-db --user-package-db)--user-package-db=[location of the user package database]:database:_files'
'--force[ignore missing dependencies, directories and libraries]'
'--force-files[ignore missing directories and libraries only]'
'--enable-multi-instance[allow registering multiple instances of the same package version]'
'--expand-env-vars[expand environment variables in input package descriptions]'
'(--expand-pkgroot --no-expand-pkgroot)--expand-pkgroot[expand pkgroot-relative paths in output package descriptions]'
'(--expand-pkgroot --no-expand-pkgroot)--no-expand-pkgroot[preserve pkgroot-relative paths in output package descriptions]'
'(- *)'{-?,--help}'[display this help and exit]'
'(- *)'{-V,--version}'[output version information and exit]'
'--simple-output[print output in easy-to-parse format for some commands]'
'--show-unit-ids[print unit-ids instead of package identifiers]'
'--names-only[only print package names, not versions]'
'--ignore-case[ignore case for substring matching]'
{--ipid,--unit-id}'[interpret package arguments as unit IDs]'
{-V,--verbose=-}'[verbosity level]:level:(0 1 2)'
)
case $words[1] in
(init|register|update)
options+=('*: :_files')
;;
(unregister|expose|hide|trust|distrust|list|latest|describe)
options+=('*: :_ghc_pkg_list_packages')
;;
(field)
options+=('*: :_ghc_pkg_field')
;;
(find-module)
options+=('*: :_ghc_pkg_find-module')
;;
esac
_arguments $options && ret=0
;;
esac
return ret
}
_ghc_pkg_command()
{
local -a _ghc_pkg_cmds
_ghc_pkg_cmds=(
_ghc_pkg_subcommands() {
local -a subcommands=(
"init:Create and initialize a package database at the given location"
"register:Register the package using package description"
"update:Register the package (overwriting existing package)"
"unregister:Unregister the specified package"
"expose:Expose the specified package"
"hide:Hide the specified package"
"trust:Trust the specified package"
"list:List registered packages"
"dot:Generate a graph of the package dependencies in a form suitable"
"find-module:List registered packages exposing module"
"latest:Prints the highest registered version of a package"
"check:Check the consistency of package dependencies and list broken packages"
"describe:Give the registered description for the specified package"
"field:Extract the specified field of the package description"
"dump:Dump the registered description for every package"
"recache:Regenerate the package database cache"
)
if (( $#WORDS == 1 )); then
_describe -t commands 'command' _ghc_pkg_cmds || compadd "$@"
else
local curcontext="$curcontext"
cmd="${${_ghc_pkg_cmds[(r)$WORDS[2]:*]%%:*}}"
if (( $#cmd )); then
_arguments \
"--user[Use current user's package database]" \
'--global[User the global package database]' \
{-f,--package-conf=}'[Use the specified package config file]:Package config file:_files' \
'--no-user-package-conf[Never reader the user package config]' \
{-V,--version}'[Output version information and exit]' \
'--force[Ignore missing directories and libraries only]' \
{-g,--auto-ghci-libs}'[Automatically build libs for GHCi]' \
{-?,--help}'[Display this help and exit]' \
'--simple-output[Print output in easy-to-parse format for some commands]' \
'--names-only[Only print package names, not versions]' \
'--ignore-case[Ignore case for substring matching]' \
'*:argument:_ghc_pkg_'$cmd
else
_message "unknown ghc-pkg command: $WORDS[2]"
fi
fi
_describe -t subcommands 'subcommand' subcommands "$@"
}
_ghc_pkg_unregister () { _ghc_pkg_list_packages }
_ghc_pkg_expose () { _ghc_pkg_list_packages }
_ghc_pkg_hide () { _ghc_pkg_list_packages }
_ghc_pkg_latest () { _ghc_pkg_list_packages }
_ghc_pkg_describe () { _ghc_pkg_list_packages }
_ghc_pkg_field ()
{
_ghc_pkg_field() {
_ghc_pkg_available_packages
if (( $#WORDS == 2 )); then
compadd "$@" -a -- _ghc_pkg_packages
elif (( $#WORDS == 3 )); then
compset -P '*,'
compset -S ',*'
compadd "$@" -S ',' -q -- \
name version license copyright maintainer \
stability homepage package-url description \
category author exposed exposed-modules \
hidden-modules import-dirs hs-libraries \
extra-libraries extra-ghci-libraries include-dirs \
includes depends hugs-options cc-options ld-options \
framework-dirs frameworks haddock-interfaces \
haddock-html
fi
_arguments \
'1:package:_values $_ghc_pkg_packages' \
'2:field:_ghc_pkg_field_fields'
}
_ghc_pkg_register () { _files }
_ghc_pkg_field_fields() {
local -a fields=(
name version visibility id key license copyright
maintainer author homepage synopsis description
category abi exposed exposed-modules hidden-modules
import-dirs library-dirs library-dirs-static dynamic-library-dirs
data-dir hs-libraries depends haddoc-interfaces haddoc-html pkgroot)
_ghc_pkg_update () { _files }
_ghc_pkg_list () { _ghc_pkg_list_packages }
_values -s ',' $fields
}
_ghc_pkg_find-module()
{
@ -580,10 +592,6 @@ _ghc_pkg_find-module ()
fi
}
_ghc_pkg_dump () {}
_ghc_pkg_check () {}
_ghc_pkg_available_packages()
{
if ( [[ ${+_ghc_pkg_packages_pkgs} -eq 0 ]] || _cache_invalid GHC_PACKAGES ) &&
@ -600,11 +608,9 @@ _ghc_pkg_list_packages ()
compadd "$@" -a -- _ghc_pkg_packages
}
#
# dispatcher
#
case $service in
ghc)
_ghc_compiler
@ -616,3 +622,11 @@ case $service in
_ghc_pkg
;;
esac
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et