|
12 | 12 | extension [ParsableCommand.Type] { |
13 | 13 | /// Generates a Zsh completion script for the given command. |
14 | 14 | var zshCompletionScript: String { |
15 | | - """ |
16 | | - #compdef \(first?._commandName ?? "") |
| 15 | + // swift-format-ignore: NeverForceUnwrap |
| 16 | + // Preconditions: |
| 17 | + // - first must be non-empty for a zsh completion script to be of use. |
| 18 | + // - first is guaranteed non-empty in the one place where this computed var is used. |
| 19 | + let commandName = first!._commandName |
| 20 | + return """ |
| 21 | + #compdef \(commandName) |
17 | 22 |
|
18 | | - \(completeFunctionName)() { |
19 | | - local -ar non_empty_completions=("${@:#(|:*)}") |
20 | | - local -ar empty_completions=("${(M)@:#(|:*)}") |
21 | | - _describe '' non_empty_completions -- empty_completions -P $'\\'\\'' |
22 | | - } |
| 23 | + \(completeFunctionName)() { |
| 24 | + local -ar non_empty_completions=("${@:#(|:*)}") |
| 25 | + local -ar empty_completions=("${(M)@:#(|:*)}") |
| 26 | + _describe '' non_empty_completions -- empty_completions -P $'\\'\\'' |
| 27 | + } |
23 | 28 |
|
24 | | - \(customCompleteFunctionName)() { |
25 | | - local -a completions |
26 | | - completions=("${(@f)"$("${@}")"}") |
27 | | - if [[ "${#completions[@]}" -gt 1 ]]; then |
28 | | - \(completeFunctionName) "${completions[@]:0:-1}" |
29 | | - fi |
30 | | - } |
| 29 | + \(customCompleteFunctionName)() { |
| 30 | + local -a completions |
| 31 | + completions=("${(@f)"$("${@}")"}") |
| 32 | + if [[ "${#completions[@]}" -gt 1 ]]; then |
| 33 | + \(completeFunctionName) "${completions[@]:0:-1}" |
| 34 | + fi |
| 35 | + } |
31 | 36 |
|
32 | | - \(completionFunctions)\ |
33 | | - \(completionFunctionName()) |
34 | | - """ |
| 37 | + \(completionFunctions)\ |
| 38 | + \(completionFunctionName()) |
| 39 | + """ |
35 | 40 | } |
36 | 41 |
|
37 | 42 | private var completionFunctions: String { |
@@ -203,11 +208,15 @@ extension [ParsableCommand.Type] { |
203 | 208 | } |
204 | 209 |
|
205 | 210 | private var completeFunctionName: String { |
206 | | - "__\(first?._commandName ?? "")_complete" |
| 211 | + // swift-format-ignore: NeverForceUnwrap |
| 212 | + // Precondition: first is guaranteed to be non-empty |
| 213 | + "__\(first!._commandName)_complete" |
207 | 214 | } |
208 | 215 |
|
209 | 216 | private var customCompleteFunctionName: String { |
210 | | - "__\(first?._commandName ?? "")_custom_complete" |
| 217 | + // swift-format-ignore: NeverForceUnwrap |
| 218 | + // Precondition: first is guaranteed to be non-empty |
| 219 | + "__\(first!._commandName)_custom_complete" |
211 | 220 | } |
212 | 221 | } |
213 | 222 |
|
|
0 commit comments