QuarticCat / zsh-smartcache

A Zsh plugin to cache command output to boost shell startup.
MIT License
27 stars 2 forks source link

fix: Completion file's name should be start with `_` #2

Closed lentil32 closed 3 months ago

lentil32 commented 7 months ago

Current behavior

autocompletion file's name is not starting with _, so compinit does not recognize the autocompletion.

Suggestion

_smartcache-comp() {
    local cache=$1; shift
+    filename=${cache:t}
+    modified_cache="${cache/$filename/_$filename}"
-    if [[ ! -f $cache ]] {
+    if [[ ! -f $modified_cache ]] {
        local cmd=$1
        autoload -Uz _$cmd
        typeset -gA _comps
        _comps[$cmd]=_$cmd
    }
    fpath+=($ZSH_SMARTCACHE_DIR)

-    $@ > $cache &!
+    $@ > $modified_cache &!
}

Reference

zsh-completions/zsh-completions-howto.org at master · zsh-users/zsh-completions

Completion functions for commands are stored in files with names beginning with an underscore _, and these files should be placed in a directory listed in the $fpath variable.

lentil32 commented 7 months ago

The above function not work when cache file dosn't exist, i.e., first run with that completion.

So, I use like following:

_smartcache-comp() {
    local cache=$1; shift
    filename=${cache:t}
    modified_cache="${cache/$filename/_$filename}"
    $@ > $modified_cache &!
}

Sourcing this plugin before compinit and it works properly.