This repository has no description
0

Configure Feed

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

tools/zsh: fix lazy completion loader to follow symlink chains

+43 -40
+43 -40
tools/zsh/flake-module.nix
··· 63 63 # Lazy load completions for commands based on their installation path 64 64 _lazy_completion_loader() { 65 65 local cmd="$words[1]" 66 - [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: _lazy_completion_loader called for cmd: $cmd" >&2 66 + local basename_cmd="$cmd" 67 + local cmd_path="" 67 68 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 69 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: _lazy_completion_loader called for: $cmd" >&2 71 70 72 - local found_zsh_completions=false 71 + # Get command path and basename 72 + if [[ "$cmd" == /* ]] && [[ -x "$cmd" ]]; then 73 + cmd_path="$cmd" 74 + basename_cmd="$(basename "$cmd")" 75 + elif command -v "$cmd" >/dev/null 2>&1; then 76 + cmd_path="$(which "$cmd" 2>/dev/null)" 77 + basename_cmd="$(basename "$cmd_path")" 78 + fi 73 79 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 80 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: cmd_path=$cmd_path, basename_cmd=$basename_cmd" >&2 78 81 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 82 + # Find and load completion file 83 + local current="$cmd_path" 84 + while [[ -n "$current" ]]; do 85 + local share_dir="$(dirname "$current")/../share" 86 + for subdir in zsh/site-functions zsh/vendor-completions; do 87 + local dir="$share_dir/$subdir" 88 + if [[ -f "$dir/_$basename_cmd" ]]; then 89 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: found completion: $dir/_$basename_cmd" >&2 90 + [[ ! " ''${fpath[@]} " =~ " $dir " ]] && fpath=("$dir" $fpath) 91 + source "$dir/_$basename_cmd" 92 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: after sourcing, _comps[$basename_cmd]=''${_comps[$basename_cmd]:-not set}" >&2 93 + break 2 97 94 fi 98 95 done 96 + # Follow absolute symlinks only 97 + [[ -L "$current" ]] || break 98 + current="$(readlink "$current" 2>/dev/null)" 99 + [[ "$current" == /* ]] || break 100 + done 99 101 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 102 + # Register completion for the original command if needed 103 + if [[ -n "''${_comps[$basename_cmd]:-}" ]]; then 104 + if [[ "$cmd" != "$basename_cmd" ]]; then 105 + _comps[$cmd]="''${_comps[$basename_cmd]}" 106 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: registered ''${_comps[$basename_cmd]} for $cmd" >&2 104 107 fi 105 108 fi 106 109 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 110 + # Remove lazy loader only if it's still set 111 + if [[ "''${_comps[$cmd]:-}" == "_lazy_completion_loader" ]]; then 112 + compdef -d "$cmd" 2>/dev/null 116 113 fi 114 + 115 + # Run the appropriate completion 116 + local comp="''${_comps[$cmd]:-}" 117 + [[ -z "$comp" ]] && comp="''${_comps[$basename_cmd]:-_normal}" 118 + [[ -n "$ZSH_COMP_DEBUG" ]] && echo "DEBUG: using completion: $comp" >&2 119 + $comp "$@" 117 120 } 118 121 119 122 # Set as default completer for commands without completions