1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
struct FishCompletionsGenerator {
static func generateCompletionScript(_ type: ParsableCommand.Type) -> String {
let programName = type._commandName
let helperFunctions = [
preprocessorFunction(commandName: programName),
helperFunction(commandName: programName)
]
let completions = generateCompletions(commandChain: [programName], [type])
return helperFunctions.joined(separator: "\n\n") + "\n\n" + completions.joined(separator: "\n")
}
}
// MARK: - Private functions
extension FishCompletionsGenerator {
private static func generateCompletions(commandChain: [String], _ commands: [ParsableCommand.Type]) -> [String] {
let type = commands.last!
let isRootCommand = commands.count == 1
let programName = commandChain[0]
var subcommands = type.configuration.subcommands
.filter { $0.configuration.shouldDisplay }
if !subcommands.isEmpty && isRootCommand {
subcommands.append(HelpCommand.self)
}
let helperFunctionName = helperFunctionName(commandName: programName)
var prefix = "complete -c \(programName) -n '\(helperFunctionName) \"\(commandChain.joined(separator: separator))\""
if !subcommands.isEmpty {
prefix += " \"\(subcommands.map { $0._commandName }.joined(separator: separator))\""
}
prefix += "'"
func complete(suggestion: String) -> String {
"\(prefix) \(suggestion)"
}
let subcommandCompletions: [String] = subcommands.map { subcommand in
let escapedAbstract = subcommand.configuration.abstract.fishEscape()
let suggestion = "-f -a '\(subcommand._commandName)' -d '\(escapedAbstract)'"
return complete(suggestion: suggestion)
}
let argumentCompletions = commands
.argumentsForHelp(visibility: .default)
.compactMap { $0.argumentSegments(commandChain) }
.map { $0.joined(separator: " ") }
.map { complete(suggestion: $0) }
let completionsFromSubcommands = subcommands.flatMap { subcommand in
generateCompletions(commandChain: commandChain + [subcommand._commandName], [subcommand])
}
return completionsFromSubcommands + argumentCompletions + subcommandCompletions
}
}
extension ArgumentDefinition {
fileprivate func argumentSegments(_ commandChain: [String]) -> [String]? {
guard help.visibility.base == .default,
!names.isEmpty
else { return nil }
var results = names.map{ $0.asFishSuggestion }
if !help.abstract.isEmpty {
results += ["-d '\(help.abstract.fishEscape())'"]
}
if isNullary {
return results
}
switch completion.kind {
case .default: return results
case .list(let list):
return results + ["-r -f -k -a '\(list.joined(separator: " "))'"]
case .file(let extensions):
let pattern = "*.{\(extensions.joined(separator: ","))}"
return results + ["-r -f -a '(for i in \(pattern); echo $i;end)'"]
case .directory:
return results + ["-r -f -a '(__fish_complete_directories)'"]
case .shellCommand(let shellCommand):
return results + ["-r -f -a '(\(shellCommand))'"]
case .custom:
let program = commandChain[0]
let subcommands = commandChain.dropFirst().joined(separator: " ")
return results + ["-r -f -a '(command \(program) ---completion \(subcommands) -- --custom (commandline -opc)[1..-1])'"]
}
}
}
extension Name {
fileprivate var asFishSuggestion: String {
switch self {
case .long(let longName):
return "-l \(longName)"
case .short(let shortName, _):
return "-s \(shortName)"
case .longWithSingleDash(let dashedName):
return "-o \(dashedName)"
}
}
}
extension String {
fileprivate func fishEscape() -> String {
replacingOccurrences(of: "'", with: #"\'"#)
}
}
extension FishCompletionsGenerator {
private static var separator: String { " " }
private static func preprocessorFunctionName(commandName: String) -> String {
"_swift_\(commandName)_preprocessor"
}
private static func preprocessorFunction(commandName: String) -> String {
"""
# A function which filters options which starts with "-" from $argv.
function \(preprocessorFunctionName(commandName: commandName))
set -l results
for i in (seq (count $argv))
switch (echo $argv[$i] | string sub -l 1)
case '-'
case '*'
echo $argv[$i]
end
end
end
"""
}
private static func helperFunctionName(commandName: String) -> String {
"_swift_" + commandName + "_using_command"
}
private static func helperFunction(commandName: String) -> String {
let functionName = helperFunctionName(commandName: commandName)
let preprocessorFunctionName = preprocessorFunctionName(commandName: commandName)
return """
function \(functionName)
set -l currentCommands (\(preprocessorFunctionName) (commandline -opc))
set -l expectedCommands (string split \"\(separator)\" $argv[1])
set -l subcommands (string split \"\(separator)\" $argv[2])
if [ (count $currentCommands) -ge (count $expectedCommands) ]
for i in (seq (count $expectedCommands))
if [ $currentCommands[$i] != $expectedCommands[$i] ]
return 1
end
end
if [ (count $currentCommands) -eq (count $expectedCommands) ]
return 0
end
if [ (count $subcommands) -gt 1 ]
for i in (seq (count $subcommands))
if [ $currentCommands[(math (count $expectedCommands) + 1)] = $subcommands[$i] ]
return 1
end
end
end
return 0
end
return 1
end
"""
}
}
|