Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5f5446b
Do not indent zsh cases.
rgoldberg Jan 16, 2025
e22f251
Do not prefix zsh cases with an open parenthesis.
rgoldberg Jan 16, 2025
beaa419
Prevent zsh parameter word splitting.
rgoldberg Jan 16, 2025
480f772
Improve comment in ZshCompletionsGenerator.swift.
rgoldberg Jan 16, 2025
0eae18f
Fix incorrect zsh shellCommand single quotes:
rgoldberg Jan 16, 2025
b4b492a
Remove extraneous zsh newline.
rgoldberg Jan 16, 2025
dbbb99e
Improve zsh variable declarations: scoping, typing & readonly.
rgoldberg Jan 16, 2025
69f2d73
Include zsh words before current subcommand in custom completion arg.
rgoldberg Jan 27, 2025
17012b5
Make subcommandHandler in ZshCompletionsGenerator.swift immutable.
rgoldberg Jan 16, 2025
a6fd712
Escape zsh single quotes via '\'' instead of via '"'"'.
rgoldberg Jan 18, 2025
7df50be
Escape single quotes in zsh shellCommand String.
rgoldberg Jan 18, 2025
ea1047f
Fix zsh custom completions for empty [String] & String elements.
rgoldberg Jan 17, 2025
ded075d
Simplify zsh subcommand completion function dispatch.
rgoldberg Jan 21, 2025
28a6b12
Restrict access to symbols in ZshCompletionsGenerator.swift.
rgoldberg Jan 18, 2025
33fbfbc
Add default help to zsh completions iff no existing help subcommand.
rgoldberg Jan 21, 2025
72eb64c
Use interpolated Strings in ZshCompletionsGenerator.swift.
rgoldberg Jan 21, 2025
83fa27c
Create & use zsh __completion function.
rgoldberg Jan 21, 2025
0f8b5ac
Improve zsh escaping.
rgoldberg Jan 24, 2025
d278fd1
Set zsh settings to a known state.
rgoldberg Jan 23, 2025
1d6e52c
Inline single-use functions & variables in ZshCompletionsGenerator.sw…
rgoldberg Jan 28, 2025
fc8b1d5
Overhaul ZshCompletionsGenerator.swift as [ParsableCommand.Type] exte…
rgoldberg Jan 28, 2025
afecd9c
Move functions in ZshCompletionsGenerator.swift.
rgoldberg Jan 29, 2025
a2743b1
Move zsh helper functions before command functions to mirror other sh…
rgoldberg Jan 29, 2025
ea62664
Prefix zsh helper functions with command name to prevent naming clashes.
rgoldberg Jan 29, 2025
15bbfa0
Separate zsh _arguments flags from specs using :.
rgoldberg Jan 30, 2025
f87085c
Rename zsh args variable as arg_specs.
rgoldberg Jan 30, 2025
697e5e8
Simplify zshCompletionString(…).
rgoldberg Jan 30, 2025
c6d1375
Allow generating zsh setup scripts for arguments.
rgoldberg Jan 30, 2025
e8ca77d
Use zsh array for list completions instead of nested strings.
rgoldberg Jan 30, 2025
8583ab6
Make CompletionShell.format(…) internal instead of public.
rgoldberg Feb 14, 2025
ecb43b9
Reword uses of "iff" in completions code.
rgoldberg Feb 14, 2025
19e4df1
Replace zsh END_MARKER pseudo-completion with a space to ease migration.
rgoldberg Feb 14, 2025
6d32ef3
Throw error if attempting to generate a zsh completion script for no …
rgoldberg Feb 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Sources/ArgumentParser/Completions/CompletionsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ public struct CompletionShell: RawRepresentable, Hashable, CaseIterable {
///
/// The environment variable is set in generated completion scripts.
static let shellVersionEnvironmentVariableName = "SAP_SHELL_VERSION"

func format(completions: [String]) -> String {
var completions = completions
if self == .zsh {
// This pseudo-completion is removed by the zsh completion script.
// It allows trailing empty string completions to work in zsh.
// zsh completion scripts generated by older SAP versions ignore spaces.
completions.append(" ")
}
return completions.joined(separator: "\n")
}
}

struct CompletionsGenerator {
Expand Down Expand Up @@ -129,7 +140,7 @@ struct CompletionsGenerator {
CompletionShell._requesting.withLock { $0 = shell }
switch shell {
case .zsh:
return ZshCompletionsGenerator.generateCompletionScript(command)
return [command].zshCompletionScript
case .bash:
return BashCompletionsGenerator.generateCompletionScript(command)
case .fish:
Expand Down Expand Up @@ -164,11 +175,40 @@ extension ParsableCommand {
}
}

extension [ParsableCommand.Type] {
/// Include default 'help' subcommand in nonempty subcommand list if & only if
/// no help subcommand already exists.
mutating func addHelpSubcommandIfMissing() {
if !isEmpty && allSatisfy({ $0._commandName != "help" }) {
append(HelpCommand.self)
}
}
}

extension Sequence where Element == ParsableCommand.Type {
func completionFunctionName() -> String {
"_"
+ self.flatMap { $0.compositeCommandName }
.uniquingAdjacentElements()
.joined(separator: "_")
}

var shellVariableNamePrefix: String {
flatMap { $0.compositeCommandName }
.joined(separator: "_")
.shellEscapeForVariableName()
}
}

extension String {
func shellEscapeForSingleQuotedString(iterationCount: UInt64 = 1) -> Self {
iterationCount == 0
? self
: replacingOccurrences(of: "'", with: "'\\''")
.shellEscapeForSingleQuotedString(iterationCount: iterationCount - 1)
}

func shellEscapeForVariableName() -> Self {
replacingOccurrences(of: "-", with: "_")
}
}
Loading