Skip to content

Commit fc96cbe

Browse files
committed
chore: resolve merge conflicts with upstream/main
2 parents fa6f077 + fb08c7b commit fc96cbe

37 files changed

+1480
-400
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
2626
with:
2727
windows_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}]"
28+
enable_macos_checks: true
2829

2930
cmake-build:
3031
name: CMake Build

Examples/math/Math.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,17 @@ extension Math.Statistics {
198198
var oneOfFour: String?
199199

200200
@Argument(
201-
completion: .custom { _ in ["alabaster", "breakfast", "crunch", "crash"] }
201+
completion: .custom { _, _, _ in
202+
["alabaster", "breakfast", "crunch", "crash"]
203+
}
202204
)
203205
var customArg: String?
204206

207+
@Argument(
208+
completion: .custom { _ in ["alabaster", "breakfast", "crunch", "crash"] }
209+
)
210+
var customDeprecatedArg: String?
211+
205212
@Argument(help: "A group of floating-point values to operate on.")
206213
var values: [Double] = []
207214

@@ -222,12 +229,16 @@ extension Math.Statistics {
222229
var directory: String?
223230

224231
@Option(
225-
completion: .shellCommand("head -100 /usr/share/dict/words | tail -50"))
232+
completion: .shellCommand("head -100 /usr/share/dict/words | tail -50")
233+
)
226234
var shell: String?
227235

228236
@Option(completion: .custom(customCompletion))
229237
var custom: String?
230238

239+
@Option(completion: .custom(customDeprecatedCompletion))
240+
var customDeprecated: String?
241+
231242
func validate() throws {
232243
if testSuccessExitCode {
233244
throw ExitCode.success
@@ -248,7 +259,13 @@ extension Math.Statistics {
248259
}
249260
}
250261

251-
func customCompletion(_ s: [String]) -> [String] {
262+
func customCompletion(_ s: [String], _: Int, _: Int) -> [String] {
263+
(s.last ?? "").starts(with: "a")
264+
? ["aardvark", "aaaaalbert"]
265+
: ["hello", "helicopter", "heliotrope"]
266+
}
267+
268+
func customDeprecatedCompletion(_ s: [String]) -> [String] {
252269
(s.last ?? "").starts(with: "a")
253270
? ["aardvark", "aaaaalbert"]
254271
: ["hello", "helicopter", "heliotrope"]

Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ extension [ParsableCommand.Type] {
2121
return """
2222
#!/bin/bash
2323
24+
\(cursorIndexInCurrentWordFunctionName)() {
25+
local remaining="${COMP_LINE}"
26+
27+
local word
28+
for word in "${COMP_WORDS[@]::COMP_CWORD}"; do
29+
remaining="${remaining##*([[:space:]])"${word}"*([[:space:]])}"
30+
done
31+
32+
local -ir index="$((COMP_POINT - ${#COMP_LINE} + ${#remaining}))"
33+
if [[ "${index}" -le 0 ]]; then
34+
printf 0
35+
else
36+
printf %s "${index}"
37+
fi
38+
}
39+
2440
# positional arguments:
2541
#
2642
# - 1: the current (sub)command's count of positional arguments
@@ -365,6 +381,16 @@ extension [ParsableCommand.Type] {
365381
"""
366382

367383
case .custom:
384+
// Generate a call back into the command to retrieve a completions list
385+
return """
386+
\(addCompletionsFunctionName) -W\
387+
"$(\(customCompleteFunctionName) \(arg.customCompletionCall(self))\
388+
"${COMP_CWORD}"\
389+
"$(\(cursorIndexInCurrentWordFunctionName))")"
390+
391+
"""
392+
393+
case .customDeprecated:
368394
// Generate a call back into the command to retrieve a completions list
369395
return """
370396
\(addCompletionsFunctionName) -W\
@@ -374,6 +400,10 @@ extension [ParsableCommand.Type] {
374400
}
375401
}
376402

403+
private var cursorIndexInCurrentWordFunctionName: String {
404+
"_\(prefix(1).completionFunctionName().shellEscapeForVariableName())_cursor_index_in_current_word"
405+
}
406+
377407
private var offerFlagsOptionsFunctionName: String {
378408
"_\(prefix(1).completionFunctionName().shellEscapeForVariableName())_offer_flags_options"
379409
}

Sources/ArgumentParser/Completions/CompletionsGenerator.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct CompletionsGenerator {
138138
case .bash:
139139
return [command].bashCompletionScript
140140
case .fish:
141-
return FishCompletionsGenerator.generateCompletionScript(command)
141+
return [command].fishCompletionScript
142142
default:
143143
fatalError("Invalid CompletionShell: \(shell)")
144144
}
@@ -149,12 +149,13 @@ extension ArgumentDefinition {
149149
/// Returns a string with the arguments for the callback to generate custom completions for
150150
/// this argument.
151151
func customCompletionCall(_ commands: [ParsableCommand.Type]) -> String {
152-
let subcommandNames = commands.dropFirst().map { $0._commandName }.joined(
153-
separator: " ")
152+
let subcommandNames =
153+
commands.dropFirst().map { "\($0._commandName) " }.joined()
154154
let argumentName =
155155
names.preferredName?.synopsisString
156-
?? self.help.keys.first?.fullPathString ?? "---"
157-
return "---completion \(subcommandNames) -- \(argumentName)"
156+
?? self.help.keys.first?.fullPathString
157+
?? "---"
158+
return "---completion \(subcommandNames)-- \(argumentName)"
158159
}
159160
}
160161

0 commit comments

Comments
 (0)