Repository: spf13/cobra
File: fish_completions.go (Go)

Code before:
            set results $results[1..-2]
        else
            # Found non-empty line, we have our proper output
            break
        end
    end

    set -l comps $results[1..-2]
    set -l directiveLine $results[-1]

    # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
    # completions must be prefixed with the flag
    set -l flagPrefix (string match -r -- '-.*=' "$lastArg")

    __%[1]s_debug "Comps: $comps"
    __%[1]s_debug "DirectiveLine: $directiveLine"
    __%[1]s_debug "flagPrefix: $flagPrefix"

    for comp in $comps
        printf "%%s%%s\n" "$flagPrefix" "$comp"
    end

    printf "%%s\n" "$directiveLine"
end

function __%[1]s_doesnt_requires_order_preservation
    __%[1]s_debug ""
    __%[1]s_debug "========= checking if order preservation is required =========="

    set -l results (__%[1]s_perform_completion)
    if test -z "$results"
        __%[1]s_debug "Error determining if order preservation is required"
        return 1
    end

    set -l directive (string sub --start 2 $results[-1])
    __%[1]s_debug "Directive is: $directive"

    set -l shellCompDirectiveKeepOrder %[9]d
    set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2)
    __%[1]s_debug "Keeporder is: $keeporder"

    if test $keeporder -ne 0
        __%[1]s_debug "This does require order preservation"
        return 1
    end

    __%[1]s_debug "This doesn't require order preservation"
    return 0
end


Review comment (refactor):
I suggest simplifying by flipping the behaviour and call this `__%[1]s_requires_order_preservation`

Code after:
            set results $results[1..-2]
        else
            # Found non-empty line, we have our proper output
            break
        end
    end

    set -l comps $results[1..-2]
    set -l directiveLine $results[-1]

    # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
    # completions must be prefixed with the flag
    set -l flagPrefix (string match -r -- '-.*=' "$lastArg")

    __%[1]s_debug "Comps: $comps"
    __%[1]s_debug "DirectiveLine: $directiveLine"
    __%[1]s_debug "flagPrefix: $flagPrefix"

    for comp in $comps
        printf "%%s%%s\n" "$flagPrefix" "$comp"
    end

    printf "%%s\n" "$directiveLine"
end

# this function limits calls to __%[1]s_perform_completion, by caching the result behind $__%[1]s_perform_completion_once_result
function __%[1]s_perform_completion_once
    __%[1]s_debug "Starting __%[1]s_perform_completion_once"

    if test -n "$__%[1]s_perform_completion_once_result"
        __%[1]s_debug "Seems like a valid result already exists, skipping __%[1]s_perform_completion"
        return 0
    end

    set --global __%[1]s_perform_completion_once_result (__%[1]s_perform_completion)
    if test -z "$__%[1]s_perform_completion_once_result"
        __%[1]s_debug "No completions, probably due to a failure"
        return 1
    end

    __%[1]s_debug "Performed completions and set __%[1]s_perform_completion_once_result"
    return 0
end

# this function is used to clear the $__%[1]s_perform_completion_once_result variable after completions are run
function __%[1]s_clear_perform_completion_once_result
    __%[1]s_debug ""
    __%[1]s_debug "========= clearing previously set __%[1]s_perform_completion_once_result variable =========="
    set --erase __%[1]s_perform_completion_once_result
    __%[1]s_debug "Succesfully erased the variable __%[1]s_perform_completion_once_result"
end