12
12
struct ZshCompletionsGenerator {
13
13
/// Generates a Zsh completion script for the given command.
14
14
static func generateCompletionScript( _ type: ParsableCommand . Type ) -> String {
15
- let initialFunctionName = [ type ] . completionFunctionName ( )
15
+ let initialFunctionName = [ type ] . completionFunctionName ( ) . zshEscapingCommandName ( )
16
+ let commandName = type. _commandName. zshEscapingCommandName ( )
16
17
17
18
return """
18
- #compdef \( type . _commandName )
19
+ #compdef \( commandName )
19
20
local context state state_descr line
20
- _ \( type . _commandName ) _commandname=$words[1]
21
+ _ \( commandName ) _commandname=$words[1]
21
22
typeset -A opt_args
22
23
23
24
\( generateCompletionFunction ( [ type] ) )
@@ -32,7 +33,7 @@ struct ZshCompletionsGenerator {
32
33
33
34
static func generateCompletionFunction( _ commands: [ ParsableCommand . Type ] ) -> String {
34
35
let type = commands. last!
35
- let functionName = commands. completionFunctionName ( )
36
+ let functionName = commands. completionFunctionName ( ) . zshEscapingCommandName ( )
36
37
let isRootCommand = commands. count == 1
37
38
38
39
var args = generateCompletionArguments ( commands)
@@ -50,14 +51,14 @@ struct ZshCompletionsGenerator {
50
51
51
52
let subcommandModes = subcommands. map {
52
53
"""
53
- ' \( $0. _commandName) : \( $0. configuration. abstract. zshEscaped ( ) ) '
54
+ ' \( $0. _commandName. zshEscapingCommandName ( ) ) : \( $0. configuration. abstract. zshEscaped ( ) ) '
54
55
"""
55
56
. indentingEachLine ( by: 12 )
56
57
}
57
58
let subcommandArgs = subcommands. map {
58
59
"""
59
- ( \( $0. _commandName) )
60
- \( functionName) _ \( $0. _commandName)
60
+ ( \( $0. _commandName. zshEscapingCommandName ( ) ) )
61
+ \( functionName) _ \( $0. _commandName. zshEscaped ( ) )
61
62
;;
62
63
"""
63
64
. indentingEachLine ( by: 12 )
@@ -122,6 +123,10 @@ extension String {
122
123
fileprivate func zshEscaped( ) -> String {
123
124
self . zshEscapingSingleQuotes ( ) . zshEscapingMetacharacters ( )
124
125
}
126
+
127
+ fileprivate func zshEscapingCommandName( ) -> String {
128
+ self . replacingOccurrences ( of: " - " , with: " _ " )
129
+ }
125
130
}
126
131
127
132
extension ArgumentDefinition {
@@ -187,7 +192,7 @@ extension ArgumentDefinition {
187
192
188
193
case . custom:
189
194
// Generate a call back into the command to retrieve a completions list
190
- let commandName = commands. first!. _commandName
195
+ let commandName = commands. first!. _commandName. zshEscapingCommandName ( )
191
196
return " {_custom_completion $_ \( commandName) _commandname \( customCompletionCall ( commands) ) $words} "
192
197
}
193
198
}
0 commit comments