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

176
src/_ghc
View File

@ -46,13 +46,15 @@ local WORDS
# ghci # ghci
# #
_ghc_ghci () { _ghc_compiler } _ghc_ghci() {
_ghc_compiler
}
# #
# ghc # ghc
# #
_ghc_compiler () _ghc_compiler()
{ {
_arguments \ _arguments \
"-no-hs-main[Don't assume this program contains main]" \ "-no-hs-main[Don't assume this program contains main]" \
@ -180,15 +182,14 @@ _ghc_compiler ()
esac esac
} }
_ghc_include_directory()
_ghc_include_directory ()
{ {
compset -P '*:' compset -P '*:'
compset -S ':*' compset -S ':*'
_path_files -r': ' -/ _path_files -r': ' -/
} }
_ghc_compiler_flags () _ghc_compiler_flags()
{ {
local _ghc_compiler_flags_list local _ghc_compiler_flags_list
_ghc_compiler_flags_list=( _ghc_compiler_flags_list=(
@ -386,7 +387,6 @@ _ghc_debugging_options ()
_describe -t flags 'ghc' _ghc_debugging_options_list || compadd "$@" _describe -t flags 'ghc' _ghc_debugging_options_list || compadd "$@"
} }
_ghc_language_flags () _ghc_language_flags ()
{ {
local _ghc_language_flags_list local _ghc_language_flags_list
@ -479,95 +479,107 @@ _ghc_language_flags ()
# ghc-pkg # ghc-pkg
# #
_ghc_pkg () _ghc_pkg() {
{ typeset -A opt_args
WORDS=() local context state line
for w in $words[1,(($CURRENT - 1))]; do
if [[ $w != --* ]]; then WORDS+=$w; fi local curcontext="$curcontext"
done local ret=1
_arguments '*:command:_ghc_pkg_command'
_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() _ghc_pkg_subcommands() {
{ local -a subcommands=(
local -a _ghc_pkg_cmds "init:Create and initialize a package database at the given location"
_ghc_pkg_cmds=(
"register:Register the package using package description" "register:Register the package using package description"
"update:Register the package (overwriting existing package)" "update:Register the package (overwriting existing package)"
"unregister:Unregister the specified package" "unregister:Unregister the specified package"
"expose:Expose the specified package" "expose:Expose the specified package"
"hide:Hide the specified package" "hide:Hide the specified package"
"trust:Trust the specified package"
"list:List registered packages" "list:List registered packages"
"dot:Generate a graph of the package dependencies in a form suitable"
"find-module:List registered packages exposing module" "find-module:List registered packages exposing module"
"latest:Prints the highest registered version of a package" "latest:Prints the highest registered version of a package"
"check:Check the consistency of package dependencies and list broken packages" "check:Check the consistency of package dependencies and list broken packages"
"describe:Give the registered description for the specified package" "describe:Give the registered description for the specified package"
"field:Extract the specified field of the package description" "field:Extract the specified field of the package description"
"dump:Dump the registered description for every package" "dump:Dump the registered description for every package"
"recache:Regenerate the package database cache"
) )
_describe -t subcommands 'subcommand' subcommands "$@"
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
} }
_ghc_pkg_unregister () { _ghc_pkg_list_packages } _ghc_pkg_field() {
_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_available_packages _ghc_pkg_available_packages
if (( $#WORDS == 2 )); then
compadd "$@" -a -- _ghc_pkg_packages _arguments \
elif (( $#WORDS == 3 )); then '1:package:_values $_ghc_pkg_packages' \
compset -P '*,' '2:field:_ghc_pkg_field_fields'
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
} }
_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 } _values -s ',' $fields
}
_ghc_pkg_list () { _ghc_pkg_list_packages } _ghc_pkg_find-module()
_ghc_pkg_find-module ()
{ {
if (( $#WORDS == 2)); then if (( $#WORDS == 2)); then
if ( [[ ${+_ghc_modules} -eq 0 ]] || _cache_invalid GHC_MODULES ) && if ( [[ ${+_ghc_modules} -eq 0 ]] || _cache_invalid GHC_MODULES ) &&
@ -580,11 +592,7 @@ _ghc_pkg_find-module ()
fi fi
} }
_ghc_pkg_dump () {} _ghc_pkg_available_packages()
_ghc_pkg_check () {}
_ghc_pkg_available_packages ()
{ {
if ( [[ ${+_ghc_pkg_packages_pkgs} -eq 0 ]] || _cache_invalid GHC_PACKAGES ) && if ( [[ ${+_ghc_pkg_packages_pkgs} -eq 0 ]] || _cache_invalid GHC_PACKAGES ) &&
! _retrieve_cache GHC_PACKAGES; ! _retrieve_cache GHC_PACKAGES;
@ -594,17 +602,15 @@ _ghc_pkg_available_packages ()
fi fi
} }
_ghc_pkg_list_packages () _ghc_pkg_list_packages()
{ {
_ghc_pkg_available_packages _ghc_pkg_available_packages
compadd "$@" -a -- _ghc_pkg_packages compadd "$@" -a -- _ghc_pkg_packages
} }
# #
# dispatcher # dispatcher
# #
case $service in case $service in
ghc) ghc)
_ghc_compiler _ghc_compiler
@ -616,3 +622,11 @@ case $service in
_ghc_pkg _ghc_pkg
;; ;;
esac 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