Repository: BurntSushi/ripgrep
File: crates/core/flags/complete/rg.zsh (Shell)

Code before:
  local -a expl
  local -aU _encodings

  _encodings=(
!ENCODINGS!
  )

  _wanted encodings expl encoding compadd -a "$@" - _encodings
}

# Complete file types
_rg_types() {
  local -a expl
  local -aU _types

  _types=( ${(@)${(f)"$( _call_program types $words[1] --type-list )"}//:[[:space:]]##/:} )

  if zstyle -t ":completion:${curcontext}:types" extra-verbose; then
    _describe -t types 'file type' _types
  else
    _wanted types expl 'file type' compadd "$@" - ${(@)_types%%:*}
  fi
}

# don't run the completion function when being sourced by itself. See #2956
if [[ "$funcstack[1]" = "_rg" ]] || ! type compdef >/dev/null 2>&1; then
  _rg "$@"
else
    compdef _rg rg
fi

################################################################################
# ZSH COMPLETION REFERENCE
#
# For the convenience of developers who aren't especially familiar with zsh
# completion functions, a brief reference guide follows. This is in no way
# comprehensive; it covers just enough of the basic structure, syntax, and
# conventions to help someone make simple changes like adding new options. For
# more complete documentation regarding zsh completion functions, please see the
# following:
#
# * http://zsh.sourceforge.net/Doc/Release/Completion-System.html
# * https://github.com/zsh-users/zsh/blob/master/Etc/completion-style-guide
#
# OVERVIEW
#
# Most zsh completion functions are defined in terms of `_arguments`, which is a
# shell function that takes a series of argument specifications. The specs for
# `rg` are stored in an array, which is common for more complex functions; the
# elements of the array are passed to `_arguments` on invocation.
#

Review comment (bug):
I originally wrote this as 

```zsh
if [[ "$funcstack[1]" = "_rg" ]]; then
  _rg "$@"
elif type compdef >/dev/null; then
    compdef _rg rg
fi
```

But `ci/test-complete` didn't like that.  ~~I feel like the test is wrong~~ (nvm I am wrong!), because realistically you will either have `compdef` or will run the completion as `_rg`.  I stared at the test for a few seconds and didn't want to spend more time digging into it.  Instead, I worked around it by making it


```zsh
if [[ "$funcstack[1]" = "_rg" ]]; then
  _rg "$@"
elif type compdef >/dev/null; then
    compdef _rg rg
else
  _rg "$@"
fi
```

To be clear, this code is fine, but feels redundant 🤷🏼‍♀️

Code after:
  local -a expl
  local -aU _encodings

  _encodings=(
!ENCODINGS!
  )

  _wanted encodings expl encoding compadd -a "$@" - _encodings
}

# Complete file types
_rg_types() {
  local -a expl
  local -aU _types

  _types=( ${(@)${(f)"$( _call_program types $words[1] --type-list )"}//:[[:space:]]##/:} )

  if zstyle -t ":completion:${curcontext}:types" extra-verbose; then
    _describe -t types 'file type' _types
  else
    _wanted types expl 'file type' compadd "$@" - ${(@)_types%%:*}
  fi
}

# Don't run the completion function when being sourced by itself.
#
# See https://github.com/BurntSushi/ripgrep/issues/2956
# See https://github.com/BurntSushi/ripgrep/pull/2957
if [[ $funcstack[1] == _rg ]] || (( ! $+functions[compdef] )); then
  _rg "$@"
else
  compdef _rg rg
fi

################################################################################
# ZSH COMPLETION REFERENCE
#
# For the convenience of developers who aren't especially familiar with zsh
# completion functions, a brief reference guide follows. This is in no way
# comprehensive; it covers just enough of the basic structure, syntax, and
# conventions to help someone make simple changes like adding new options. For
# more complete documentation regarding zsh completion functions, please see the
# following:
#
# * http://zsh.sourceforge.net/Doc/Release/Completion-System.html
# * https://github.com/zsh-users/zsh/blob/master/Etc/completion-style-guide
#
# OVERVIEW
#
# Most zsh completion functions are defined in terms of `_arguments`, which is a
# shell function that takes a series of argument specifications. The specs for