Skip to content

Commit 2757517

Browse files
committed
Update README and add configuration and debugging docs.
1 parent d40e749 commit 2757517

File tree

3 files changed

+166
-243
lines changed

3 files changed

+166
-243
lines changed

Documentation/Configuration.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
> TODO: Add support for enabling/disabling specific syntax transformations in
50+
> the pipeline.
51+
52+
### Example
53+
54+
An example `.swift-format` configuration file is shown below.
55+
56+
```javascript
57+
{
58+
"version": 1,
59+
"lineLength": 100,
60+
"indentation": {
61+
"spaces": 2
62+
},
63+
"maximumBlankLines": 1,
64+
"respectsExistingLineBreaks": true,
65+
"blankLineBetweenMembers": {
66+
"ignoreSingleLineProperties": true
67+
}
68+
}
69+
```
70+
71+
## API Configuration
72+
73+
The `SwiftConfiguration` module contains a `Configuration` type that is
74+
equivalent to the JSON structure described above. (In fact, `Configuration`
75+
conforms to `Codable` and is how the JSON form is read from and written to
76+
disk.)
77+
78+
The `SwiftFormatter` and `SwiftLinter` APIs in the `SwiftFormat` module take a
79+
mandatory `Configuration` argument that specifies how the formatter should
80+
behave when acting upon source code or syntax trees.
81+
82+
The default initializer for `Configuration` creates a value equivalent to the
83+
default configuration that would be printed by invoking
84+
`swift-format --mode dump-configuration`. API users can also provide their own
85+
configuration by modifying this value or loading it from another source using
86+
Swift's `Codable` APIs.

Documentation/Debugging.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Debugging `swift-format`
2+
3+
## Command Line Options
4+
5+
`swift-format` provides some hidden command line options to facilitate
6+
debugging the tool during development:
7+
8+
* `--debug-disable-pretty-print`: Disables the pretty-printing pass of the
9+
formatter, causing only the syntax tree transformations in the first phase
10+
pipeline to run.
11+
12+
* `--debug-dump-token-stream`: Dumps a human-readable indented structure
13+
representing the pseudotoken stream constructed by the pretty printing
14+
phase.

0 commit comments

Comments
 (0)