You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make a couple of changes to `format.py` and use the opportunity to re-write it in Swift.
1. Always build a local copy of swift-format in `swift-syntax/.swift-format-build` in release mode. This eliminates issues that occur if you had `swift-format` installed on your system and `format.py` would pick it up
2. Since the local swift-format build is persistent (doesn’t live in `/tmp`), we can build it in release. This increases the first run but decreases the time for any subsequent runs from ~15s to ~1s, which should pay usually pay off for frequent contributors because of faster turnaround times.
3. Add a little tip to add `swift-format` as a git hook to Contributing.md
And as an added bonus, the rewrite reduced the runtime of `format.(py|swift)` from 1.2s to 800ms.
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+14-4Lines changed: 14 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -19,12 +19,22 @@ swift-syntax is a SwiftPM package, so you can build and test it using anything t
19
19
20
20
swift-syntax is formatted using [swift-format](http://github.com/apple/swift-format) to ensure a consistent style.
21
21
22
-
To format your changes run `format.py` at the root of this repository. If you have a `swift-format` executable ready, you can pass it to `format.py`. If you do not, `format.py` will build its own copy of `swift-format` in `/tmp/swift-format`.
23
-
24
-
If you are seeing surprising formatting results, you most likely have a `swift-format` installed on your system that’s not the most recent version built from the `main` branch. To fix this, clone [swift-format](http://github.com/apple/swift-format), build it using `swift build` and pass the freshly built executable to `format.py` as `--swift-format path/to/swift-format/.build/debug/swift-format`. Alternatively, you can uninstall `swift-format` on your system and `format.py` will build it from scratch.
22
+
To format your changes run `format.swift` at the root of this repository from the terminal as `./format.swift`. It will build a local copy of swift-format from the `main` branch and format the repository. Since it is building a release version of `swift-format`, the first run will take few minutes. Subsequent runs take less than 2 seconds.
25
23
26
24
Generated source code is not formatted to make it easier to spot changes when re-running code generation.
27
25
26
+
> 💡 Tip: You can add a git hook to ensure all commits to the swift-syntax repository are correctly formatted.
27
+
> 1. Save the following contents to `swift-syntax/.git/hooks/pre-commit`
28
+
> ```bash
29
+
>#!/bin/bash
30
+
>set -e
31
+
> SOURCE_DIR=$(realpath "$(dirname $0)/../..")
32
+
>"$SOURCE_DIR/format.swift" --lint
33
+
>```
34
+
> 2. Mark the file as executable: `chmod a+x swift-syntax/.git/hooks/pre-commit`
35
+
> 3. If you have any global git hooks installed, be sure to call them at the end of the script: `path/to/global/hooks/pre-commit "$@"
36
+
37
+
28
38
## Generating Source Code
29
39
30
40
If you want to modify the generated files, open the [CodeGeneration](CodeGeneration) package and run the `generate-swift-syntax` executable.
@@ -51,4 +61,4 @@ When testing finds one of these failures, it will show you the syntax tree that
51
61
52
62
## Swift Version
53
63
54
-
We require that swift-syntax builds with the latest released compiler and the previous major version (e.g. with Swift 5.8 and Swift 5.7).
64
+
We require that swift-syntax builds with the latest released compiler and the previous major version (e.g. with Swift 5.8 and Swift 5.7).
0 commit comments