1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
# `swift-format` Configuration
`swift-format` allows users to configure a subset of its behavior, both when
used as a command line tool or as an API.
## Command Line Configuration
A `swift-format` configuration file is a JSON file with the following
top-level keys and values:
### `version`
**type:** number
**description:** The version of the configuration file. For now, this should always be `1`.
**default:** `1`
---
### `lineLength`
**type:** number
**description:** The maximum allowed length of a line, in characters.
**default:** `100`
---
### `indentation`
**type:** object
**description:** The kind and amount of whitespace that should be added when indenting one level. The object value of this property should have exactly one of the following properties:
- `spaces` _(number)_: One level of indentation is the given number of spaces.
- `tabs` _(number)_: One level of indentation is the given number of tabs.
**default:** `{ "spaces": 2 }`
---
### `tabWidth`
**type:** number
**description:** The number of spaces that should be considered equivalent to one tab character. This is used during line length calculations when tabs are used for indentation.
**default:** `8`
---
### `maximumBlankLines`
**type:** number
**description:** The maximum number of consecutive blank lines that are allowed to be present in a source file. Any number larger than this will be collapsed down to the maximum.
**default:** `1`
---
### `spacesBeforeEndOfLineComments`
**type:** number
**description:** The number of spaces between the last token on a non-empty line and a line comment starting with `//`.
**default:** `2`
---
### `respectsExistingLineBreaks`
**type:** boolean
**description:** Indicates whether or not existing line breaks in the source code should be honored (if they are valid according to the style guidelines being enforced). If this settings is `false`, then the formatter will be more "opinionated" by only inserting line breaks where absolutely necessary and removing any others, effectively canonicalizing the output.
**default:** `true`
---
### `lineBreakBeforeControlFlowKeywords`
**type:** boolean
**description:** Determines the line-breaking behavior for control flow keywords that follow a closing brace, like `else` and `catch`. If true, a line break will be added before the keyword, forcing it onto its own line. If `false`, the keyword will be placed after the closing brace (separated by a space).
**default:** `false`
---
### `lineBreakBeforeEachArgument`
**type:** boolean
**description:** Determines the line-breaking behavior for generic arguments and function arguments when a declaration is wrapped onto multiple lines. If true, a line break will be added before each argument, forcing the entire argument list to be laid out vertically. If `false`, arguments will be laid out horizontally first, with line breaks only being fired when the line length would be exceeded.
**default:** `false`
---
### `lineBreakBeforeEachGenericRequirement`
**type:** boolean
**description:** Determines the line-breaking behavior for generic requirements when the requirements list is wrapped onto multiple lines. If true, a line break will be added before each requirement, forcing the entire requirements list to be laid out vertically. If `false`, requirements will be laid out horizontally first, with line breaks only being fired when the line length would be exceeded.
**default:** `false`
---
### `lineBreakBetweenDeclarationAttributes`
**type:** boolean
**description:** Determines the line-breaking behavior for adjacent attributes on declarations. If true, a line break will be added between each attribute, forcing the attribute list to be laid out vertically. If `false`, attributes will be laid out horizontally first, with line breaks only being fired when the line length would be exceeded.
**default:** `false`
---
### `prioritizeKeepingFunctionOutputTogether`
**type:** boolean
**description:** Determines if function-like declaration outputs should be prioritized to be together with thefunction signature right (closing) parenthesis. If `false`, function output (i.e. throws, return type) is not prioritized to be together with the signature's right parenthesis, and when the line length would be exceeded,a line break will be fired after the function signature first, indenting the declaration output one additional level. If true, A line break will be fired further up in the function's declaration (e.g. generic parameters, parameters) before breaking on the function's output.
**default:** `false`
---
### `indentConditionalCompilationBlocks`
**type:** boolean
**description:** Determines if conditional compilation blocks are indented. If this setting is `false` the body of `#if`, `#elseif`, and `#else` is not indented.
**default:** `true`
---
### `lineBreakAroundMultilineExpressionChainComponents`
**type:** boolean
**description:** Determines whether line breaks should be forced before and after multiline components of dot-chained expressions, such as function calls and subscripts chained together through member access (i.e. "." expressions). When any component is multiline and this option is true, a line break is forced before the "." of the component and after the component's closing delimiter (i.e. right paren, right bracket, right brace, etc.).
**default:** `false`
---
## `fileScopedDeclarationPrivacy`
**type:** object
**description:** Declarations at file scope with effective private access should be consistently declared as either `fileprivate` or `private`, determined by configuration.
- `accessLevel` _(string)_: The formal access level to use when encountering a file-scoped declaration with effective private access. Allowed values are `private` and `fileprivate`.
**default:** `{ "accessLevel" : "private" }`
---
### `indentSwitchCaseLabels`
**type:** boolean
**description:** Determines if `case` statements should be indented compared to the containing `switch` block.
When `false`, the correct form is:
```swift
switch someValue {
case someCase:
someStatement
...
}
```
When `true`, the correct form is:
```swift
switch someValue {
case someCase:
someStatement
...
}
```
**default:** `false`
---
### `spacesAroundRangeFormationOperators`
**type:** boolean
**description:** Determines whether whitespace should be forced before and after the range formation operators `...` and `..<`.
**default:** `false`
---
### `noAssignmentInExpressions`
**type:** object
**description:** Assignment expressions must be their own statements. Assignment should not be used in an expression context that expects a `Void` value. For example, assigning a variable within a `return` statement existing a `Void` function is prohibited.
- `allowedFunctions` _(strings array)_: A list of function names where assignments are allowed to be embedded in expressions that are passed as parameters to that function.
**default:** `{ "allowedFunctions" : ["XCTAssertNoThrow"] }`
---
### `multiElementCollectionTrailingCommas`
**type:** boolean
**description:** Determines whether multi-element collection literals should have trailing commas.
**default:** `true`
---
### `reflowMultilineStringLiterals`
**type:** `string`
**description:** Determines how multiline string literals should reflow when formatted.
- `never`: Never reflow multiline string literals.
- `onlyLinesOverLength`: Reflow lines in string literal that exceed the maximum line length.
For example with a line length of 10:
```swift
"""
an escape\
line break
a hard line break
"""
```
will be formatted as:
```swift
"""
an esacpe\
line break
a hard \
line break
"""
```
- `always`: Always reflow multiline string literals, this will ignore existing escaped newlines in the literal and reflow each line. Hard linebreaks are still respected.
For example, with a line length of 10:
```swift
"""
one \
word \
a line.
this is too long.
"""
```
will be formatted as:
```swift
"""
one word \
a line.
this is \
too long.
"""
```
**default:** `"never"`
---
### `indentBlankLines`
**type:** boolean
**description:** Determines whether blank lines should be modified to match the current indentation. When this setting is true, blank lines will be modified whitespace. If `false`, all whitespace in blank lines will be completely removed.
**default:** `false`
> TODO: Add support for enabling/disabling specific syntax transformations in
> the pipeline.
### Example
An example `.swift-format` configuration file is shown below.
```javascript
{
"version": 1,
"lineLength": 100,
"indentation": {
"spaces": 2
},
"maximumBlankLines": 1,
"respectsExistingLineBreaks": true,
"lineBreakBeforeControlFlowKeywords": true,
"lineBreakBeforeEachArgument": true
}
```
## Linter and Formatter Rules Configuration
In the `rules` block of `.swift-format`, you can specify which rules to apply
when linting and formatting your project. Read the
[rules documentation](RuleDocumentation.md) to see the list of all
supported linter and formatter rules, and their overview.
You can also run this command to see the list of rules in the default
`swift-format` configuration:
$ swift-format dump-configuration
## API Configuration
The `SwiftConfiguration` module contains a `Configuration` type that is
equivalent to the JSON structure described above. (In fact, `Configuration`
conforms to `Codable` and is how the JSON form is read from and written to
disk.)
The `SwiftFormatter` and `SwiftLinter` APIs in the `SwiftFormat` module take a
mandatory `Configuration` argument that specifies how the formatter should
behave when acting upon source code or syntax trees.
The default initializer for `Configuration` creates a value equivalent to the
default configuration that would be printed by invoking
`swift-format dump-configuration`. API users can also provide their own
configuration by modifying this value or loading it from another source using
Swift's `Codable` APIs.
|