@@ -11,109 +11,110 @@ internal object FishCompletionGenerator {
11
11
}
12
12
13
13
14
- private fun generateFishCompletionForCommand (command : BaseCliktCommand <* >): String = buildString {
15
- val parentCommandName = command.currentContext.parentNames().lastOrNull()
16
- val rootCommandName = command.currentContext.commandNameWithParents().first()
17
- val isTopLevel = parentCommandName == null
18
- val commandName = command.commandName
19
- val options = command._options .filterNot { it.hidden }
20
- val arguments = command._arguments
21
- val subcommands = command._subcommands
22
- val hasSubcommands = subcommands.isNotEmpty()
23
- val subcommandsVarName =
24
- command.currentContext.commandNameWithParents().subcommandsVarName()
25
- val parentSubcommandsVarName = when {
26
- isTopLevel -> subcommandsVarName
27
- else -> command.currentContext.parentNames().subcommandsVarName()
28
- }
14
+ private fun generateFishCompletionForCommand (command : BaseCliktCommand <* >): String =
15
+ buildString {
16
+ val parentCommandName = command.currentContext.parentNames().lastOrNull()
17
+ val rootCommandName = command.currentContext.commandNameWithParents().first()
18
+ val isTopLevel = parentCommandName == null
19
+ val commandName = command.commandName
20
+ val options = command._options .filterNot { it.hidden }
21
+ val arguments = command._arguments
22
+ val subcommands = command._subcommands
23
+ val hasSubcommands = subcommands.isNotEmpty()
24
+ val subcommandsVarName =
25
+ command.currentContext.commandNameWithParents().subcommandsVarName()
26
+ val parentSubcommandsVarName = when {
27
+ isTopLevel -> subcommandsVarName
28
+ else -> command.currentContext.parentNames().subcommandsVarName()
29
+ }
29
30
30
- if (isTopLevel) {
31
- appendLine(
32
- """
31
+ if (isTopLevel) {
32
+ appendLine(
33
+ """
33
34
|# Command completion for $commandName
34
35
|# Generated by Clikt
35
36
""" .trimMargin()
36
- )
37
- }
37
+ )
38
+ }
38
39
39
- if (hasSubcommands || ! isTopLevel) {
40
- appendLine(" \n\n ### Setup for $commandName " )
41
- }
40
+ if (hasSubcommands || ! isTopLevel) {
41
+ appendLine(" \n\n ### Setup for $commandName " )
42
+ }
42
43
43
- if (hasSubcommands) {
44
- val subcommandsStr = subcommands.joinToString(" " ) { it.commandName }
45
- appendLine(" set -l $subcommandsVarName '$subcommandsStr '" )
46
- }
44
+ if (hasSubcommands) {
45
+ val subcommandsStr = subcommands.joinToString(" " ) { it.commandName }
46
+ appendLine(" set -l $subcommandsVarName '$subcommandsStr '" )
47
+ }
47
48
48
- if (! isTopLevel) {
49
- append(" complete -c $rootCommandName -f " )
49
+ if (! isTopLevel) {
50
+ append(" complete -c $rootCommandName -f " )
50
51
51
- if (rootCommandName == parentCommandName) {
52
- append(" -n __fish_use_subcommand " )
53
- } else {
54
- append(" -n \" __fish_seen_subcommand_from $parentCommandName ; and not __fish_seen_subcommand_from \$ $parentSubcommandsVarName \" " )
55
- }
52
+ if (rootCommandName == parentCommandName) {
53
+ append(" -n __fish_use_subcommand " )
54
+ } else {
55
+ append(" -n \" __fish_seen_subcommand_from $parentCommandName ; and not __fish_seen_subcommand_from \$ $parentSubcommandsVarName \" " )
56
+ }
57
+
58
+ append(" -a $commandName " )
56
59
57
- append(" -a $commandName " )
60
+ val help = command.help(command.currentContext).replace(" '" , " \\ '" )
61
+ if (help.isNotBlank()) {
62
+ append(" -d '${help} '" )
63
+ }
58
64
59
- val help = command.help(command.currentContext).replace(" '" , " \\ '" )
60
- if (help.isNotBlank()) {
61
- append(" -d '${help} '" )
65
+ appendLine()
62
66
}
63
67
64
- appendLine()
65
- }
68
+ if (options.any { o -> o.allNames.any { it.isValidFishCompletionOption } }) {
69
+ appendLine(" \n ## Options for $commandName " )
70
+ }
66
71
67
- if (options.any { o -> o.allNames.any { it.isValidFishCompletionOption } }) {
68
- appendLine(" \n ## Options for $commandName " )
69
- }
72
+ for (option in options) {
73
+ val names = option.allNames.filter { it.isValidFishCompletionOption }
74
+ if (names.isEmpty()) {
75
+ continue
76
+ }
70
77
71
- for (option in options) {
72
- val names = option.allNames.filter { it.isValidFishCompletionOption }
73
- if (names.isEmpty()) {
74
- continue
75
- }
78
+ appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
76
79
77
- appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
80
+ for (name in names) {
81
+ append(' ' )
82
+ when {
83
+ name.startsWith(" --" ) -> append(" -l " )
84
+ name.length == 2 -> append(" -s " )
85
+ else -> append(" -o " )
86
+ }
87
+ append(name.trimStart(' -' ))
88
+ }
78
89
79
- for (name in names) {
80
- append(' ' )
81
- when {
82
- name.startsWith(" --" ) -> append(" -l " )
83
- name.length == 2 -> append(" -s " )
84
- else -> append(" -o " )
90
+ if (option.nvalues.first > 0 ) {
91
+ append(" -r" )
85
92
}
86
- append(name.trimStart(' -' ))
87
- }
88
93
89
- if (option.nvalues.first > 0 ) {
90
- append(" -r" )
94
+ appendParamCompletion(option.completionCandidates)
95
+ appendHelp(option.optionHelp(command.currentContext))
96
+ appendLine()
91
97
}
92
98
93
- appendParamCompletion(option.completionCandidates)
94
- appendHelp(option.optionHelp(command.currentContext))
95
- appendLine()
96
- }
97
-
98
- if (arguments.isNotEmpty()) {
99
- appendLine(" \n ## Arguments for $commandName " )
100
- }
99
+ if (arguments.isNotEmpty()) {
100
+ appendLine(" \n ## Arguments for $commandName " )
101
+ }
101
102
102
- for (argument in arguments) {
103
- appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
104
- appendParamCompletion(argument.completionCandidates)
105
- appendHelp(argument.getArgumentHelp(command.currentContext))
106
- appendLine()
107
- }
103
+ for (argument in arguments) {
104
+ appendCompleteCall(rootCommandName, isTopLevel, hasSubcommands, commandName)
105
+ appendParamCompletion(argument.completionCandidates)
106
+ appendHelp(argument.getArgumentHelp(command.currentContext))
107
+ appendLine()
108
+ }
108
109
109
- for (subcommand in subcommands) {
110
- append(
111
- generateFishCompletionForCommand(
112
- command = subcommand
110
+ for (subcommand in subcommands) {
111
+ append(
112
+ generateFishCompletionForCommand(
113
+ command = subcommand
114
+ )
113
115
)
114
- )
116
+ }
115
117
}
116
- }
117
118
118
119
private fun StringBuilder.appendCompleteCall (
119
120
rootCommandName : String ,
0 commit comments