This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

tools/zsh: add lazy completion loader for dynamic command completions

Adds a lazy completion loader that dynamically discovers and loads
completions from the installation path of commands. This allows
zsh to find completions for any command based on its ../share directory
without requiring manual configuration.

+65
+65
tools/zsh/flake-module.nix
··· 60 60 #enable automatic rehashing of $PATH 61 61 zstyle ':completion:*' rehash true 62 62 63 + # Lazy load completions for commands based on their installation path 64 + _lazy_completion_loader() { 65 + local cmd="$words[1]" 66 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: _lazy_completion_loader called for cmd: $cmd" >&2 67 + 68 + if command -v "$cmd" >/dev/null 2>&1; then 69 + local cmd_path="$(which "$cmd" 2>/dev/null)" 70 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: initial cmd_path: $cmd_path" >&2 71 + 72 + local found_zsh_completions=false 73 + 74 + # Follow absolute symlinks and check each level 75 + while [[ -n "$cmd_path" ]]; do 76 + local share_dir="$(dirname "$cmd_path")/../share" 77 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: checking share_dir: $share_dir" >&2 78 + 79 + # Check zsh completion directories 80 + for dir in "$share_dir/zsh/site-functions" "$share_dir/zsh/vendor-completions"; do 81 + if [[ -d "$dir" ]]; then 82 + [[ ! " ''${fpath[@]} " =~ " $dir " ]] && fpath=("$dir" $fpath) 83 + if [[ -f "$dir/_$cmd" ]]; then 84 + found_zsh_completions=true 85 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: found zsh completion: $dir/_$cmd" >&2 86 + fi 87 + fi 88 + done 89 + 90 + # Follow absolute symlink or stop 91 + if [[ -L "$cmd_path" ]]; then 92 + cmd_path="$(readlink "$cmd_path" 2>/dev/null)" 93 + [[ "$cmd_path" == /* ]] || cmd_path="" # Only follow absolute symlinks 94 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: following symlink to: $cmd_path" >&2 95 + else 96 + break 97 + fi 98 + done 99 + 100 + # Load completions if found 101 + if [[ "$found_zsh_completions" == true ]]; then 102 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: reinitializing completions" >&2 103 + compinit &>/dev/null 104 + fi 105 + fi 106 + 107 + # Save completion function before removing lazy loader 108 + local completion_function="''${_comps[$cmd]:-}" 109 + compdef -d "$cmd" 2>/dev/null 110 + 111 + # Use loaded completion or fall back to normal 112 + if [[ -n "$completion_function" ]]; then 113 + $completion_function "$@" 114 + else 115 + _normal 116 + fi 117 + } 118 + 119 + # Set as default completer for commands without completions 120 + zstyle -e ':completion:*' completer ' 121 + if [[ -z $_comps[$words[1]] ]]; then 122 + reply=(_lazy_completion_loader _complete) 123 + else 124 + reply=(_complete) 125 + fi 126 + ' 127 + 63 128 # fancy mv which interactively gets the second argument if not given 64 129 function mv() { 65 130 if [[ "$#" -ne 1 ]] || [[ ! -e "$1" ]]; then