|
| 1 | +# `swift-format` Configuration |
| 2 | + |
| 3 | +`swift-format` allows users to configure a subset of its behavior, both when |
| 4 | +used as a command line tool or as an API. |
| 5 | + |
| 6 | +## Command Line Configuration |
| 7 | + |
| 8 | +A `swift-format` configuration file is a JSON file with the following |
| 9 | +top-level keys and values: |
| 10 | + |
| 11 | +* `version` _(number)_: The version of the configuration file. For now, this |
| 12 | + should always be `1`. |
| 13 | + |
| 14 | +* `lineLength` _(number)_: The maximum allowed length of a line, in |
| 15 | + characters. |
| 16 | + |
| 17 | +* `indentation` _(object)_: The kind and amount of whitespace that should be |
| 18 | + added when indenting one level. The object value of this property should |
| 19 | + have **exactly one of the following properties:** |
| 20 | + |
| 21 | + * `spaces` _(number)_: One level of indentation is the given number of |
| 22 | + spaces. |
| 23 | + * `tabs` _(number)_: One level of indentation is the given number of |
| 24 | + tabs. |
| 25 | + |
| 26 | +* `tabWidth` _(number)_: The number of spaces that should be considered |
| 27 | + equivalent to one tab character. This is used during line length |
| 28 | + calculations when tabs are used for indentation. |
| 29 | + |
| 30 | +* `maximumBlankLines` _(number)_: The maximum number of consecutive blank |
| 31 | + lines that are allowed to be present in a source file. Any number larger |
| 32 | + than this will be collapsed down to the maximum. |
| 33 | + |
| 34 | +* `respectsExistingLineBreaks` _(boolean)_: Indicates whether or not existing |
| 35 | + line breaks in the source code should be honored (if they are valid |
| 36 | + according to the style guidelines being enforced). If this settings is |
| 37 | + `false`, then the formatter will be more "opinionated" by only inserting |
| 38 | + line breaks where absolutely necessary and removing any others, effectively |
| 39 | + canonicalizing the output. |
| 40 | + |
| 41 | +* `blankLineBetweenMembers` _(object)_: Controls logic specific to the |
| 42 | + enforcement of blank lines between type members. The object value of this |
| 43 | + property has the following properties: |
| 44 | + |
| 45 | + * `ignoreSingleLineProperties` _(boolean)_: If `true`, then single-line |
| 46 | + property declarations are allowed to appear consecutively without a |
| 47 | + blank line separating them. |
| 48 | + |
| 49 | +* `lineBreakBeforeControlFlowKeywords` _(boolean)_: Determines the |
| 50 | + line-breaking behavior for control flow keywords that follow a closing |
| 51 | + brace, like `else` and `catch`. If true, a line break will be added before |
| 52 | + the keyword, forcing it onto its own line. If false (the default), the |
| 53 | + keyword will be placed after the closing brace (separated by a space). |
| 54 | + |
| 55 | +* `lineBreakBeforeEachArgument` _(boolean)_: Determines the line-breaking |
| 56 | + behavior for generic arguments and function arguments when a declaration is |
| 57 | + wrapped onto multiple lines. If true (the default), a line break will be |
| 58 | + added before each argument, forcing the entire argument list to be laid out |
| 59 | + vertically. If false, arguments will be laid out horizontally first, with |
| 60 | + line breaks only being fired when the line length would be exceeded. |
| 61 | + |
| 62 | +> TODO: Add support for enabling/disabling specific syntax transformations in |
| 63 | +> the pipeline. |
| 64 | +
|
| 65 | +### Example |
| 66 | + |
| 67 | +An example `.swift-format` configuration file is shown below. |
| 68 | + |
| 69 | +```javascript |
| 70 | +{ |
| 71 | + "version": 1, |
| 72 | + "lineLength": 100, |
| 73 | + "indentation": { |
| 74 | + "spaces": 2 |
| 75 | + }, |
| 76 | + "maximumBlankLines": 1, |
| 77 | + "respectsExistingLineBreaks": true, |
| 78 | + "blankLineBetweenMembers": { |
| 79 | + "ignoreSingleLineProperties": true |
| 80 | + }, |
| 81 | + "lineBreakBeforeControlFlowKeywords": true, |
| 82 | + "lineBreakBeforeEachArgument": true |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## API Configuration |
| 87 | + |
| 88 | +The `SwiftConfiguration` module contains a `Configuration` type that is |
| 89 | +equivalent to the JSON structure described above. (In fact, `Configuration` |
| 90 | +conforms to `Codable` and is how the JSON form is read from and written to |
| 91 | +disk.) |
| 92 | + |
| 93 | +The `SwiftFormatter` and `SwiftLinter` APIs in the `SwiftFormat` module take a |
| 94 | +mandatory `Configuration` argument that specifies how the formatter should |
| 95 | +behave when acting upon source code or syntax trees. |
| 96 | + |
| 97 | +The default initializer for `Configuration` creates a value equivalent to the |
| 98 | +default configuration that would be printed by invoking |
| 99 | +`swift-format --mode dump-configuration`. API users can also provide their own |
| 100 | +configuration by modifying this value or loading it from another source using |
| 101 | +Swift's `Codable` APIs. |
0 commit comments