Skip to content

Commit f38829e

Browse files
authored
Merge pull request swiftlang#1 from allevato/initial-commit
Initial implementation of swift-format.
2 parents 374db8c + 0050b48 commit f38829e

File tree

181 files changed

+22059
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+22059
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.build/
2+
.swiftpm/
3+
swift-format.xcodeproj/

Documentation/Configuration.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.

Documentation/Development.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Developing `swift-format`
2+
3+
## Command Line Options for Debugging
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.
15+
16+
## Support Scripts
17+
18+
The [Scripts](../Scripts) directory contains a `format-diff.sh` script
19+
that some developers may find useful. When invoked, it rebuilds
20+
`swift-format` (if necessary to pick up any recent changes) and lets
21+
you view a side-by-side `diff` with the original file on the left side
22+
and the formatted output on the right side.
23+
24+
This script will use `colordiff` if it is installed on your `PATH`;
25+
otherwise, it will fall back to `diff`.

0 commit comments

Comments
 (0)