Skip to content

Commit 6bcbea7

Browse files
allevatoabladelliboviatamez31dabelknap
committed
Initial implementation of swift-format.
Co-authored-by: Alexander Lash <[email protected]> Co-authored-by: Alfredo Delli Bovi <[email protected]> Co-authored-by: Andrés Tamez Hdz <[email protected]> Co-authored-by: Austin Belknap <[email protected]> Co-authored-by: Harlan Haskins <[email protected]> Co-authored-by: Lauren White <[email protected]> Co-authored-by: Tony Allevato <[email protected]> Co-authored-by: Yuta Saito <[email protected]>
1 parent 374db8c commit 6bcbea7

File tree

223 files changed

+25248
-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.

223 files changed

+25248
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.build/
2+
.swiftpm/
3+
swift-format.xcodeproj/

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "Sources/CCommonMark/cmark"]
2+
path = Sources/CCommonMark/cmark
3+
url = https://github.com/apple/swift-cmark

Documentation/Configuration.md

+101
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

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Developing `swift-format`
2+
3+
## Check Out the Submodules
4+
5+
The `CCommonMark` target is a Git submodule that pulls the C code in
6+
[apple/swift-cmark](https://github.com/apple/swift-cmark) into this
7+
project.
8+
9+
After you clone this repository, remember to run the following commands
10+
to make sure those sources are available to the compiler:
11+
12+
```shell
13+
git submodule init
14+
git submodule update
15+
```
16+
17+
## Command Line Options for Debugging
18+
19+
`swift-format` provides some hidden command line options to facilitate
20+
debugging the tool during development:
21+
22+
* `--debug-disable-pretty-print`: Disables the pretty-printing pass of the
23+
formatter, causing only the syntax tree transformations in the first phase
24+
pipeline to run.
25+
26+
* `--debug-dump-token-stream`: Dumps a human-readable indented structure
27+
representing the pseudotoken stream constructed by the pretty printing
28+
phase.
29+
30+
## Support Scripts
31+
32+
The [Scripts](../Scripts) directory contains a `format-diff.sh` script
33+
that some developers may find useful. When invoked, it rebuilds
34+
`swift-format` (if necessary to pick up any recent changes) and lets
35+
you view a side-by-side `diff` with the original file on the left side
36+
and the formatted output on the right side.
37+
38+
This script will use `colordiff` if it is installed on your `PATH`;
39+
otherwise, it will fall back to `diff`.

0 commit comments

Comments
 (0)