@@ -17,81 +17,156 @@ invoked via an [API](#api-usage).
1717
1818` swift-format ` depends on [ SwiftSyntax] ( https://github.com/apple/swift-syntax )
1919and the standalone parsing library that is distributed as part of the Swift
20- toolchain. The SwiftSyntax version in use must match the toolchain version, so
21- you should check out and build ` swift-format ` from the branch that is
22- compatible with the version of Swift you are using. This version dependency
23- is also expressed in the ` SwiftSyntax ` dependency in
24- [ Package.swift] ( Package.swift ) .
25-
26- | Xcode Release | Swift Version | ` swift-format ` Branch |
27- | :-------------:| :---------------------------------------:| :----------------------|
28- | – | Swift at ` main ` | ` main ` |
29- | Xcode 13.0 | Swift 5.5 | ` swift-5.5-branch ` |
30- | Xcode 12.5 | Swift 5.4 | ` swift-5.4-branch ` |
31- | Xcode 12.0 | Swift 5.3 | ` swift-5.3-branch ` |
32- | Xcode 11.4 | Swift 5.2 | ` swift-5.2-branch ` |
33- | Xcode 11.0 | Swift 5.1 | ` swift-5.1-branch ` |
34-
35- For example, if you are using Xcode 12.0 (Swift 5.3), you can check out and
36- build ` swift-format ` using the following commands:
37-
38- ```
39- git clone -b swift-5.3-branch https://github.com/apple/swift-format.git
20+ toolchain, so you should check out and build ` swift-format ` from the release
21+ tag or branch that is compatible with the version of Swift you are using.
22+
23+ The major and minor version components of ` swift-format ` and SwiftSyntax must
24+ be the same—this is expressed in the ` SwiftSyntax ` dependency in
25+ [ Package.swift] ( Package.swift ) —and those version components must match the
26+ Swift toolchain that is installed and used to build and run the formatter:
27+
28+ | Xcode Release | Swift Version | ` swift-format ` Branch / Tags |
29+ | :----------------| :-----------------------| :---------------------------------|
30+ | – | Swift at ` main ` | ` main ` |
31+ | Xcode 13.0–13.2 | Swift 5.5 | ` swift-5.5-branch ` / ` 0.50500.x ` |
32+ | Xcode 12.5 | Swift 5.4 | ` swift-5.4-branch ` / ` 0.50400.x ` |
33+ | Xcode 12.0–12.4 | Swift 5.3 | ` swift-5.3-branch ` / ` 0.50300.x ` |
34+ | Xcode 11.4–11.7 | Swift 5.2 | ` swift-5.2-branch ` / ` 0.50200.x ` |
35+ | Xcode 11.0–11.3 | Swift 5.1 | ` swift-5.1-branch ` |
36+
37+ For example, if you are using Xcode 13.1 (Swift 5.5), you will need
38+ ` swift-format ` 0.50500.0.
39+
40+ ## Getting swift-format
41+
42+ If you are mainly interested in using swift-format (rather than developing it),
43+ then once you have identified the version you need, you can check out the
44+ source and build it using the following commands:
45+
46+ ``` sh
47+ VERSION=0.50500.0 # replace this with the version you need
48+ git clone https://github.com/apple/swift-format.git
4049cd swift-format
41- swift build
50+ git checkout " tags/$VERSION "
51+ swift build -c release
4252```
4353
44- You can also add the ` --single-branch ` option if you only want to clone that
45- specific branch.
54+ Note that the ` git checkout ` command above will leave the repository in a
55+ "detached HEAD" state. This is fine if building and running the tool is all you
56+ want to do.
4657
47- The ` main ` branch is used for development and may depend on either a release
48- version of Swift or on a developer snapshot. Changes committed to ` main `
49- that are compatible with the latest release branch will be cherry-picked into
50- that branch.
58+ Once the build has finished, the ` swift-format ` executable will be located at
59+ ` .build/release/swift-format ` .
5160
5261To test that the formatter was built succesfully and is compatible with your
53- Swift toolchain, you can run the following command:
62+ Swift toolchain, you can also run the following command:
5463
55- ```
64+ ``` sh
5665swift test --parallel
5766```
67+
5868We recommend using the ` --parallel ` flag to speed up the test run since there
5969are a large number of tests.
6070
6171## Command Line Usage
6272
73+ The general invocation syntax for ` swift-format ` is as follows:
74+
75+ ``` sh
76+ swift-format [SUBCOMMAND] [OPTIONS...] [FILES...]
77+ ```
78+
79+ The tool supports a number of subcommands, each of which has its own options
80+ and are described below. Descriptions of the subcommands that are available
81+ can also be obtained by running ` swift-format --help ` , and the description of
82+ a specific subcommand can be obtained by using the ` --help ` flag after the
83+ subcommand name; for example, ` swift-format lint --help ` .
84+
85+ ### Formatting
86+
87+ ``` sh
88+ swift-format [format] [OPTIONS...] [FILES...]
6389```
64- swift-format [OPTIONS] FILE...
90+
91+ The ` format ` subcommand formats one or more Swift source files (or source code
92+ from standard input if no file paths are given on the command line). Writing
93+ out the ` format ` subcommand is optional; it is the default behavior if no other
94+ subcommand is given.
95+
96+ This subcommand supports all of the
97+ [ common lint and format options] ( #options-supported-by-formatting-and-linting ) ,
98+ as well as the formatting-only options below:
99+
100+ * ` -i/--in-place ` : Overwrites the input files when formatting instead of
101+ printing the results to standard output. _ No backup of the original file is
102+ made before it is overwritten._
103+
104+ ### Linting
105+
106+ ``` sh
107+ swift-format lint [OPTIONS...] [FILES...]
65108```
66109
67- The ` swift-format ` tool can be invoked with one or more ` .swift ` source files,
68- as well as the following command line options:
110+ The ` lint ` subcommand checks one or more Swift source files (or source code
111+ from standard input if no file paths are given on the command line) for style
112+ violations and prints diagnostics to standard error for any violations that
113+ are detected.
114+
115+ This subcommand supports all of the
116+ [ common lint and format options] ( #options-supported-by-formatting-and-linting ) ,
117+ as well as the linting-only options below:
118+
119+ * ` -s/--strict ` : If this option is specified, lint warnings will cause the
120+ tool to exit with a non-zero exit code (failure). By default, lint warnings
121+ do not prevent a successful exit; only fatal errors (for example, trying to
122+ lint a file that does not exist) cause the tool to exit unsuccessfully.
123+
124+ ### Options Supported by Formatting and Linting
125+
126+ The following options are supported by both the ` format ` and ` lint `
127+ subcommands:
69128
70- * ` -v/--version ` : Prints the ` swift-format ` version and exits.
129+ * ` --assume-filename <path> ` : The file path that should be used in
130+ diagnostics when linting or formatting from standard input. If this option
131+ is not provided, then ` <stdin> ` will be used as the filename printed in
132+ diagnostics.
71133
72- * ` -m/--mode <format|lint|dump-configuration> ` : The mode in which to run
73- ` swift-format ` . The ` format ` mode formats source files. The ` lint ` mode
74- only prints diagnostics indicating style violations. The ` dump-configuration `
75- mode dumps the default ` swift-format ` configuration to standard output.
134+ * ` --color-diagnostics/--no-color-diagnostics ` : By default, ` swift-format `
135+ will print diagnostics in color if standard error is connected to a
136+ terminal and without color otherwise (for example, if standard error is
137+ being redirected to a file). These flags can be used to force colors on
138+ or off respectively, regardless of whether the output is going to a
139+ terminal.
76140
77- If unspecified, the default mode is ` format ` .
141+ * ` --configuration <file> ` : The path to a JSON file that contains
142+ [ configurable settings] ( #configuring-the-command-line-tool ) for
143+ ` swift-format ` . If omitted, a default configuration is use (which
144+ can be seen by running ` swift-format dump-configuration ` ).
78145
79- * ` --configuration <file> ` : The path to a JSON file that contains
80- [ configurable settings] ( #configuration ) for ` swift-format ` . If omitted, a
81- default configuration is use (which can be seen by running
82- ` --mode dump-configuration ` ).
146+ * ` --ignore-unparsable-files ` : If this option is specified and a source file
147+ contains syntax errors or can otherwise not be parsed successfully by the
148+ Swift syntax parser, it will be ignored (no diagnostics will be emitted
149+ and it will not be formatted). Without this option, an error will be
150+ emitted for any unparsable files.
83151
84- * ` -i /--in-place ` : Overwrites the input files when formatting instead of
85- printing the results to standard output .
152+ * ` -p /--parallel ` : Process files in parallel, simultaneously across
153+ multiple cores .
86154
87- * ` -p/--parallel ` : Process files in parallel, simultaneously across
88- multiple cores.
155+ * ` -r/--recursive ` : If specified, then the tool will process ` .swift ` source
156+ files in any directories listed on the command line and their descendants.
157+ Without this flag, it is an error to list a directory on the command line.
89158
90- * ` -r/--recursive ` : If specified, then the tool will process ` .swift ` source
91- files in any directories listed on the command line and their descendants.
92- Without this flag, it is an error to list a directory on the command line.
159+ ### Viewing the Default Configuration
93160
94- ### Configuration
161+ ``` sh
162+ swift-format dump-configuration
163+ ```
164+
165+ The ` dump-configuration ` subcommand dumps the default configuration in JSON
166+ format to standard output. This can be used to simplify generating a custom
167+ configuration, by redirecting it to a file and editing it.
168+
169+ ### Configuring the Command Line Tool
95170
96171For any source file being checked or formatted, ` swift-format ` looks for a
97172JSON-formatted file named ` .swift-format ` in the same directory. If one is
@@ -111,6 +186,11 @@ See [Documentation/Configuration.md](Documentation/Configuration.md) for a
111186description of the configuration file format and the settings that are
112187available.
113188
189+ ### Miscellaneous
190+
191+ Running ` swift-format -v ` or ` swift-format --version ` will print version
192+ information about ` swift-format ` version and then exit.
193+
114194## API Usage
115195
116196` swift-format ` can be easily integrated into other tools written in Swift.
@@ -133,7 +213,12 @@ Please see the documentation in the
133213[ ` SwiftLinter ` ] ( Sources/SwiftFormat/SwiftLinter.swift ) classes for more
134214information about their usage.
135215
136- ## Development
216+ ### Checking Out the Source Code for Development
217+
218+ The ` main ` branch is used for development. Pull requests should be created
219+ to merge into the ` main ` branch; changes that are low-risk and compatible with
220+ the latest release branch may be cherry-picked into that branch after they have
221+ been merged into ` main ` .
137222
138223If you are interested in developing ` swift-format ` , there is additional
139224documentation about that [ here] ( Documentation/Development.md ) .
0 commit comments