_rg() {
    local i cur prev opts cmds
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cmd=""
    opts=""

    for i in ${COMP_WORDS[@]}
    do
        case "${i}" in
            rg)
                cmd="rg"
                ;;
            
            ripgrep)
                cmd+="__ripgrep"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        rg)
            opts=" -a -c -F -i -n -N -q -u -v -w -x -l -H -L -0 -o -p -s -S -h -V -e -E -g -t -T -A -B -C -f -m -r -j -M  --files --type-list --text --count --fixed-strings --ignore-case --line-number --no-line-number --quiet --unrestricted --invert-match --word-regexp --line-regexp --column --debug --files-with-matches --files-without-match --with-filename --no-filename --heading --no-heading --hidden --follow --mmap --no-messages --no-mmap --no-ignore --no-ignore-parent --no-ignore-vcs --null --only-matching --pretty --case-sensitive --smart-case --sort-files --vimgrep --help --version --regexp --color --colors --encoding --glob --iglob --type --type-not --after-context --before-context --context --context-separator --dfa-size-limit --file --ignore-file --max-count --max-filesize --maxdepth --path-separator --replace --regex-size-limit --threads --max-columns --type-add --type-clear  <PATTERN> <path>... "
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
                return 0
            fi
            case "${prev}" in
                
                --regexp)
                    COMPREPLY=("<PATTERN>...")
                    return 0
                    ;;
                    -e)
                    COMPREPLY=("<PATTERN>...")
                    return 0
                    ;;
                --color)
                    COMPREPLY=($(compgen -W "never auto always ansi" -- ${cur}))
                    return 0
                    ;;
                --colors)
                    COMPREPLY=("<SPEC>...")
                    return 0
                    ;;
                --encoding)
                    COMPREPLY=("<ENCODING>")
                    return 0
                    ;;
                    -E)
                    COMPREPLY=("<ENCODING>")
                    return 0
                    ;;
                --glob)
                    COMPREPLY=("<GLOB>...")
                    return 0
                    ;;
                    -g)
                    COMPREPLY=("<GLOB>...")
                    return 0
                    ;;
                --iglob)
                    COMPREPLY=("<GLOB>...")
                    return 0
                    ;;
                --type)
                    COMPREPLY=("<TYPE>...")
                    return 0
                    ;;
                    -t)
                    COMPREPLY=("<TYPE>...")
                    return 0
                    ;;
                --type-not)
                    COMPREPLY=("<TYPE>...")
                    return 0
                    ;;
                    -T)
                    COMPREPLY=("<TYPE>...")
                    return 0
                    ;;
                --after-context)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                    -A)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                --before-context)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                    -B)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                --context)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                    -C)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                --context-separator)
                    COMPREPLY=("<SEPARATOR>")
                    return 0
                    ;;
                --dfa-size-limit)
                    COMPREPLY=("<NUM+SUFFIX?>")
                    return 0
                    ;;
                --file)
                    COMPREPLY=("<FILE>...")
                    return 0
                    ;;
                    -f)
                    COMPREPLY=("<FILE>...")
                    return 0
                    ;;
                --ignore-file)
                    COMPREPLY=("<FILE>...")
                    return 0
                    ;;
                --max-count)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                    -m)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                --max-filesize)
                    COMPREPLY=("<NUM+SUFFIX?>")
                    return 0
                    ;;
                --maxdepth)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                --path-separator)
                    COMPREPLY=("<SEPARATOR>")
                    return 0
                    ;;
                --replace)
                    COMPREPLY=("<ARG>")
                    return 0
                    ;;
                    -r)
                    COMPREPLY=("<ARG>")
                    return 0
                    ;;
                --regex-size-limit)
                    COMPREPLY=("<NUM+SUFFIX?>")
                    return 0
                    ;;
                --threads)
                    COMPREPLY=("<ARG>")
                    return 0
                    ;;
                    -j)
                    COMPREPLY=("<ARG>")
                    return 0
                    ;;
                --max-columns)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                    -M)
                    COMPREPLY=("<NUM>")
                    return 0
                    ;;
                --type-add)
                    COMPREPLY=("<TYPE>...")
                    return 0
                    ;;
                --type-clear)
                    COMPREPLY=("<TYPE>...")
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
            return 0
            ;;
        
    esac
}

complete -F _rg -o bashdefault -o default rg
