# http://zsh.sourceforge.net/Doc/Release/Completion-System.html
# https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/completion.zsh

# load fancier completion menu which (most notably) supports `list-colors`
zmodload zsh/complist
zstyle ':completion:*' menu select
# show even more completion results
zstyle ':completion:*' verbose yes

zstyle ':completion:*' use-cache yes
zstyle ':completion:*' cache-path "$ZSH_CACHE_DIR"

# group completion result based on their categories
zstyle ':completion:*' group-name ''
# format for displaying category names
zstyle ':completion:*:descriptions' format '%F{yellow}[%d]%f'

# Sometimes zsh completion authors for whatever reason add descriptions for
# option values, but don't do describe the options themselves (e.g. ffmpeg,
# some options for GCC). In such cases description of an option can be inferred
# from the description of its value. That's the purpose of `auto-description`.
zstyle ':completion:*:options' auto-description '%d'

# case insensitive (all), partial-word and substring completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|=*' 'l:|=* r:|=*'

zstyle ':completion:*' list-dirs-first yes
# complete . and .. directories
# This is very useful when I just want to quickly look around inside a
# directory without running `ls`
zstyle ':completion:*' special-dirs yes

if [[ -n "$LS_COLORS" ]]; then
  zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}"
fi

zstyle ':completion:*:processes' command "ps xo pid,user,cmd"
zstyle ':completion:*:processes-names' command "ps xho comm="
zstyle ':completion:*:processes' force-list always

_complete_ssh_hosts() {
  reply=( localhost )
  local line
  if [[ -r ~/.ssh/config ]]; then
    < ~/.ssh/config while IFS= read -r line; do
      if [[ "$line" =~ '^Host[[:blank:]]+(.*)[[:blank:]]*' ]]; then
        reply+=("${match[1]}")
      fi
    done
  fi
}
zstyle -e ':completion:*:hosts' hosts '_complete_ssh_hosts'

# Delete the completion dump if it is stale. Description of the glob qualifiers:
#   N    turn on NULL_GLOB for this expansion
#   .    match only plain files
#   m+0  check if the file was modified more than a day ago
# see "Filename Generation" in zshexpn(1).
for stale in "${ZSH_CACHE_DIR}/zcompdump"(N.m+0); do
  command rm -v "$stale"
done; unset stale

# -u disables the "security check", see "Use of compinit" in zshcompsys(1), and
# -d specifies the path to a completion dump file. -w was only added in a recent
# version of Zsh and prints the reason for updating the compdump if that happens
# (<https://github.com/zsh-users/zsh/commit/6f4cf791405e74925c497bf3493bcd834918cf85>).
autoload -Uz compinit is-at-least && \
  compinit -u -d "${ZSH_CACHE_DIR}/zcompdump" $(if is-at-least '5.8.1.2'; then print -- '-w'; fi)

if ! is_function _rustup && command_exists rustup; then
  lazy_load _rustup 'source <(rustup completions zsh)'
  compdef _rustup rustup
fi

if ! is_function _cargo && command_exists rustup && command_exists rustc; then
  lazy_load _cargo 'source "$(rustc --print sysroot)/share/zsh/site-functions/_cargo"'
  compdef _cargo cargo
fi

# Complete any commands following simple wrapper functions.
# <https://stackoverflow.com/a/13547531>
compdef 'shift words; (( CURRENT-- )); _normal' prime-run allow-ptrace
