* Generate zsh completion command using native zsh commands
( prefix: string, completionType: ShellCompletionType, )
| 161 | * Generate zsh completion command using native zsh commands |
| 162 | */ |
| 163 | function getZshCompletionCommand( |
| 164 | prefix: string, |
| 165 | completionType: ShellCompletionType, |
| 166 | ): string { |
| 167 | if (completionType === 'variable') { |
| 168 | // Variable completion - use zsh pattern matching for safe filtering |
| 169 | const varName = prefix.slice(1) |
| 170 | return `print -rl -- \${(k)parameters[(I)${quote([varName])}*]} 2>/dev/null` |
| 171 | } else if (completionType === 'file') { |
| 172 | // File completion with trailing slash for directories and trailing space for files |
| 173 | // Note: zsh glob expansion is safe from command injection (unlike bash for-in loops) |
| 174 | return `for f in ${quote([prefix])}*(N[1,${MAX_SHELL_COMPLETIONS}]); do [[ -d "$f" ]] && echo "$f/" || echo "$f "; done` |
| 175 | } else { |
| 176 | // Command completion - use zsh pattern matching for safe filtering |
| 177 | return `print -rl -- \${(k)commands[(I)${quote([prefix])}*]} 2>/dev/null` |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get completions for the given shell type |
no test coverage detected