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

Try it in the playground →

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.

OptionDefaultswift-format calls it
width100lineLength
max-width
indent.width2indentation.spaces
indent.style
line-ending"lf"
trailing-comma.add"preserve"
trailing-comma.forces-breakfalse
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 = { ... }.

OptionDefaultWhat it does
indentation.tabs0Indents with this many tabs a level in place of indentation.spaces. Takes a number.
tabWidth8Counts a tab as this many columns when measuring a line against lineLength. Takes a number.
maximumBlankLines1Keeps at most this many blank lines in a row. Takes a number.
spacesBeforeEndOfLineComments2Puts this many spaces between code and a // comment that ends its line. Takes a number.
respectsExistingLineBreakstrueKeeps the line breaks written between tokens where breaking is allowed; when false, joins lines that fit. Takes true or false.
lineBreakBeforeControlFlowKeywordsfalsePuts else and catch on a line of their own rather than after the closing brace. Takes true or false.
lineBreakBeforeEachArgumentfalsePuts 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.
lineBreakBeforeEachGenericRequirementfalsePuts 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.
lineBreakBetweenDeclarationAttributesfalsePuts each attribute of a declaration on its own line. Takes true or false.
prioritizeKeepingFunctionOutputTogetherfalseBreaks inside a function's parameters before breaking between its closing parenthesis and its return type or throws. Takes true or false.
indentConditionalCompilationBlockstrueIndents the code between #if, #elseif, #else, and #endif. Takes true or false.
lineBreakAroundMultilineExpressionChainComponentsfalsePuts each .member of a chain on its own line around a component that spans several lines. Takes true or false.
indentSwitchCaseLabelsfalseIndents a switch's case and default labels one level from the switch. Takes true or false.
spacesAroundRangeFormationOperatorsfalseWrites a space on each side of ... and ..<. Takes true or false.
multiElementCollectionTrailingCommastrueAdds 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.
indentBlankLinesfalseIndents 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.includeConditionalImportsfalseSorts the imports inside #if blocks with the rest. Takes true or false.
orderedImports.shouldGroupImportstrueGroups the sorted imports by kind, regular, declaration, @_implementationOnly, and @testable, with a blank line between groups. Takes true or false.
rules.DoNotUseSemicolonstrueRemoves the semicolons that end statements, putting statements a semicolon separated on lines of their own. Takes true or false.
rules.FileScopedDeclarationPrivacytrueWrites file-scoped private or fileprivate declarations with the access level fileScopedDeclarationPrivacy.accessLevel names. Takes true or false.
rules.FullyIndirectEnumtrueMarks an enum indirect where every case with associated values is, and removes indirect from its cases. Takes true or false.
rules.GroupNumericLiteralstrueWrites _ between each group of digits in long integer literals. Takes true or false.
rules.NoAccessLevelOnExtensionDeclarationtrueMoves an access level written on an extension onto each of its members. Takes true or false.
rules.NoAssignmentInExpressionstrueMoves 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.NoCasesWithOnlyFallthroughtrueMerges a case whose body is only fallthrough into the case that follows it. Takes true or false.
rules.NoEmptyLinesOpeningClosingBracesfalseRemoves blank lines just after an opening brace and just before a closing one. Takes true or false.
rules.NoEmptyTrailingClosureParenthesestrueRemoves the empty () before a trailing closure. Takes true or false.
rules.NoLabelsInCasePatternstrueRemoves the labels that repeat the names a case pattern binds, writing .a(let x) for .a(x: let x). Takes true or false.
rules.NoParensAroundConditionstrueRemoves the parentheses around the condition of an if, guard, while, or switch. Takes true or false.
rules.NoVoidReturnOnFunctionSignaturetrueRemoves -> Void and -> () from function signatures. Takes true or false.
rules.OmitExplicitReturnsfalseRemoves return from a function, getter, or closure whose body is one expression. Takes true or false.
rules.OneCasePerLinetruePuts each enum case that has associated values or a raw value on a case of its own. Takes true or false.
rules.OneVariableDeclarationPerLinetrueSplits a let or var that declares several variables into one declaration each. Takes true or false.
rules.OrderedImportstrueSorts the imports, as orderedImports.shouldGroupImports and orderedImports.includeConditionalImports say. Takes true or false.
rules.ReturnVoidInsteadOfEmptyTupletrueWrites Void in place of () as a function type's return type. Takes true or false.
rules.UseEarlyExitsfalseRewrites an if whose else ends in return, throw, break, or continue as a guard. Takes true or false.
rules.UseExplicitNilCheckInConditionstrueWrites x != nil in place of let _ = x in a condition. Takes true or false.
rules.UseShorthandTypeNamestrueWrites [T], [K: V], and T? in place of Array<T>, Dictionary<K, V>, and Optional<T>. Takes true or false.
rules.UseSingleLinePropertyGettertrueRemoves the get { } around a read-only computed property's body. Takes true or false.
rules.UseTripleSlashForDocumentationCommentstrueWrites documentation comments as /// lines in place of /** */ blocks. Takes true or false.
rules.UseWhereClausesInForLoopsfalseMoves an if that is the whole body of a for loop into the loop's where clause. Takes true or false.