Skip to content

Enable swift-format checking #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 15 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ on:
types: [opened, reopened, synchronize]

jobs:
validate_format_config:
name: Validate Format Config
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Install apt dependencies
run: sudo apt-get -qq update && sudo apt-get -qq -y install curl

- name: Compare against swift-mmio swift-format config
run: |
curl -sL https://raw.githubusercontent.com/apple/swift-mmio/refs/heads/main/.swift-format -o .swift-format-mmio
diff .swift-format .swift-format-mmio

soundness:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
with:
format_check_enabled: false # bug: https://github.com/apple/swift-argument-parser/issues/702
license_header_check_enabled: false # feature: https://github.com/swiftlang/github-workflows/issues/78
license_header_check_project_name: "Swift Argument Parser" # bug: https://github.com/swiftlang/github-workflows/issues/76
shell_check_enabled: false # bug: https://github.com/apple/swift-argument-parser/issues/703
71 changes: 71 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"indentation" : {
"spaces" : 2
},
"indentConditionalCompilationBlocks" : false,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 80,
"maximumBlankLines" : 1,
"multiElementCollectionTrailingCommas" : true,
"noAssignmentInExpressions" : {
"allowedFunctions" : [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : true,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : false,
"BeginDocumentationCommentWithOneLineSummary" : true,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : true,
"NeverUseForceTry" : true,
"NeverUseImplicitlyUnwrappedOptionals" : true,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoPlaygroundLiterals" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"TypeNamesShouldBeCapitalized" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : true,
"UseLetInEveryBoundCaseVariable" : true,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : true,
"UseSynthesizedInitializer" : true,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : true
},
"spacesBeforeEndOfLineComments": 2,
"spacesAroundRangeFormationOperators" : false,
"tabWidth" : 2,
"version" : 1
}
3 changes: 2 additions & 1 deletion Examples/color/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct Color: ParsableCommand {
@Option(help: "Your favorite color.")
var fav: ColorOptions

@Option(help: .init("Your second favorite color.", discussion: "This is optional."))
@Option(
help: .init("Your second favorite color.", discussion: "This is optional."))
var second: ColorOptions?

func run() {
Expand Down
93 changes: 46 additions & 47 deletions Examples/count-lines/CountLines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,57 @@ import Foundation
@main
@available(macOS 12, *)
struct CountLines: AsyncParsableCommand {
@Argument(
help: "A file to count lines in. If omitted, counts the lines of stdin.",
completion: .file(), transform: URL.init(fileURLWithPath:))
var inputFile: URL? = nil
@Option(help: "Only count lines with this prefix.")
var prefix: String? = nil
@Flag(help: "Include extra information in the output.")
var verbose = false
@Argument(
help: "A file to count lines in. If omitted, counts the lines of stdin.",
completion: .file(), transform: URL.init(fileURLWithPath:))
var inputFile: URL? = nil

@Option(help: "Only count lines with this prefix.")
var prefix: String? = nil

@Flag(help: "Include extra information in the output.")
var verbose = false
}

@available(macOS 12, *)
extension CountLines {
var fileHandle: FileHandle {
get throws {
guard let inputFile else {
return .standardInput
}
return try FileHandle(forReadingFrom: inputFile)
}
var fileHandle: FileHandle {
get throws {
guard let inputFile else {
return .standardInput
}
return try FileHandle(forReadingFrom: inputFile)
}

func printCount(_ count: Int) {
guard verbose else {
print(count)
return
}

if let filename = inputFile?.lastPathComponent {
print("Lines in '\(filename)'", terminator: "")
} else {
print("Lines from stdin", terminator: "")
}

if let prefix {
print(", prefixed by '\(prefix)'", terminator: "")
}

print(": \(count)")
}

func printCount(_ count: Int) {
guard verbose else {
print(count)
return
}

if let filename = inputFile?.lastPathComponent {
print("Lines in '\(filename)'", terminator: "")
} else {
print("Lines from stdin", terminator: "")
}

mutating func run() async throws {
let countAllLines = prefix == nil
let lineCount = try await fileHandle.bytes.lines.reduce(0) { count, line in
if countAllLines || line.starts(with: prefix!) {
return count + 1
} else {
return count
}
}

printCount(lineCount)

if let prefix {
print(", prefixed by '\(prefix)'", terminator: "")
}

print(": \(count)")
}

mutating func run() async throws {
var lineCount = 0
for try await line in try fileHandle.bytes.lines {
if let prefix {
lineCount += line.starts(with: prefix) ? 1 : 0
} else {
lineCount += 1
}
}
printCount(lineCount)
}
}
Loading