Skip to content

Commit d3a1a5b

Browse files
committed
Re-architect format.py in swift
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.
1 parent 6f23c24 commit d3a1a5b

File tree

4 files changed

+564
-140
lines changed

4 files changed

+564
-140
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ Package.resolved
2020
*.pyc
2121

2222
Tests/PerformanceTest/baselines.json
23+
24+
# The local build of swift-format to format swift-syntax
25+
.swift-format-build

CONTRIBUTING.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@ swift-syntax is a SwiftPM package, so you can build and test it using anything t
1919

2020
swift-syntax is formatted using [swift-format](http://github.com/apple/swift-format) to ensure a consistent style.
2121

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.
2523

2624
Generated source code is not formatted to make it easier to spot changes when re-running code generation.
2725

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+
2838
## Generating Source Code
2939
3040
If you want to modify the generated files, open the [CodeGeneration](CodeGeneration) package and run the `generate-swift-syntax` executable.

format.py

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)