Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for completion files #618

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ jobs:
mkdir -p "$HOME/.local/bin" && echo "$HOME/.local/bin" >> $GITHUB_PATH
git clone --depth 1 https://github.com/zdharma-continuum/zunit; cd ./zunit
./configure --prefix=$HOME/.local && make all install


- name: "gh-r"
run: $HOME/.local/bin/zunit run --verbose tests/gh-r.zunit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
run: $HOME/.local/bin/zunit run --verbose tests/gh-r.zunit
run: zunit run --verbose tests/gh-r.zunit


- name: "annexes"
run: zunit run tests/annexes.zunit

Expand All @@ -79,9 +82,6 @@ jobs:
- name: "compile"
run: zunit run tests/compile.zunit

- name: "gh-r"
run: zunit run tests/gh-r.zunit

- name: "ices"
run: zunit run tests/ices.zunit

Expand Down
2 changes: 1 addition & 1 deletion tests/gh-r.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
HOME="$zi_test_dir" # Stops programs creating directories in user home
typeset -gx ZBIN="$zi_test_dir/polaris/bin" os_type="${OSTYPE//[0-9\.]*/}"
[[ ! -d $ZBIN ]] && mkdir -p "$ZBIN"
zinit default-ice --quiet from'gh-r' lbin'!' null
zinit default-ice --quiet from'gh-r' lbin'!'
}

@test 'act' { # Run your GitHub Actions locally
Expand Down
11 changes: 8 additions & 3 deletions tests/ices.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,32 @@
assert "$ZPLUGINS/test---make/whatever" is_file
}
@test 'completions' {
run zinit as"null" id-as"test/completions" atclone"touch _whatever" completions for zdharma-continuum/null
run zinit as"null" id-as"test/completions" atclone"echo '#compdef whatever' >| _whatever" completions for zdharma-continuum/null
assert $state equals 0
assert "$ZPLUGINS/test---completions/_whatever" is_file
assert "$ZINIT[COMPLETIONS_DIR]/_whatever" is_file
run zinit completions
}
@test 'completions-overwrite' {
# if both are given, the completions wins
run zinit as"null" id-as"test/completions-overwrite" atclone"touch _whatever2" nocompletions completions for zdharma-continuum/null
run zinit as"null" id-as"test/completions-overwrite" atclone"echo '#compdef whatever2' >| _whatever2" nocompletions completions for zdharma-continuum/null
assert $state equals 0
assert "$ZPLUGINS/test---completions-overwrite/_whatever2" is_file
assert "$ZINIT[COMPLETIONS_DIR]/_whatever2" is_file
run zinit completions
}
@test 'completions-ignored' {
# only the _valid file should be installed as a completion
run zinit as"null" id-as"test/ignored_completions" atclone"touch __init__.py _valid" completions for zdharma-continuum/null
run zinit as"null" id-as"test/ignored_completions" atclone"touch __init__.py; echo '#compdef valid' >| _valid" completions for zdharma-continuum/null
assert $state equals 0
assert "$ZPLUGINS/test---ignored_completions/_valid" is_file
assert "$ZPLUGINS/test---ignored_completions/__init__.py" is_file
assert "$ZINIT[COMPLETIONS_DIR]/_valid" is_file
COMPS=( "$ZINIT[COMPLETIONS_DIR]"/_* )
assert __init__.py is_not_value_in $COMPS
zinit delete --yes test/ignored_completions
run zinit cuninstall test/ignored_completions
assert $output contains 'No completions found for `test/ignored_completions'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a test with two files of the same name getting linked under different names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jankatins Good idea.

}

# vim:ft=zsh:sw=2:sts=2:et:foldmarker=\ {,}:foldmethod=marker
83 changes: 43 additions & 40 deletions zinit-install.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -528,78 +528,81 @@ builtin source "${ZINIT[BIN_DIR]}/zinit-side.zsh" || {
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
# $2 - plugin if $1 (i.e., user) given
# $3 - if 1, then reinstall, otherwise only install completions that are not present
.zinit-install-completions() {
.zinit-install-completions () {
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
setopt nullglob extendedglob warncreateglobal typesetsilent noshortloops
setopt extended_glob no_short_loops null_glob typeset_silent warn_create_global

local id_as=$1${2:+${${${(M)1:#%}:+$2}:-/$2}}
local reinstall=${3:-0} quiet=${${4:+1}:-0}
(( OPTS[opt_-q,--quiet] )) && quiet=1
[[ $4 = -Q ]] && quiet=2
typeset -ga INSTALLED_COMPS SKIPPED_COMPS
INSTALLED_COMPS=() SKIPPED_COMPS=()
local id_as=$1${2:+${${${(M)1:#%}:+$2}:-/$2}}
local reinstall=${3:-0} quiet=${${4:+1}:-0}

(( OPTS[opt_-q,--quiet] )) && quiet=1
[[ $4 = -Q ]] && quiet=2

typeset -gaU INSTALLED_COMPS SKIPPED_COMPS
INSTALLED_COMPS=() SKIPPED_COMPS=()

.zinit-any-to-user-plugin "$id_as" ""
local user=${reply[-2]}
local plugin=${reply[-1]}
local user=${reply[-2]}
local plugin=${reply[-1]}
.zinit-any-colorify-as-uspl2 "$user" "$plugin"
local abbrev_pspec=$REPLY

local abbrev_pspec=$REPLY
.zinit-exists-physically-message "$id_as" "" || return 1

# Symlink any completion files included in the plugin directory
typeset -a completions already_symlinked backup_comps
typeset -aU completions already_symlinked backup_comps
local c cfile bkpfile
# The plugin == . is a semi-hack/trick to handle 'creinstall .' properly
nt () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats nt?

Copy link
Member Author

@vladdoster vladdoster Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nt is just a random function name. It could be anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, stupid question: why not a speaking function name so one can actually know what it does by just reading the function name? reading the zinit code is hard enough even without random function names :-)

Copy link
Member Author

@vladdoster vladdoster Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jankatins,

lol, I just took the name from the zsh docs. But agreed, it should be renamed.

Screenshot 2024-01-18 at 1 55 51 PM

[[ $(head -n 1 $REPLY) != *compdef* ]]
}
# Symlink any completion files included in the plugin directory
[[ $user == % || ( -z $user && $plugin == . ) ]] && \
completions=( "${plugin}"/**/_[^_.]*~*(*.zwc|*.html|*.txt|*.png|*.jpg|*.jpeg|*.js|*.md|*.yml|*.yaml|*.py|*.ri|_zsh_highlight*|/zsdoc/*|*.ps1)(DN^/) ) || \
completions=( "${ZINIT[PLUGINS_DIR]}/${id_as//\//---}"/**/_[^_.]*~*(*.zwc|*.html|*.txt|*.png|*.jpg|*.jpeg|*.js|*.md|*.yml|*.yaml|*.py|*.ri|_zsh_highlight*|/zsdoc/*|*.ps1)(DN^/) )
already_symlinked=( "${ZINIT[COMPLETIONS_DIR]}"/_[^_.]*~*.zwc(DN) )
backup_comps=( "${ZINIT[COMPLETIONS_DIR]}"/[^_.]*~*.zwc(DN) )

completions=("${plugin}"/**/(_*|*.zsh*)(OLDN^/+nt)) || \
completions=("${ZINIT[PLUGINS_DIR]}/${id_as//\//---}"/**/(_*|*.zsh*)(OLDN^/+nt))
already_symlinked=("${ZINIT[COMPLETIONS_DIR]}"/_[^_.]*~*.zwc(DN))
backup_comps=("${ZINIT[COMPLETIONS_DIR]}"/[^_.]*~*.zwc(DN))
# Symlink completions if they are not already there
# either as completions (_fname) or as backups (fname)
# OR - if its a reinstall
for c in "${completions[@]:A}"; do
cfile="${c:t}"
bkpfile="${cfile#_}"
if [[ ( -z ${already_symlinked[(r)*/$cfile]} || $reinstall = 1 ) &&
-z ${backup_comps[(r)*/$bkpfile]}
]]; then
local cfile="${c:t}"
if [[ $cfile != (#s)_* ]]; then
cfile="_${${$(head -n1 ${c})//\#compdef/}// /}"
if (($completions[(Ie)$cfile])); then
+zi-log "{m} ${cfile} is amongst the values of the array"
continue
else
+zi-log "{m} Renaming completion {obj}${c:t}{rst} as {file}_${${$(head -n1 ${c})//\#compdef/}// /}{rst}"
fi
fi
bkpfile="${cfile#_}"
if [[ ( -z ${already_symlinked[(r)*/$cfile]} || $reinstall = 1 ) && -z ${backup_comps[(r)*/$bkpfile]} ]]; then
if [[ $reinstall = 1 ]]; then
# Remove old files
command rm -f "${ZINIT[COMPLETIONS_DIR]}/$cfile" "${ZINIT[COMPLETIONS_DIR]}/$bkpfile"
fi
INSTALLED_COMPS+=( $cfile )
INSTALLED_COMPS+=($cfile)
(( quiet )) || builtin print -Pr "Symlinking completion ${ZINIT[col-uname]}$cfile%f%b to completions directory."
command ln -fs "$c" "${ZINIT[COMPLETIONS_DIR]}/$cfile"
# Make compinit notice the change
.zinit-forget-completion "$cfile" "$quiet"
else
SKIPPED_COMPS+=( $cfile )
SKIPPED_COMPS+=($cfile)
(( quiet )) || builtin print -Pr "Not symlinking completion \`${ZINIT[col-obj]}$cfile%f%b', it already exists."
(( quiet )) || builtin print -Pr "${ZINIT[col-info2]}Use \`${ZINIT[col-pname]}zinit creinstall $abbrev_pspec${ZINIT[col-info2]}' to force install.%f%b"
fi
done

local comps msg
local -A comp_types=(\$INSTALLED_COMPS 'Installed' \$SKIPPED_COMPS 'Skipped re-installing')
local -A comp_types=(\$INSTALLED_COMPS 'Installed' \$SKIPPED_COMPS 'Skipped re-installing')
for comps msg in ${(kv)comp_types}; do
local comps_num=${#${(e)comps}}
local comps_num=${#${(e)comps}}
if (( comps_num > 0 )); then
+zi-log "{m} ${msg} {num}$comps_num{rst} completion${=${comps_num:#1}:+s}"
if (( quiet == 0 )); then
+zi-log "{m} Added $comps_num completion${=${comps_num:#1}:+s} to {var}$comps{rst} array"
fi
fi
done

if (( ZSH_SUBSHELL )) {
builtin print -rl -- $INSTALLED_COMPS >! ${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst
builtin print -rl -- $SKIPPED_COMPS >! ${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst
}

.zinit-compinit 1 1 &>/dev/null
if (( ZSH_SUBSHELL )); then
builtin print -rl -- $INSTALLED_COMPS >| ${TMPDIR:-/tmp}/zinit.installed_comps.$$.lst
builtin print -rl -- $SKIPPED_COMPS >| ${TMPDIR:-/tmp}/zinit.skipped_comps.$$.lst
fi
.zinit-compinit 1 1 &> /dev/null
} # ]]]
# FUNCTION: .zinit-compinit [[[
# User-exposed `compinit' frontend which first ensures that all
Expand Down
Loading