From 4ea51d1aa215cfef443111d6f474d7d466d8510c Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Sat, 15 May 2021 14:59:31 -0500 Subject: [PATCH] node: Optimize completions for loading lazily on demand Move `grunt` and `gulp` to separate completion definitions wrapping `grunt --completion=zsh` and `gulp --completion=zsh` respectively. Since the completions are loaded lazily on demand, they avoid the performance overhead during Zsh initialization. Additionally, remove `npm` completion since it is already bundled with Zsh for quite a while. --- modules/node/README.md | 3 +++ modules/node/functions/_grunt | 15 +++++++++++++++ modules/node/functions/_gulp | 15 +++++++++++++++ modules/node/init.zsh | 28 +--------------------------- 4 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 modules/node/functions/_grunt create mode 100644 modules/node/functions/_gulp diff --git a/modules/node/README.md b/modules/node/README.md index 2de4faa..edff6e6 100644 --- a/modules/node/README.md +++ b/modules/node/README.md @@ -3,6 +3,9 @@ Provides utility functions for [Node.js][1], loads the Node Version Manager, and enables [_npm_][2] completion. +This module must be loaded _before_ the _`completion`_ module so that the +provided completion definitions are loaded. + ## nodenv [_nodenv_][5] does one thing well - it is concerned solely with switching diff --git a/modules/node/functions/_grunt b/modules/node/functions/_grunt new file mode 100644 index 0000000..4f1a188 --- /dev/null +++ b/modules/node/functions/_grunt @@ -0,0 +1,15 @@ +#compdef grunt +#autoload + +# +# Grunt completion, delegating to grunt to do all the completion work. +# +# Authors: +# Indrajit Raychaudhuri +# + +if (( $+commands[grunt] )); then + eval "$(grunt --completion=zsh)" + + _grunt_completion "$@" +fi diff --git a/modules/node/functions/_gulp b/modules/node/functions/_gulp new file mode 100644 index 0000000..b06acca --- /dev/null +++ b/modules/node/functions/_gulp @@ -0,0 +1,15 @@ +#compdef gulp +#autoload + +# +# Gulp completion, delegating to gulp to do all the completion work. +# +# Authors: +# Indrajit Raychaudhuri +# + +if (( $+commands[gulp] )); then + eval "$(gulp --completion=zsh)" + + _gulp_completion "$@" +fi diff --git a/modules/node/init.zsh b/modules/node/init.zsh index b436a0d..5051570 100644 --- a/modules/node/init.zsh +++ b/modules/node/init.zsh @@ -5,6 +5,7 @@ # Authors: # Sorin Ionescu # Zeh Rizzatti +# Indrajit Raychaudhuri # # Possible lookup locations. @@ -36,30 +37,3 @@ elif (( $+commands[brew] )) \ elif (( ! $+commands[node] )); then return 1 fi - -# Load NPM and known helper completions. -typeset -A _compl_commands=( - npm 'npm completion' - grunt 'grunt --completion=zsh' - gulp 'gulp --completion=zsh' -) - -for _compl_command in "${(k)_compl_commands[@]}"; do - if (( $+commands[$_compl_command] )); then - cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/prezto/${_compl_command}-cache.zsh" - - # Completion commands are slow; cache their output if old or missing. - if [[ "$commands[$_compl_command]" -nt "$cache_file" \ - || "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \ - || ! -s "$cache_file" ]]; then - mkdir -p "$cache_file:h" - command ${=_compl_commands[$_compl_command]} >! "$cache_file" 2> /dev/null - fi - - source "$cache_file" - - unset cache_file - fi -done - -unset _compl_command{s,}