[zsh] Rewrite the completion generation logic

This commit is contained in:
Alyxia Sother 2023-06-05 10:56:19 +02:00
parent 7255035de4
commit bdabaa7546
No known key found for this signature in database
GPG key ID: 01E16C4E775A37E4

View file

@ -6,6 +6,25 @@ _plugin() {
[ -z "$DOTFILES_LOAD_SILENT" ] && _perf_timer_stop "plugin $1"
}
# based off of Dima's completion generation code, but made dynamic
# https://github.com/dmitmel/dotfiles/blob/19d80233265629b33cf57daf05a928239b0c73a8/zsh/plugins.zsh#L17-L25
_addcomp() {
local command_name="$1"
local command_args="${2:-completion zsh}"
declare ${command_name}_bin="$(command_locate $command_name)"
local command_bin="${(P)${:-${command_name}_bin}}"
if [[ -n "${command_bin}" ]]; then
declare ${command_name}_comp_path="${ZSH_CACHE_DIR}/site-functions/_${command_name}"
local comp_path="${(P)${:-${command_name}_comp_path}}"
if [[ "$command_bin" -nt "$comp_path" || ! -s "$comp_path" ]]; then
_perf_timer_start "generate $command_name completions"
eval "$command_bin" "$command_args" >| "$comp_path"
_perf_timer_stop "generate $command_name completions"
fi
fi; unset comp_path
}
_plugin gitio 'denysdovhan/gitio-zsh'
# Load the brilliant project management tool.
@ -15,36 +34,9 @@ _plugin project 'https://git.sr.ht/~keanucode/scripts/blob/master/project/projec
load="project.zsh" \
# Completions {{{
# from Dima, assuming it's based off of:
# https://github.com/dmitmel/dotfiles/blob/19d80233265629b33cf57daf05a928239b0c73a8/zsh/plugins.zsh#L17-L25
if gh_bin="$(command_locate gh)" && [[ -n "$gh_bin" ]]; then
gh_comp_path="${ZSH_CACHE_DIR}/site-functions/_gh"
if [[ "$gh_bin" -nt "$gh_comp_path" || ! -s "$gh_comp_path" ]]; then
_perf_timer_start "generate gh completions"
"$gh_bin" completion -s zsh >| "$gh_comp_path"
_perf_timer_stop "generate gh completions"
fi
unset gh_comp_path
fi; unset gh_bin
if bw_bin="$(command_locate bw)" && [[ -n "$bw_bin" ]]; then
bw_comp_path="${ZSH_CACHE_DIR}/site-functions/_bw"
if [[ "$bw_bin" -nt "$bw_comp_path" || ! -s "$bw_comp_path" ]]; then
_perf_timer_start "generate bw completions"
"$bw_bin" completion --shell zsh >| "$bw_comp_path"
_perf_timer_stop "generate bw completions"
fi
unset bw_comp_path
fi; unset bw_bin
if kctl_bin="$(command_locate kubectl)" && [[ -n "$kctl_bin" ]]; then
kctl_comp_path="${ZSH_CACHE_DIR}/site-functions/_kubectl"
if [[ "$kctl_bin" -nt "$kctl_comp_path" || ! -s "$kctl_comp_path" ]]; then
_perf_timer_start "generate kubectl completions"
"$kctl_bin" completion zsh >| "$kctl_comp_path"
_perf_timer_stop "generate kubectl completions"
fi
fi; unset kctl_comp_path
_addcomp gh "completion -s zsh"
_addcomp bw "completion --shell zsh"
_addcomp kubectl
_plugin nim-compl 'https://raw.githubusercontent.com/nim-lang/Nim/devel/tools/nim.zsh-completion' from=url \
after_load='plugin-cfg-path fpath prepend ""'