swift-format
- Formats
- Swift
- In
ur.toml [format.swift] style = "swift-format"- Its options
- https://github.com/swiftlang/swift-format/blob/main/Documentation/Configuration.md
Formatted
Cart.swift, as written// What a cart costs.
struct Item {
let name: String
let price: Int
let quantity: Int
}
func subtotal(_ items: [Item]) -> Int {
var sum = 0
for item in items where item.quantity > 0 {
sum += item.price * item.quantity
}
return sum
}
let codes = [
"SPRING",
"WELCOME"
]
let range = 0 ..< codes.count
ur format --style swift-format Cart.swift// What a cart costs.
struct Item {
let name: String
let price: Int
let quantity: Int
}
func subtotal(_ items: [Item]) -> Int {
var sum = 0
for item in items where item.quantity > 0 {
sum += item.price * item.quantity
}
return sum
}
let codes = [
"SPRING",
"WELCOME",
]
let range = 0..<codes.count
Defaults
The shared options, as swift-format sets them when nothing configures it. Its own names for them are read from its configuration files, but ur.toml sets them by the shared name only.
| Option | Default | swift-format calls it |
|---|---|---|
width | 100 | lineLength |
max-width | ||
indent.width | 2 | indentation.spaces |
indent.style | ||
line-ending | "lf" | |
trailing-comma.add | "preserve" | |
trailing-comma.forces-break | false | |
quotes | "preserve" | |
target |
Its own options
Set in a table named for it, such as [format.swift-format], or in a language's table, as swift-format = { ... }.
| Option | Default | What it does |
|---|---|---|
indentation.tabs | 0 | Indents with this many tabs a level in place of indentation.spaces. Takes a number. |
tabWidth | 8 | Counts a tab as this many columns when measuring a line against lineLength. Takes a number. |
maximumBlankLines | 1 | Keeps at most this many blank lines in a row. Takes a number. |
spacesBeforeEndOfLineComments | 2 | Puts this many spaces between code and a // comment that ends its line. Takes a number. |
respectsExistingLineBreaks | true | Keeps the line breaks written between tokens where breaking is allowed; when false, joins lines that fit. Takes true or false. |
lineBreakBeforeControlFlowKeywords | false | Puts else and catch on a line of their own rather than after the closing brace. Takes true or false. |
lineBreakBeforeEachArgument | false | Puts every argument of a call or parameter of a declaration on its own line once the list does not fit on one. Takes true or false. |
lineBreakBeforeEachGenericRequirement | false | Puts every requirement of a where clause or generic parameter list on its own line once the list does not fit on one. Takes true or false. |
lineBreakBetweenDeclarationAttributes | false | Puts each attribute of a declaration on its own line. Takes true or false. |
prioritizeKeepingFunctionOutputTogether | false | Breaks inside a function's parameters before breaking between its closing parenthesis and its return type or throws. Takes true or false. |
indentConditionalCompilationBlocks | true | Indents the code between #if, #elseif, #else, and #endif. Takes true or false. |
lineBreakAroundMultilineExpressionChainComponents | false | Puts each .member of a chain on its own line around a component that spans several lines. Takes true or false. |
indentSwitchCaseLabels | false | Indents a switch's case and default labels one level from the switch. Takes true or false. |
spacesAroundRangeFormationOperators | false | Writes a space on each side of ... and ..<. Takes true or false. |
multiElementCollectionTrailingCommas | true | Adds a trailing comma after the last element of an array or dictionary literal that spans lines, and removes it from one on one line. Takes true or false. |
indentBlankLines | false | Indents blank lines to the level of the code around them rather than leaving them empty. Takes true or false. |
multilineTrailingCommaBehavior | "keptAsWritten" | Whether a list that spans lines always gets a trailing comma, never does, or keeps what it has. Takes "alwaysUsed", "neverUsed", "keptAsWritten". |
reflowMultilineStringLiterals | "never" | Whether multiline string literals are reflowed never, only where a line is longer than lineLength, or always. Takes "never", "onlyLinesOverLength", "always". |
fileScopedDeclarationPrivacy.accessLevel | "private" | The access level FileScopedDeclarationPrivacy gives file-scoped declarations: private or fileprivate. Takes "private", "fileprivate". |
noAssignmentInExpressions.allowedFunctions | ["XCTAssertNoThrow"] | The functions whose arguments NoAssignmentInExpressions allows to hold an assignment. Takes a list of strings. |
orderedImports.includeConditionalImports | false | Sorts the imports inside #if blocks with the rest. Takes true or false. |
orderedImports.shouldGroupImports | true | Groups the sorted imports by kind, regular, declaration, @_implementationOnly, and @testable, with a blank line between groups. Takes true or false. |
rules.DoNotUseSemicolons | true | Removes the semicolons that end statements, putting statements a semicolon separated on lines of their own. Takes true or false. |
rules.FileScopedDeclarationPrivacy | true | Writes file-scoped private or fileprivate declarations with the access level fileScopedDeclarationPrivacy.accessLevel names. Takes true or false. |
rules.FullyIndirectEnum | true | Marks an enum indirect where every case with associated values is, and removes indirect from its cases. Takes true or false. |
rules.GroupNumericLiterals | true | Writes _ between each group of digits in long integer literals. Takes true or false. |
rules.NoAccessLevelOnExtensionDeclaration | true | Moves an access level written on an extension onto each of its members. Takes true or false. |
rules.NoAssignmentInExpressions | true | Moves an assignment out of an expression it is nested in, such as a return, onto a statement of its own. Takes true or false. |
rules.NoCasesWithOnlyFallthrough | true | Merges a case whose body is only fallthrough into the case that follows it. Takes true or false. |
rules.NoEmptyLinesOpeningClosingBraces | false | Removes blank lines just after an opening brace and just before a closing one. Takes true or false. |
rules.NoEmptyTrailingClosureParentheses | true | Removes the empty () before a trailing closure. Takes true or false. |
rules.NoLabelsInCasePatterns | true | Removes the labels that repeat the names a case pattern binds, writing .a(let x) for .a(x: let x). Takes true or false. |
rules.NoParensAroundConditions | true | Removes the parentheses around the condition of an if, guard, while, or switch. Takes true or false. |
rules.NoVoidReturnOnFunctionSignature | true | Removes -> Void and -> () from function signatures. Takes true or false. |
rules.OmitExplicitReturns | false | Removes return from a function, getter, or closure whose body is one expression. Takes true or false. |
rules.OneCasePerLine | true | Puts each enum case that has associated values or a raw value on a case of its own. Takes true or false. |
rules.OneVariableDeclarationPerLine | true | Splits a let or var that declares several variables into one declaration each. Takes true or false. |
rules.OrderedImports | true | Sorts the imports, as orderedImports.shouldGroupImports and orderedImports.includeConditionalImports say. Takes true or false. |
rules.ReturnVoidInsteadOfEmptyTuple | true | Writes Void in place of () as a function type's return type. Takes true or false. |
rules.UseEarlyExits | false | Rewrites an if whose else ends in return, throw, break, or continue as a guard. Takes true or false. |
rules.UseExplicitNilCheckInConditions | true | Writes x != nil in place of let _ = x in a condition. Takes true or false. |
rules.UseShorthandTypeNames | true | Writes [T], [K: V], and T? in place of Array<T>, Dictionary<K, V>, and Optional<T>. Takes true or false. |
rules.UseSingleLinePropertyGetter | true | Removes the get { } around a read-only computed property's body. Takes true or false. |
rules.UseTripleSlashForDocumentationComments | true | Writes documentation comments as /// lines in place of /** */ blocks. Takes true or false. |
rules.UseWhereClausesInForLoops | false | Moves an if that is the whole body of a for loop into the loop's where clause. Takes true or false. |