# cabal command line completion
# Copyright 2007-2008 "Lennart Kolmodin" <kolmodin@gentoo.org>
#                     "Duncan Coutts"     <dcoutts@gentoo.org>
#

# List cabal targets by type, pass:
#   - test-suite for test suites
#   - benchmark for benchmarks
#   - executable for executables
#   - executable|test-suite|benchmark for the three
_cabal_list()
{
	cat *.cabal |
	grep -Ei "^[[:space:]]*($1)[[:space:]]" |
	sed -e "s/.* \([^ ]*\).*/\1/"
}

# List possible targets depending on the command supplied as parameter.  The
# ideal option would be to implement this via --list-options on cabal directly.
# This is a temporary workaround.
_cabal_targets()
{
	# If command ($*) contains build, repl, test or bench completes with
	# targets of according type.
	[ -f *.cabal ] || return 0
	local comp
	for comp in $*; do
		[ $comp == build ] && _cabal_list "executable|test-suite|benchmark" && break
		[ $comp == repl  ] && _cabal_list "executable|test-suite|benchmark" && break
		[ $comp == run   ] && _cabal_list "executable"                      && break
		[ $comp == test  ] && _cabal_list            "test-suite"           && break
		[ $comp == bench ] && _cabal_list                       "benchmark" && break
	done
}

# List possible subcommands of a cabal subcommand.
#
# In example "sandbox" is a cabal subcommand that itself has subcommands. Since
# "cabal --list-options" doesn't work in such cases we have to get the list
# using other means.
_cabal_subcommands()
{
    local word
    for word in "$@"; do
        case "$word" in
          sandbox)
            # Get list of "cabal sandbox" subcommands from its help message.
            #
            # Following command short-circuits if it reaches flags section.
            # This is to prevent any problems that might arise from unfortunate
            # word combinations in flag descriptions. Usage section is parsed
            # using simple regexp and "sandbox" subcommand is printed for each
            # successful substitution.
            "$1" help sandbox |
            sed -rn '/Flags/q;s/^.* cabal sandbox  *([^ ]*).*/\1/;t p;b;: p;p'
            break  # Terminate for loop.
            ;;
        esac
    done
}

_cabal()
{
    # get the word currently being completed
    local cur
    cur=${COMP_WORDS[$COMP_CWORD]}

    # create a command line to run
    local cmd
    # copy all words the user has entered
    cmd=( ${COMP_WORDS[@]} )

    # replace the current word with --list-options
    cmd[${COMP_CWORD}]="--list-options"

    # the resulting completions should be put into this array
    COMPREPLY=( $( compgen -W "$( ${cmd[@]} ) $( _cabal_targets ${cmd[@]} ) $( _cabal_subcommands ${COMP_WORDS[@]} )" -- $cur ) )
}

complete -F _cabal -o default cabal
