#compdef tt
compdef _tt tt

# zsh completion for tt                                   -*- shell-script -*-

__tt_debug()
{
    local file="$BASH_COMP_DEBUG_FILE"
    if [[ -n ${file} ]]; then
        echo "$*" >> "${file}"
    fi
}

_tt()
{
    local shellCompDirectiveError=1
    local shellCompDirectiveNoSpace=2
    local shellCompDirectiveNoFileComp=4
    local shellCompDirectiveFilterFileExt=8
    local shellCompDirectiveFilterDirs=16
    local shellCompDirectiveKeepOrder=32

    local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
    local -a completions

    __tt_debug "\n========= starting completion logic =========="
    __tt_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"

    if [[ ${#words[@]} -ge 4 && ${words[2]} = "rocks" && ${words[3]} = "admin" ]]; then
        shift words
        shift words
        (( CURRENT-- ))
        (( CURRENT-- ))
        _luarocks-admin
        return
    fi
    if [[ ${#words[@]} -ge 3 && ${words[2]} = "rocks" ]]; then
        shift words
        (( CURRENT-- ))
        _rocks
        return
    fi
    # The user could have moved the cursor backwards on the command-line.
    # We need to trigger completion from the $CURRENT location, so we need
    # to truncate the command-line ($words) up to the $CURRENT location.
    # (We cannot use $CURSOR as its value does not work when a command is an alias.)
    words=("${=words[1,CURRENT]}")
    __tt_debug "Truncated words[*]: ${words[*]},"

    lastParam=${words[-1]}
    lastChar=${lastParam[-1]}
    __tt_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"

    # For zsh, when completing a flag with an = (e.g., tt -n=<TAB>)
    # completions must be prefixed with the flag
    setopt local_options BASH_REMATCH
    if [[ "${lastParam}" =~ '-.*=' ]]; then
        # We are dealing with a flag with an =
        flagPrefix="-P ${BASH_REMATCH}"
    fi

    # Prepare the command to obtain completions
    requestComp="${words[1]} __complete ${words[2,-1]}"
    if [ "${lastChar}" = "" ]; then
        # If the last parameter is complete (there is a space following it)
        # We add an extra empty parameter so we can indicate this to the go completion code.
        __tt_debug "Adding extra empty parameter"
        requestComp="${requestComp} \"\""
    fi

    __tt_debug "About to call: eval ${requestComp}"

    # Use eval to handle any environment variables and such
    out=$(eval ${requestComp} 2>/dev/null)
    __tt_debug "completion output: ${out}"

    # Extract the directive integer following a : from the last line
    local lastLine
    while IFS='\n' read -r line; do
        lastLine=${line}
    done < <(printf "%s\n" "${out[@]}")
    __tt_debug "last line: ${lastLine}"

    if [ "${lastLine[1]}" = : ]; then
        directive=${lastLine[2,-1]}
        # Remove the directive including the : and the newline
        local suffix
        (( suffix=${#lastLine}+2))
        out=${out[1,-$suffix]}
    else
        # There is no directive specified.  Leave $out as is.
        __tt_debug "No directive found.  Setting do default"
        directive=0
    fi

    __tt_debug "directive: ${directive}"
    __tt_debug "completions: ${out}"
    __tt_debug "flagPrefix: ${flagPrefix}"

    if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
        __tt_debug "Completion received error. Ignoring completions."
        return
    fi

    local activeHelpMarker="_activeHelp_ "
    local endIndex=${#activeHelpMarker}
    local startIndex=$((${#activeHelpMarker}+1))
    local hasActiveHelp=0
    while IFS='\n' read -r comp; do
        # Check if this is an activeHelp statement (i.e., prefixed with $activeHelpMarker)
        if [ "${comp[1,$endIndex]}" = "$activeHelpMarker" ];then
            __tt_debug "ActiveHelp found: $comp"
            comp="${comp[$startIndex,-1]}"
            if [ -n "$comp" ]; then
                compadd -x "${comp}"
                __tt_debug "ActiveHelp will need delimiter"
                hasActiveHelp=1
            fi

            continue
        fi

        if [ -n "$comp" ]; then
            # If requested, completions are returned with a description.
            # The description is preceded by a TAB character.
            # For zsh's _describe, we need to use a : instead of a TAB.
            # We first need to escape any : as part of the completion itself.
            comp=${comp//:/\\:}

            local tab="$(printf '\t')"
            comp=${comp//$tab/:}

            __tt_debug "Adding completion: ${comp}"
            completions+=${comp}
            lastComp=$comp
        fi
    done < <(printf "%s\n" "${out[@]}")

    # Add a delimiter after the activeHelp statements, but only if:
    # - there are completions following the activeHelp statements, or
    # - file completion will be performed (so there will be choices after the activeHelp)
    if [ $hasActiveHelp -eq 1 ]; then
        if [ ${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then
            __tt_debug "Adding activeHelp delimiter"
            compadd -x "--"
            hasActiveHelp=0
        fi
    fi

    if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
        __tt_debug "Activating nospace."
        noSpace="-S ''"
    fi

    if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
        __tt_debug "Activating keep order."
        keepOrder="-V"
    fi

    if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
        # File extension filtering
        local filteringCmd
        filteringCmd='_files'
        for filter in ${completions[@]}; do
            if [ ${filter[1]} != '*' ]; then
                # zsh requires a glob pattern to do file filtering
                filter="\*.$filter"
            fi
            filteringCmd+=" -g $filter"
        done
        filteringCmd+=" ${flagPrefix}"

        __tt_debug "File filtering command: $filteringCmd"
        _arguments '*:filename:'"$filteringCmd"
    elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
        # File completion for directories only
        local subdir
        subdir="${completions[1]}"
        if [ -n "$subdir" ]; then
            __tt_debug "Listing directories in $subdir"
            pushd "${subdir}" >/dev/null 2>&1
        else
            __tt_debug "Listing directories in ."
        fi

        local result
        _arguments '*:dirname:_files -/'" ${flagPrefix}"
        result=$?
        if [ -n "$subdir" ]; then
            popd >/dev/null 2>&1
        fi
        return $result
    else
        __tt_debug "Calling _describe"
        if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then
            __tt_debug "_describe found some completions"

            # Return the success of having called _describe
            return 0
        else
            __tt_debug "_describe did not find completions."
            __tt_debug "Checking if we should do file completion."
            if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
                __tt_debug "deactivating file completion"

                # We must return an error code here to let zsh know that there were no
                # completions found by _describe; this is what will trigger other
                # matching algorithms to attempt to find completions.
                # For example zsh can match letters in the middle of words.
                return 1
            else
                # Perform file completion
                __tt_debug "Activating file completion"

                # We must return the result of this command, so it must be the
                # last command, or else we must store its result to return it.
                _arguments '*:filename:_files'" ${flagPrefix}"
            fi
        fi
    fi
}

# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_tt" ]; then
    _tt
fi
_rocks() {
  local context state state_descr line
  typeset -A opt_args

  local -a options=(
    {-h,--help}"[Show this help message and exit]"
    "--version[Show version info and exit]"
    "--dev[Enable the sub-repositories in rocks servers for rockspecs of in-development versions]"
    {--server,--from}"[Fetch rocks/rockspecs from this server (takes priority over config file)]: :_files"
    {--only-server,--only-from}"[Fetch rocks/rockspecs from this server only (overrides any entries in the config file)]: :_files"
    {--only-sources,--only-sources-from}"[Restrict downloads to paths matching the given URL]: :_files"
    "--namespace[Specify the rocks server namespace to use]: :_files"
    "--lua-dir[Which Lua installation to use]: :_files"
    "--lua-version[Which Lua version to use]: :_files"
    {--tree,--to}"[Which tree to operate on]: :_files"
    "--local[Use the tree in the user's home directory]"
    "--global[Use the system tree when \`local_by_default\` is \`true\`]"
    "--no-project[Do not use project tree even if running from a project folder]"
    "--verbose[Display verbose output of commands executed]"
    "--timeout[Timeout on network operations, in seconds]: :_files"
    "--project-tree: :_files"
    "--pack-binary-rock"
    "--branch: :_files"
    "--sign"
  )
  _arguments -s -S \
    $options \
    ": :_rocks_cmds" \
    "*:: :->args" \
    && return 0

  case $words[1] in
    help)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :(help build config doc download install lint list make make_manifest make-manifest new_version new-version pack purge remove search show test unpack which write_rockspec write-rockspec)" \
        && return 0
      ;;

    build)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        {--only-deps,--deps-only}"[Install only the dependencies of the rock]"
        "--branch[Override the \`source.branch\` field in the loaded rockspec]: :_files"
        "--pin[Create a luarocks.lock file listing the exact versions of each dependency found for this rock (recursively), and store it in the rock's directory]"
        "--no-install[Do not install the rock]"
        "--no-doc[Install the rock without its documentation]"
        "--pack-binary-rock[Do not install rock]"
        "--keep[Do not remove previously installed versions of the rock after building a new one]"
        "--force[If --keep is not specified, force removal of previously installed versions if it would break dependencies]"
        "--force-fast[Like --force, but performs a forced removal without reporting dependency issues]"
        "--verify[Verify signature of the rockspec or src.rock being built]"
        "--sign[To be used with --pack-binary-rock]"
        "--check-lua-versions[If the rock can't be found, check repository and report if it is available for another Lua version]"
        "--pin[Pin the exact dependencies used for the rockspecbeing built into a luarocks.lock file in the current directory]"
        "--no-manifest[Skip creating/updating the manifest]"
        {--only-deps,--deps-only}"[Install only the dependencies of the rock]"
        "--chdir[Specify a source directory of the rock]: :_files"
        "--deps-mode[How to handle dependencies]: :(all one order none)"
        "--nodeps"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    config)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--scope[The scope indicates which config file should be rewritten]: :(system user project)"
        "--unset[Delete the key from the configuration file]"
        "--json[Output as JSON]"
        "--lua-incdir"
        "--lua-libdir"
        "--lua-ver"
        "--system-config"
        "--user-config"
        "--rock-trees"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    doc)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--home[Open the home page of project]"
        "--list[List documentation files only]"
        "--porcelain[Produce machine-friendly output]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    download)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--all[Download all files if there are multiple matches]"
        "--source[Download .src.rock if available]"
        "--rockspec[Download .rockspec if available]"
        "--arch[Download rock for a specific architecture]: :_files"
        "--check-lua-versions[If the rock can't be found, check repository and report if it is available for another Lua version]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    install)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--keep[Do not remove previously installed versions of the rock after building a new one]"
        "--force[If --keep is not specified, force removal of previously installed versions if it would break dependencies]"
        "--force-fast[Like --force, but performs a forced removal without reporting dependency issues]"
        {--only-deps,--deps-only}"[Install only the dependencies of the rock]"
        "--no-doc[Install the rock without its documentation]"
        "--verify[Verify signature of the rockspec or src.rock being built]"
        "--check-lua-versions[If the rock can't be found, check repository and report if it is available for another Lua version]"
        "--deps-mode[How to handle dependencies]: :(all one order none)"
        "--nodeps"
        "--no-manifest[Skip creating/updating the manifest]"
        "--pin[If the installed rock is a Lua module, create a luarocks.lock file listing the exact versions of each dependency found for this rock (recursively), and store it in the rock's directory]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    lint)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        && return 0
      ;;

    list)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--outdated[List only rocks for which there is a higher version available in the rocks server]"
        "--porcelain[Produce machine-friendly output]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    make)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--no-install[Do not install the rock]"
        "--no-doc[Install the rock without its documentation]"
        "--pack-binary-rock[Do not install rock]"
        "--keep[Do not remove previously installed versions of the rock after building a new one]"
        "--force[If --keep is not specified, force removal of previously installed versions if it would break dependencies]"
        "--force-fast[Like --force, but performs a forced removal without reporting dependency issues]"
        "--verify[Verify signature of the rockspec or src.rock being built]"
        "--sign[To be used with --pack-binary-rock]"
        "--check-lua-versions[If the rock can't be found, check repository and report if it is available for another Lua version]"
        "--pin[Pin the exact dependencies used for the rockspecbeing built into a luarocks.lock file in the current directory]"
        "--no-manifest[Skip creating/updating the manifest]"
        {--only-deps,--deps-only}"[Install only the dependencies of the rock]"
        "--chdir[Specify a source directory of the rock]: :_files"
        "--deps-mode[How to handle dependencies]: :(all one order none)"
        "--nodeps"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        && return 0
      ;;

    make_manifest|make-manifest)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--local-tree[If given, do not write versioned versions of the manifest file]"
        "--deps-mode[How to handle dependencies]: :(all one order none)"
        "--nodeps"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        && return 0
      ;;

    new_version|new-version)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--dir[Output directory for the new rockspec]: :_files"
        "--tag[New SCM tag]: :_files"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    pack)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--sign[Produce a signature file as well]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    purge)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--old-versions[Keep the highest-numbered version of each rock and remove the other ones]"
        "--force[If --old-versions is specified, force removal of previously installed versions if it would break dependencies]"
        "--force-fast[Like --force, but performs a forced removal without reporting dependency issues]"
      )
      _arguments -s -S \
        $options \
        && return 0
      ;;

    remove)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--force[Force removal if it would break dependencies]"
        "--force-fast[Perform a forced removal without reporting dependency issues]"
        "--deps-mode[How to handle dependencies]: :(all one order none)"
        "--nodeps"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    search)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--source[Return only rockspecs and source rocks, to be used with the \"build\" command]"
        "--binary[Return only pure Lua and binary rocks (rocks that can be used with the \"install\" command without requiring a C toolchain)]"
        "--all[List all contents of the server that are suitable to this platform, do not filter by name]"
        "--porcelain[Return a machine readable format]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    show)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--home[Show home page of project]"
        "--modules[Show all modules provided by the package as used by require()]"
        "--deps[Show packages the package depends on]"
        "--build-deps[Show build-only dependencies for the package]"
        "--test-deps[Show dependencies for testing the package]"
        "--rockspec[Show the full path of the rockspec file]"
        "--mversion[Show the package version]"
        "--rock-tree[Show local tree where rock is installed]"
        "--rock-namespace[Show rock namespace]"
        "--rock-dir[Show data directory of the installed rock]"
        "--rock-license[Show rock license]"
        "--issues[Show URL for project's issue tracker]"
        "--labels[List the labels of the rock]"
        "--porcelain[Produce machine-friendly output]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    test)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--prepare[Only install dependencies needed for testing only, but do not run the test]"
        "--test-type[Specify the test suite type manually if it was not specified in the rockspec and it could not be auto-detected]: :_files"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        "*: :_files" \
        && return 0
      ;;

    unpack)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--force[Unpack files even if the output directory already exists]"
        "--check-lua-versions[If the rock can't be found, check repository and report if it is available for another Lua version]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

    which)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        && return 0
      ;;

    write_rockspec|write-rockspec)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--output[Write the rockspec with the given filename]: :_files"
        "--license[A license string, such as \"MIT/X11\" or \"GNU GPL v3\"]: :_files"
        "--summary[A short one-line description summary]: :_files"
        "--detailed[A longer description string]: :_files"
        "--homepage[Project homepage]: :_files"
        "--lua-versions[Supported Lua versions]: :(5.1 5.2 5.3 5.4 5.1,5.2 5.2,5.3 5.3,5.4 5.1,5.2,5.3 5.2,5.3,5.4 5.1,5.2,5.3,5.4)"
        "--rockspec-format[Rockspec format version, such as \"1.0\" or \"1.1\"]: :_files"
        "--tag[Tag to use]: :_files"
        "--lib[A comma-separated list of libraries that C files need to link to]: :_files"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        ": :_files" \
        ": :_files" \
        && return 0
      ;;

  esac

  return 1
}

_rocks_cmds() {
  local -a commands=(
    "help:Show help for commands"
    "admin:LuaRocks repository administration interface"
    "build:Build/compile a rock"
    "config:Query information about the LuaRocks configuration"
    "doc:Show documentation for an installed rock"
    "download:Download a specific rock file from a rocks server"
    "install:Install a rock"
    "lint:Check syntax of a rockspec"
    "list:List currently installed rocks"
    "make:Compile package in current directory using a rockspec"
    {make_manifest,make-manifest}":Compile a manifest file for a repository"
    {new_version,new-version}":Auto-write a rockspec for a new version of a rock"
    "pack:Create a rock, packing sources or binaries"
    "purge:Remove all installed rocks from a tree"
    "remove:Uninstall a rock"
    "search:Query the LuaRocks servers"
    "show:Show information about an installed rock"
    "test:Run the test suite in the current directory"
    "unpack:Unpack the contents of a rock"
    "which:Tell which file corresponds to a given module name"
    {write_rockspec,write-rockspec}":Write a template for a rockspec file"
  )
  _describe "command" commands
}

_luarocks-admin() {
  local context state state_descr line
  typeset -A opt_args

  local -a options=(
    {-h,--help}"[Show this help message and exit]"
    "--version[Show version info and exit]"
    "--dev[Enable the sub-repositories in rocks servers for rockspecs of in-development versions]"
    {--server,--from}"[Fetch rocks/rockspecs from this server (takes priority over config file)]: :_files"
    {--only-server,--only-from}"[Fetch rocks/rockspecs from this server only (overrides any entries in the config file)]: :_files"
    {--only-sources,--only-sources-from}"[Restrict downloads to paths matching the given URL]: :_files"
    "--namespace[Specify the rocks server namespace to use]: :_files"
    "--lua-dir[Which Lua installation to use]: :_files"
    "--lua-version[Which Lua version to use]: :_files"
    {--tree,--to}"[Which tree to operate on]: :_files"
    "--local[Use the tree in the user's home directory]"
    "--global[Use the system tree when \`local_by_default\` is \`true\`]"
    "--no-project[Do not use project tree even if running from a project folder]"
    "--verbose[Display verbose output of commands executed]"
    "--timeout[Timeout on network operations, in seconds]: :_files"
    "--project-tree: :_files"
  )
  _arguments -s -S \
    $options \
    ": :_luarocks-admin_cmds" \
    "*:: :->args" \
    && return 0

  case $words[1] in
    help)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :(help add make_manifest make-manifest refresh_cache refresh-cache remove)" \
        && return 0
      ;;

    add)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--server[The server to use]: :_files"
        "--no-refresh[Do not refresh the local cache prior to generation of the updated manifest]"
        "--index[Produce an index.html file for the manifest]"
      )
      _arguments -s -S \
        $options \
        "*: :_files" \
        && return 0
      ;;

    make_manifest|make-manifest)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--local-tree[If given, do not write versioned versions of the manifest file]"
        "--deps-mode[How to handle dependencies]: :(all one order none)"
        "--nodeps"
      )
      _arguments -s -S \
        $options \
        ": :_files" \
        && return 0
      ;;

    refresh_cache|refresh-cache)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--from[The server to use]: :_files"
      )
      _arguments -s -S \
        $options \
        && return 0
      ;;

    remove)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--server[The server to use]: :_files"
        "--no-refresh[Do not refresh the local cache prior to generation of the updated manifest]"
      )
      _arguments -s -S \
        $options \
        "*: :_files" \
        && return 0
      ;;

  esac

  return 1
}

_luarocks-admin_cmds() {
  local -a commands=(
    "help:Show help for commands"
    "add:Add a rock or rockspec to a rocks server"
    {make_manifest,make-manifest}":Compile a manifest file for a repository"
    {refresh_cache,refresh-cache}":Refresh local cache of a remote rocks server"
    "remove:Remove a rock or rockspec from a rocks server"
  )
  _describe "command" commands
}
