fastfmt 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastfmt-0.1.0/.github/workflows/ci.yml +72 -0
- fastfmt-0.1.0/.gitignore +14 -0
- fastfmt-0.1.0/Cargo.lock +225 -0
- fastfmt-0.1.0/Cargo.toml +15 -0
- fastfmt-0.1.0/DEV.md +38 -0
- fastfmt-0.1.0/LICENSE +202 -0
- fastfmt-0.1.0/PKG-INFO +59 -0
- fastfmt-0.1.0/README.md +40 -0
- fastfmt-0.1.0/_config.yml +1 -0
- fastfmt-0.1.0/_layouts/default.html +52 -0
- fastfmt-0.1.0/pyproject.toml +33 -0
- fastfmt-0.1.0/src/main.rs +224 -0
- fastfmt-0.1.0/tests/test_cli.py +27 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
tags: ['v*']
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v7
|
|
14
|
+
- uses: dtolnay/rust-toolchain@stable
|
|
15
|
+
with:
|
|
16
|
+
components: rustfmt
|
|
17
|
+
- uses: actions/setup-python@v7
|
|
18
|
+
with:
|
|
19
|
+
python-version: '3.12'
|
|
20
|
+
- run: pip install -e '.[dev]'
|
|
21
|
+
- run: pytest -q
|
|
22
|
+
|
|
23
|
+
build:
|
|
24
|
+
needs: test
|
|
25
|
+
strategy:
|
|
26
|
+
matrix:
|
|
27
|
+
os: [ubuntu-latest, macos-latest]
|
|
28
|
+
runs-on: ${{ matrix.os }}
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@v7
|
|
31
|
+
- uses: PyO3/maturin-action@v1
|
|
32
|
+
with:
|
|
33
|
+
args: --release --out dist
|
|
34
|
+
manylinux: 2_28
|
|
35
|
+
- uses: actions/upload-artifact@v7
|
|
36
|
+
with:
|
|
37
|
+
name: wheels-${{ matrix.os }}
|
|
38
|
+
path: dist
|
|
39
|
+
|
|
40
|
+
sdist:
|
|
41
|
+
runs-on: ubuntu-latest
|
|
42
|
+
steps:
|
|
43
|
+
- uses: actions/checkout@v7
|
|
44
|
+
- uses: PyO3/maturin-action@v1
|
|
45
|
+
with:
|
|
46
|
+
command: sdist
|
|
47
|
+
args: -o dist
|
|
48
|
+
- uses: actions/upload-artifact@v7
|
|
49
|
+
with:
|
|
50
|
+
name: wheels-sdist
|
|
51
|
+
path: dist
|
|
52
|
+
|
|
53
|
+
publish:
|
|
54
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
55
|
+
needs: [build, sdist]
|
|
56
|
+
runs-on: ubuntu-latest
|
|
57
|
+
permissions:
|
|
58
|
+
id-token: write
|
|
59
|
+
contents: write
|
|
60
|
+
steps:
|
|
61
|
+
- uses: actions/checkout@v7
|
|
62
|
+
- uses: actions/download-artifact@v8
|
|
63
|
+
with:
|
|
64
|
+
path: dist
|
|
65
|
+
merge-multiple: true
|
|
66
|
+
- uses: softprops/action-gh-release@v3
|
|
67
|
+
with:
|
|
68
|
+
files: dist/*
|
|
69
|
+
generate_release_notes: true
|
|
70
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
71
|
+
with:
|
|
72
|
+
packages-dir: dist/
|
fastfmt-0.1.0/.gitignore
ADDED
fastfmt-0.1.0/Cargo.lock
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "aho-corasick"
|
|
7
|
+
version = "1.1.5"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"memchr",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[[package]]
|
|
15
|
+
name = "cc"
|
|
16
|
+
version = "1.4.4"
|
|
17
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
18
|
+
checksum = "0ad534f4357a5264cce5019c989cf66a4f0dc4e0d1b1d15f8aacec0ff7360273"
|
|
19
|
+
dependencies = [
|
|
20
|
+
"find-msvc-tools",
|
|
21
|
+
"shlex",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[[package]]
|
|
25
|
+
name = "equivalent"
|
|
26
|
+
version = "1.0.2"
|
|
27
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
28
|
+
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
|
29
|
+
|
|
30
|
+
[[package]]
|
|
31
|
+
name = "fastfmt"
|
|
32
|
+
version = "0.1.0"
|
|
33
|
+
dependencies = [
|
|
34
|
+
"tree-sitter",
|
|
35
|
+
"tree-sitter-rust",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[[package]]
|
|
39
|
+
name = "find-msvc-tools"
|
|
40
|
+
version = "0.1.11"
|
|
41
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
42
|
+
checksum = "d45db016d36b838f563236e9193d0ee6ce38f3f68b6c94e914b4929c96bbb890"
|
|
43
|
+
|
|
44
|
+
[[package]]
|
|
45
|
+
name = "hashbrown"
|
|
46
|
+
version = "0.17.1"
|
|
47
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
48
|
+
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
|
49
|
+
|
|
50
|
+
[[package]]
|
|
51
|
+
name = "indexmap"
|
|
52
|
+
version = "2.14.0"
|
|
53
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
54
|
+
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
|
|
55
|
+
dependencies = [
|
|
56
|
+
"equivalent",
|
|
57
|
+
"hashbrown",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[[package]]
|
|
61
|
+
name = "itoa"
|
|
62
|
+
version = "1.0.18"
|
|
63
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
64
|
+
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
|
65
|
+
|
|
66
|
+
[[package]]
|
|
67
|
+
name = "memchr"
|
|
68
|
+
version = "2.8.3"
|
|
69
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
70
|
+
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
71
|
+
|
|
72
|
+
[[package]]
|
|
73
|
+
name = "proc-macro2"
|
|
74
|
+
version = "1.0.107"
|
|
75
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
76
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
77
|
+
dependencies = [
|
|
78
|
+
"unicode-ident",
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
[[package]]
|
|
82
|
+
name = "quote"
|
|
83
|
+
version = "1.0.47"
|
|
84
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
85
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
86
|
+
dependencies = [
|
|
87
|
+
"proc-macro2",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
[[package]]
|
|
91
|
+
name = "regex"
|
|
92
|
+
version = "1.13.1"
|
|
93
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
94
|
+
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
|
|
95
|
+
dependencies = [
|
|
96
|
+
"aho-corasick",
|
|
97
|
+
"memchr",
|
|
98
|
+
"regex-automata",
|
|
99
|
+
"regex-syntax",
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
[[package]]
|
|
103
|
+
name = "regex-automata"
|
|
104
|
+
version = "0.4.18"
|
|
105
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
106
|
+
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
|
|
107
|
+
dependencies = [
|
|
108
|
+
"aho-corasick",
|
|
109
|
+
"memchr",
|
|
110
|
+
"regex-syntax",
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
[[package]]
|
|
114
|
+
name = "regex-syntax"
|
|
115
|
+
version = "0.8.11"
|
|
116
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
117
|
+
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
|
|
118
|
+
|
|
119
|
+
[[package]]
|
|
120
|
+
name = "serde"
|
|
121
|
+
version = "1.0.229"
|
|
122
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
123
|
+
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
|
124
|
+
dependencies = [
|
|
125
|
+
"serde_core",
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
[[package]]
|
|
129
|
+
name = "serde_core"
|
|
130
|
+
version = "1.0.229"
|
|
131
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
132
|
+
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
|
133
|
+
dependencies = [
|
|
134
|
+
"serde_derive",
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
[[package]]
|
|
138
|
+
name = "serde_derive"
|
|
139
|
+
version = "1.0.229"
|
|
140
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
141
|
+
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
|
142
|
+
dependencies = [
|
|
143
|
+
"proc-macro2",
|
|
144
|
+
"quote",
|
|
145
|
+
"syn",
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
[[package]]
|
|
149
|
+
name = "serde_json"
|
|
150
|
+
version = "1.0.151"
|
|
151
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
152
|
+
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
|
|
153
|
+
dependencies = [
|
|
154
|
+
"indexmap",
|
|
155
|
+
"itoa",
|
|
156
|
+
"memchr",
|
|
157
|
+
"serde",
|
|
158
|
+
"serde_core",
|
|
159
|
+
"zmij",
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
[[package]]
|
|
163
|
+
name = "shlex"
|
|
164
|
+
version = "2.0.1"
|
|
165
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
166
|
+
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
|
167
|
+
|
|
168
|
+
[[package]]
|
|
169
|
+
name = "streaming-iterator"
|
|
170
|
+
version = "0.1.9"
|
|
171
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
172
|
+
checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520"
|
|
173
|
+
|
|
174
|
+
[[package]]
|
|
175
|
+
name = "syn"
|
|
176
|
+
version = "3.0.4"
|
|
177
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
178
|
+
checksum = "e6275cddf4610d1775e6d1fe9469b2e77d0f39fd98fb7450901b821e0c53649f"
|
|
179
|
+
dependencies = [
|
|
180
|
+
"proc-macro2",
|
|
181
|
+
"quote",
|
|
182
|
+
"unicode-ident",
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
[[package]]
|
|
186
|
+
name = "tree-sitter"
|
|
187
|
+
version = "0.25.10"
|
|
188
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
189
|
+
checksum = "78f873475d258561b06f1c595d93308a7ed124d9977cb26b148c2084a4a3cc87"
|
|
190
|
+
dependencies = [
|
|
191
|
+
"cc",
|
|
192
|
+
"regex",
|
|
193
|
+
"regex-syntax",
|
|
194
|
+
"serde_json",
|
|
195
|
+
"streaming-iterator",
|
|
196
|
+
"tree-sitter-language",
|
|
197
|
+
]
|
|
198
|
+
|
|
199
|
+
[[package]]
|
|
200
|
+
name = "tree-sitter-language"
|
|
201
|
+
version = "0.1.7"
|
|
202
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
203
|
+
checksum = "009994f150cc0cd50ff54917d5bc8bffe8cad10ca10d81c34da2ec421ae61782"
|
|
204
|
+
|
|
205
|
+
[[package]]
|
|
206
|
+
name = "tree-sitter-rust"
|
|
207
|
+
version = "0.24.2"
|
|
208
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
209
|
+
checksum = "439e577dbe07423ec2582ac62c7531120dbfccfa6e5f92406f93dd271a120e45"
|
|
210
|
+
dependencies = [
|
|
211
|
+
"cc",
|
|
212
|
+
"tree-sitter-language",
|
|
213
|
+
]
|
|
214
|
+
|
|
215
|
+
[[package]]
|
|
216
|
+
name = "unicode-ident"
|
|
217
|
+
version = "1.0.24"
|
|
218
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
219
|
+
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
|
220
|
+
|
|
221
|
+
[[package]]
|
|
222
|
+
name = "zmij"
|
|
223
|
+
version = "1.0.23"
|
|
224
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
225
|
+
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
fastfmt-0.1.0/Cargo.toml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "fastfmt"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
description = "cargo fmt plus fastai-style compaction: keeps simple one-line constructs on one line"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[[bin]]
|
|
10
|
+
name = "cargo-fastfmt"
|
|
11
|
+
path = "src/main.rs"
|
|
12
|
+
|
|
13
|
+
[dependencies]
|
|
14
|
+
tree-sitter = "0.25"
|
|
15
|
+
tree-sitter-rust = "0.24"
|
fastfmt-0.1.0/DEV.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Development
|
|
2
|
+
|
|
3
|
+
## Design
|
|
4
|
+
|
|
5
|
+
Everything lives in `src/main.rs`. `fastfmt()` pipes a file through `rustfmt
|
|
6
|
+
--emit stdout` (width and `use_small_heuristics=Max` passed via `--config`, so
|
|
7
|
+
target crates need no rustfmt.toml), then `compact()` re-joins. Joining is
|
|
8
|
+
syntax-aware via tree-sitter: `join_limit` names the joinable node kinds and
|
|
9
|
+
their item limits, `joined` renders a candidate onto one line (dropping a
|
|
10
|
+
trailing comma, adding no space before `.`-led continuations), and
|
|
11
|
+
`compact_round` applies innermost candidates first, iterating to a fixpoint so
|
|
12
|
+
nested one-liners collapse. Anything containing a comment or a multi-line
|
|
13
|
+
token never joins.
|
|
14
|
+
|
|
15
|
+
The tuning benchmark is AnswerDotAI/loopmini: run stable `cargo fmt` on its
|
|
16
|
+
`src/`, then `cargo-fastfmt`, and diff against the original. The join caps
|
|
17
|
+
(105, and 80 for two-statement blocks) come from that exercise; the remaining
|
|
18
|
+
diff should be only rustfmt canonicalisations such as import order and
|
|
19
|
+
trailing semicolons.
|
|
20
|
+
|
|
21
|
+
## Commands
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
maturin develop && pytest -q
|
|
25
|
+
ship-rs-build
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Versioning
|
|
29
|
+
|
|
30
|
+
The canonical version lives in `Cargo.toml`. `pyproject.toml` gets the Python package version from Cargo via `dynamic = ["version"]`.
|
|
31
|
+
|
|
32
|
+
## Release
|
|
33
|
+
|
|
34
|
+
1. Run `maturin develop && pytest -q`.
|
|
35
|
+
2. Confirm the release version in `Cargo.toml` (`[package].version`).
|
|
36
|
+
3. Run `ship-release`.
|
|
37
|
+
|
|
38
|
+
Fastship commits the changelog, pushes the version tag for GitHub Actions, then bumps and pushes `Cargo.toml`.
|
fastfmt-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
fastfmt-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastfmt
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Requires-Dist: fastship>=0.0.11 ; extra == 'dev'
|
|
6
|
+
Requires-Dist: maturin>=1.0,<2.0 ; extra == 'dev'
|
|
7
|
+
Requires-Dist: pytest ; extra == 'dev'
|
|
8
|
+
Provides-Extra: dev
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Summary: cargo fmt plus fastai-style compaction: keeps simple one-line constructs on one line
|
|
11
|
+
Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
|
|
12
|
+
License: Apache-2.0
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
15
|
+
Project-URL: Homepage, https://github.com/AnswerDotAI/fastfmt
|
|
16
|
+
Project-URL: Issues, https://github.com/AnswerDotAI/fastfmt/issues
|
|
17
|
+
Project-URL: Repository, https://github.com/AnswerDotAI/fastfmt
|
|
18
|
+
|
|
19
|
+
# fastfmt
|
|
20
|
+
|
|
21
|
+
`cargo fmt`, then put the one-liners back.
|
|
22
|
+
|
|
23
|
+
Stable rustfmt always breaks small constructs onto multiple lines: a
|
|
24
|
+
single-expression fn body, a statement-position if/else, a short match or
|
|
25
|
+
struct body. The options that would prevent this are nightly-only, and some
|
|
26
|
+
shapes have no option at all. fastfmt runs rustfmt (via stdin, so no config
|
|
27
|
+
file is needed), then re-joins those constructs when they are comment-free,
|
|
28
|
+
small, and within a width cap, giving fastai-style compact Rust from a stable
|
|
29
|
+
toolchain.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install fastfmt
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The wheel ships a single `cargo-fastfmt` binary and no Python code. With it on
|
|
38
|
+
PATH, cargo picks it up as a subcommand:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
cargo fastfmt # format the current directory tree in place
|
|
42
|
+
cargo fastfmt --check # exit 1 listing files that would change; write nothing
|
|
43
|
+
cargo fastfmt src lib.rs # format specific files or directories
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`--width N` sets the rustfmt line cap (default 130). Joined one-liners use
|
|
47
|
+
tighter caps: 105 columns, or 80 for a two-statement block. rustfmt must be on
|
|
48
|
+
PATH (`rustup component add rustfmt`).
|
|
49
|
+
|
|
50
|
+
## What gets joined
|
|
51
|
+
|
|
52
|
+
A block joins onto one line only when nothing in it is a comment or spans
|
|
53
|
+
multiple lines itself, and the result fits the cap: fn bodies with one
|
|
54
|
+
statement, if/else and loop bodies with one or two, match/struct/enum bodies
|
|
55
|
+
with up to three arms, variants, or fields, and single-item impl blocks.
|
|
56
|
+
Joins run innermost-first to a fixpoint, so nested one-liners collapse fully.
|
|
57
|
+
Match arms stay expanded, and any block containing a comment or a multi-line
|
|
58
|
+
string is left exactly as rustfmt wrote it.
|
|
59
|
+
|
fastfmt-0.1.0/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# fastfmt
|
|
2
|
+
|
|
3
|
+
`cargo fmt`, then put the one-liners back.
|
|
4
|
+
|
|
5
|
+
Stable rustfmt always breaks small constructs onto multiple lines: a
|
|
6
|
+
single-expression fn body, a statement-position if/else, a short match or
|
|
7
|
+
struct body. The options that would prevent this are nightly-only, and some
|
|
8
|
+
shapes have no option at all. fastfmt runs rustfmt (via stdin, so no config
|
|
9
|
+
file is needed), then re-joins those constructs when they are comment-free,
|
|
10
|
+
small, and within a width cap, giving fastai-style compact Rust from a stable
|
|
11
|
+
toolchain.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install fastfmt
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The wheel ships a single `cargo-fastfmt` binary and no Python code. With it on
|
|
20
|
+
PATH, cargo picks it up as a subcommand:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
cargo fastfmt # format the current directory tree in place
|
|
24
|
+
cargo fastfmt --check # exit 1 listing files that would change; write nothing
|
|
25
|
+
cargo fastfmt src lib.rs # format specific files or directories
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`--width N` sets the rustfmt line cap (default 130). Joined one-liners use
|
|
29
|
+
tighter caps: 105 columns, or 80 for a two-statement block. rustfmt must be on
|
|
30
|
+
PATH (`rustup component add rustfmt`).
|
|
31
|
+
|
|
32
|
+
## What gets joined
|
|
33
|
+
|
|
34
|
+
A block joins onto one line only when nothing in it is a comment or spans
|
|
35
|
+
multiple lines itself, and the result fits the cap: fn bodies with one
|
|
36
|
+
statement, if/else and loop bodies with one or two, match/struct/enum bodies
|
|
37
|
+
with up to three arms, variants, or fields, and single-item impl blocks.
|
|
38
|
+
Joins run innermost-first to a fixpoint, so nested one-liners collapse fully.
|
|
39
|
+
Match arms stay expanded, and any block containing a comment or a multi-line
|
|
40
|
+
string is left exactly as rustfmt wrote it.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
theme: jekyll-theme-minimal
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="{{ site.lang | default: "en-US" }}">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
7
|
+
|
|
8
|
+
{% seo %}
|
|
9
|
+
<link rel="stylesheet" href="{{ "/assets/css/style.css?v=" | append: site.github.build_revision | relative_url }}">
|
|
10
|
+
{% include head-custom.html %}
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<div class="wrapper">
|
|
14
|
+
<header>
|
|
15
|
+
<h1><a href="{{ "/" | absolute_url }}">{{ site.title | default: site.github.repository_name }}</a></h1>
|
|
16
|
+
|
|
17
|
+
{% if site.logo %}
|
|
18
|
+
<img src="{{site.logo | relative_url}}" alt="Logo" />
|
|
19
|
+
{% endif %}
|
|
20
|
+
|
|
21
|
+
<p>{{ site.description | default: site.github.project_tagline }}</p>
|
|
22
|
+
|
|
23
|
+
{% if site.github.is_project_page %}
|
|
24
|
+
<p class="view"><a href="{{ site.github.repository_url }}">View the Project on GitHub <small>{{ site.github.repository_nwo }}</small></a></p>
|
|
25
|
+
{% endif %}
|
|
26
|
+
|
|
27
|
+
{% if site.github.is_user_page %}
|
|
28
|
+
<p class="view"><a href="{{ site.github.owner_url }}">View My GitHub Profile</a></p>
|
|
29
|
+
{% endif %}
|
|
30
|
+
|
|
31
|
+
{% if site.show_downloads %}
|
|
32
|
+
<ul class="downloads">
|
|
33
|
+
<li><a href="{{ site.github.zip_url }}">Download <strong>ZIP File</strong></a></li>
|
|
34
|
+
<li><a href="{{ site.github.tar_url }}">Download <strong>TAR Ball</strong></a></li>
|
|
35
|
+
<li><a href="{{ site.github.repository_url }}">View On <strong>GitHub</strong></a></li>
|
|
36
|
+
</ul>
|
|
37
|
+
{% endif %}
|
|
38
|
+
</header>
|
|
39
|
+
<section>
|
|
40
|
+
|
|
41
|
+
{{ content }}
|
|
42
|
+
|
|
43
|
+
</section>
|
|
44
|
+
<footer>
|
|
45
|
+
{% if site.github.is_project_page %}
|
|
46
|
+
<p>This project is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a></p>
|
|
47
|
+
{% endif %}
|
|
48
|
+
</footer>
|
|
49
|
+
</div>
|
|
50
|
+
<script src="{{ "/assets/js/scale.fix.js" | relative_url }}"></script>
|
|
51
|
+
</body>
|
|
52
|
+
</html>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.0,<2.0"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fastfmt"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "cargo fmt plus fastai-style compaction: keeps simple one-line constructs on one line"
|
|
9
|
+
license = {text = "Apache-2.0"}
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
authors = [{name = "Jeremy Howard", email = "github@jhoward.fastmail.fm"}]
|
|
13
|
+
classifiers = ["Programming Language :: Rust"]
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
dev = ["fastship>=0.0.11", "maturin>=1.0,<2.0", "pytest"]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Homepage = "https://github.com/AnswerDotAI/fastfmt"
|
|
20
|
+
Repository = "https://github.com/AnswerDotAI/fastfmt"
|
|
21
|
+
Issues = "https://github.com/AnswerDotAI/fastfmt/issues"
|
|
22
|
+
|
|
23
|
+
[tool.maturin]
|
|
24
|
+
bindings = "bin"
|
|
25
|
+
|
|
26
|
+
[tool.uv]
|
|
27
|
+
cache-keys = [{ file = "pyproject.toml" }, { file = "src/**/*.rs" }, { file = "Cargo.toml" }, { file = "Cargo.lock" }]
|
|
28
|
+
|
|
29
|
+
[tool.fastship]
|
|
30
|
+
branch = "main"
|
|
31
|
+
|
|
32
|
+
[tool.pytest.ini_options]
|
|
33
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
//! fastfmt: rustfmt, then re-join the short constructs the house style keeps on
|
|
2
|
+
//! one line. rustfmt (stable) always breaks fn bodies, statement if/else, loop
|
|
3
|
+
//! bodies, and short match/struct/enum/impl bodies onto multiple lines; the
|
|
4
|
+
//! compaction pass joins any such block back when it is comment-free, within the
|
|
5
|
+
//! width cap, and small (fn/impl: 1 item, control-flow blocks: 2 statements,
|
|
6
|
+
//! comma-separated bodies: 3 items). Joins run innermost-first to a fixpoint, so
|
|
7
|
+
//! nested one-liners collapse fully.
|
|
8
|
+
use std::io::Write;
|
|
9
|
+
use std::path::{Path, PathBuf};
|
|
10
|
+
use std::process::{Command, Stdio};
|
|
11
|
+
use tree_sitter::{Node, Parser};
|
|
12
|
+
|
|
13
|
+
const DEFAULT_WIDTH: usize = 150; // when neither --width nor a rustfmt.toml max_width applies
|
|
14
|
+
|
|
15
|
+
fn parser() -> Parser {
|
|
16
|
+
let mut p = Parser::new();
|
|
17
|
+
p.set_language(&tree_sitter_rust::LANGUAGE.into()).unwrap();
|
|
18
|
+
p
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Statement/item count limit for a joinable body, or None when `kind` never joins.
|
|
22
|
+
fn join_limit(node: &Node) -> Option<usize> {
|
|
23
|
+
match node.kind() {
|
|
24
|
+
"block" if node.parent().is_some_and(|p| p.kind() == "function_item") => Some(1),
|
|
25
|
+
"block" if node.parent().is_some_and(|p| p.kind() == "match_arm") => None, // arms stay expanded
|
|
26
|
+
"block" => Some(2),
|
|
27
|
+
"match_block" | "field_declaration_list" | "enum_variant_list" => Some(3),
|
|
28
|
+
"declaration_list" => Some(1),
|
|
29
|
+
_ => None,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fn has_multiline_leaf(node: Node, src: &str) -> bool {
|
|
34
|
+
let mut c = node.walk();
|
|
35
|
+
let mut stack = vec![node];
|
|
36
|
+
while let Some(n) = stack.pop() {
|
|
37
|
+
if n.child_count() == 0 && src[n.byte_range()].contains('\n') { return true }
|
|
38
|
+
if n.kind().contains("comment") { return true }
|
|
39
|
+
stack.extend(n.children(&mut c));
|
|
40
|
+
}
|
|
41
|
+
false
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// True when `block` ends with a return/break/continue statement, whose
|
|
45
|
+
/// rustfmt-added trailing semicolon can drop in the one-line form.
|
|
46
|
+
fn ends_jump(node: &Node) -> bool {
|
|
47
|
+
if node.kind() != "block" || node.named_child_count() == 0 { return false }
|
|
48
|
+
let last = node.named_child(node.named_child_count() - 1).unwrap();
|
|
49
|
+
last.kind() == "expression_statement"
|
|
50
|
+
&& last.child(0).is_some_and(|c| matches!(c.kind(), "return_expression" | "break_expression" | "continue_expression"))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// `node`'s text joined onto one line, or None when it must stay multi-line:
|
|
54
|
+
/// not a joinable kind, too many items, comment-bearing, or over its width cap.
|
|
55
|
+
fn joined(node: Node, src: &str, width: usize) -> Option<String> {
|
|
56
|
+
let limit = join_limit(&node)?;
|
|
57
|
+
let text = &src[node.byte_range()];
|
|
58
|
+
if !text.contains('\n') { return None }
|
|
59
|
+
if node.named_child_count() > limit { return None }
|
|
60
|
+
if has_multiline_leaf(node, src) { return None }
|
|
61
|
+
let mut out = String::with_capacity(text.len());
|
|
62
|
+
for (i, line) in text.lines().enumerate() {
|
|
63
|
+
let line = if i > 0 { line.trim_start() } else { line };
|
|
64
|
+
// No space before a continuation that rustfmt broke at an operator or delimiter
|
|
65
|
+
if i > 0 && !line.starts_with(['.', ',', ';', '?', ')', ']']) { out.push(' ') }
|
|
66
|
+
out.push_str(line);
|
|
67
|
+
}
|
|
68
|
+
let out = out.replace(", }", " }");
|
|
69
|
+
let out = if ends_jump(&node) && out.ends_with("; }") { format!("{} }}", &out[..out.len() - 3]) } else { out };
|
|
70
|
+
let prefix = src[..node.start_byte()].rfind('\n').map_or(0, |p| node.start_byte() - p - 1);
|
|
71
|
+
let suffix = src[node.end_byte()..].find('\n').unwrap_or(0);
|
|
72
|
+
if prefix + out.len() + suffix > width { return None }
|
|
73
|
+
Some(out)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// One compaction round: join every innermost joinable block, return the new text.
|
|
77
|
+
fn compact_round(src: &str, width: usize) -> String {
|
|
78
|
+
let tree = parser().parse(src, None).unwrap();
|
|
79
|
+
let mut edits: Vec<(std::ops::Range<usize>, String)> = vec![];
|
|
80
|
+
let mut stack = vec![tree.root_node()];
|
|
81
|
+
let mut c = tree.root_node().walk();
|
|
82
|
+
while let Some(n) = stack.pop() {
|
|
83
|
+
if let Some(j) = joined(n, src, width) {
|
|
84
|
+
edits.push((n.byte_range(), j));
|
|
85
|
+
continue; // children are inside the joined text; skip them
|
|
86
|
+
}
|
|
87
|
+
stack.extend(n.children(&mut c));
|
|
88
|
+
}
|
|
89
|
+
edits.sort_by(|a, b| b.0.start.cmp(&a.0.start));
|
|
90
|
+
let mut out = src.to_string();
|
|
91
|
+
for (r, j) in edits { out.replace_range(r, &j) }
|
|
92
|
+
out
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
pub fn compact(src: &str, width: usize) -> String {
|
|
96
|
+
let mut cur = src.to_string();
|
|
97
|
+
for _ in 0..5 {
|
|
98
|
+
let next = compact_round(&cur, width);
|
|
99
|
+
if next == cur { break }
|
|
100
|
+
cur = next;
|
|
101
|
+
}
|
|
102
|
+
cur
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// Format `src` with rustfmt (stdin mode, sharing our width cap), then compact.
|
|
106
|
+
fn fastfmt(src: &str, edition: &str, width: usize) -> Result<String, String> {
|
|
107
|
+
let mut child = Command::new("rustfmt")
|
|
108
|
+
.args(["--edition", edition, "--emit", "stdout", "--config", &format!("max_width={width},use_small_heuristics=Max")])
|
|
109
|
+
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped())
|
|
110
|
+
.spawn().map_err(|e| format!("failed to run rustfmt: {e}"))?;
|
|
111
|
+
child.stdin.take().unwrap().write_all(src.as_bytes()).map_err(|e| e.to_string())?;
|
|
112
|
+
let out = child.wait_with_output().map_err(|e| e.to_string())?;
|
|
113
|
+
if !out.status.success() { return Err(String::from_utf8_lossy(&out.stderr).into_owned()) }
|
|
114
|
+
Ok(compact(&String::from_utf8_lossy(&out.stdout), width))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// The value at `key` in the nearest `file` at or above `path`, e.g. the edition
|
|
118
|
+
/// from Cargo.toml or max_width from rustfmt.toml.
|
|
119
|
+
fn toml_lookup(path: &Path, file: &str, key: &str) -> Option<String> {
|
|
120
|
+
for dir in path.canonicalize().unwrap_or_else(|_| path.to_path_buf()).ancestors() {
|
|
121
|
+
if let Ok(t) = std::fs::read_to_string(dir.join(file)) {
|
|
122
|
+
return t.lines().find(|l| l.trim_start().starts_with(key))
|
|
123
|
+
.and_then(|l| l.split('=').nth(1))
|
|
124
|
+
.map(|v| v.trim().trim_matches('"').to_string());
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
None
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn edition_for(path: &Path) -> String { toml_lookup(path, "Cargo.toml", "edition").unwrap_or_else(|| "2021".into()) }
|
|
131
|
+
|
|
132
|
+
/// `--width` beats the nearest rustfmt.toml's max_width, which beats the default.
|
|
133
|
+
fn width_for(path: &Path, flag: Option<usize>) -> usize {
|
|
134
|
+
flag.or_else(|| toml_lookup(path, "rustfmt.toml", "max_width").and_then(|w| w.parse().ok())).unwrap_or(DEFAULT_WIDTH)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fn rs_files(path: &Path, out: &mut Vec<PathBuf>) {
|
|
138
|
+
if path.is_file() {
|
|
139
|
+
if path.extension().is_some_and(|e| e == "rs") { out.push(path.to_path_buf()) }
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
|
143
|
+
if name == "target" || name.starts_with('.') && name.len() > 1 { return }
|
|
144
|
+
if let Ok(rd) = std::fs::read_dir(path) {
|
|
145
|
+
for e in rd.flatten() { rs_files(&e.path(), out) }
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
fn main() {
|
|
150
|
+
let mut args: Vec<String> = std::env::args().skip(1).collect();
|
|
151
|
+
if args.first().is_some_and(|a| a == "fastfmt") { args.remove(0); } // invoked as `cargo fastfmt`
|
|
152
|
+
let check = args.iter().any(|a| a == "--check");
|
|
153
|
+
let width_flag = args.iter().position(|a| a == "--width")
|
|
154
|
+
.and_then(|i| args.get(i + 1)).and_then(|w| w.parse().ok());
|
|
155
|
+
let mut paths: Vec<PathBuf> = vec![];
|
|
156
|
+
let mut skip = false;
|
|
157
|
+
for (i, a) in args.iter().enumerate() {
|
|
158
|
+
if skip { skip = false; continue }
|
|
159
|
+
match a.as_str() {
|
|
160
|
+
"--check" => {}
|
|
161
|
+
"--width" => skip = true,
|
|
162
|
+
_ if i > 0 && args[i - 1] == "--width" => {}
|
|
163
|
+
p => paths.push(p.into()),
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if paths.is_empty() { paths.push(".".into()) }
|
|
167
|
+
let mut files = vec![];
|
|
168
|
+
for p in &paths { rs_files(p, &mut files) }
|
|
169
|
+
files.sort();
|
|
170
|
+
let mut dirty = vec![];
|
|
171
|
+
for f in &files {
|
|
172
|
+
let src = match std::fs::read_to_string(f) { Ok(s) => s, Err(e) => { eprintln!("{}: {e}", f.display()); std::process::exit(2) } };
|
|
173
|
+
match fastfmt(&src, &edition_for(f), width_for(f, width_flag)) {
|
|
174
|
+
Ok(new) if new != src => {
|
|
175
|
+
if check { dirty.push(f) } else if let Err(e) = std::fs::write(f, &new) { eprintln!("{}: {e}", f.display()); std::process::exit(2) }
|
|
176
|
+
}
|
|
177
|
+
Ok(_) => {}
|
|
178
|
+
Err(e) => { eprintln!("{}: {e}", f.display()); std::process::exit(2) }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if check && !dirty.is_empty() {
|
|
182
|
+
for f in dirty { println!("would reformat {}", f.display()) }
|
|
183
|
+
std::process::exit(1);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[cfg(test)]
|
|
188
|
+
mod tests {
|
|
189
|
+
use super::compact;
|
|
190
|
+
|
|
191
|
+
#[test]
|
|
192
|
+
fn joins_house_shapes() {
|
|
193
|
+
let cases = [
|
|
194
|
+
("fn f(x: u8) -> u8 {\n if x > 1 {\n return 1;\n }\n 0\n}\n",
|
|
195
|
+
"fn f(x: u8) -> u8 {\n if x > 1 { return 1 }\n 0\n}\n"),
|
|
196
|
+
("fn time(&self) -> f64 {\n self.core.time()\n}\n",
|
|
197
|
+
"fn time(&self) -> f64 { self.core.time() }\n"),
|
|
198
|
+
("fn f(c: bool, e: &mut E, h: H) {\n if c {\n e.writer = Some(h)\n } else {\n e.reader = Some(h)\n }\n}\n",
|
|
199
|
+
"fn f(c: bool, e: &mut E, h: H) { if c { e.writer = Some(h) } else { e.reader = Some(h) } }\n"),
|
|
200
|
+
("struct FdEntry<H> {\n reader: Option<H>,\n writer: Option<H>,\n}\n",
|
|
201
|
+
"struct FdEntry<H> { reader: Option<H>, writer: Option<H> }\n"),
|
|
202
|
+
("enum Rt {\n Owned(Runtime),\n Borrowed(Handle),\n}\n",
|
|
203
|
+
"enum Rt { Owned(Runtime), Borrowed(Handle) }\n"),
|
|
204
|
+
("fn drain(&self, q: &mut Q) {\n while let Some(h) = q.pop_front() {\n ready.push_back(h)\n }\n}\n",
|
|
205
|
+
"fn drain(&self, q: &mut Q) { while let Some(h) = q.pop_front() { ready.push_back(h) } }\n"),
|
|
206
|
+
("impl<H> Default for FdEntry<H> {\n fn default() -> Self {\n Self { reader: None, writer: None }\n }\n}\n",
|
|
207
|
+
"impl<H> Default for FdEntry<H> { fn default() -> Self { Self { reader: None, writer: None } } }\n"),
|
|
208
|
+
("fn h(&self) -> Handle {\n match self {\n Rt::Owned(r) => r.handle().clone(),\n Rt::Borrowed(h) => h.clone(),\n }\n}\n",
|
|
209
|
+
"fn h(&self) -> Handle { match self { Rt::Owned(r) => r.handle().clone(), Rt::Borrowed(h) => h.clone() } }\n"),
|
|
210
|
+
];
|
|
211
|
+
for (src, want) in cases { assert_eq!(compact(src, 150), want, "src: {src}") }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#[test]
|
|
215
|
+
fn keeps_what_must_stay() {
|
|
216
|
+
for src in [
|
|
217
|
+
"fn f() {\n a();\n b();\n}\n", // two statements in a fn body
|
|
218
|
+
"fn f() {\n // why\n a()\n}\n", // comment must survive
|
|
219
|
+
"fn f() {\n let s = \"a\nb\";\n}\n", // multiline string literal
|
|
220
|
+
] { assert_eq!(compact(src, 150), src) }
|
|
221
|
+
let long = format!("fn f() {{\n {}()\n}}\n", "x".repeat(160));
|
|
222
|
+
assert_eq!(compact(&long, 150), long); // width cap
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"End-to-end check of the installed binary: rustfmt runs, then compaction joins."
|
|
2
|
+
import subprocess
|
|
3
|
+
|
|
4
|
+
BROKEN ="""fn time() -> f64 {
|
|
5
|
+
42.0
|
|
6
|
+
}
|
|
7
|
+
fn pick(c: bool, e: &mut (Option<u8>, Option<u8>)) {
|
|
8
|
+
if c {
|
|
9
|
+
e.0 = Some(1)
|
|
10
|
+
} else {
|
|
11
|
+
e.1 = Some(2)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def test_fastfmt_binary(tmp_path):
|
|
17
|
+
(tmp_path / 'Cargo.toml').write_text('[package]\nedition = "2021"\n')
|
|
18
|
+
f = tmp_path / 'main.rs'
|
|
19
|
+
f.write_text(BROKEN)
|
|
20
|
+
subprocess.run(['cargo-fastfmt'], cwd=tmp_path, check=True)
|
|
21
|
+
out = f.read_text()
|
|
22
|
+
assert 'fn time() -> f64 { 42.0 }' in out
|
|
23
|
+
assert 'if c { e.0 = Some(1) } else { e.1 = Some(2) }' in out
|
|
24
|
+
assert subprocess.run(['cargo-fastfmt', '--check'], cwd=tmp_path).returncode == 0
|
|
25
|
+
f.write_text(BROKEN)
|
|
26
|
+
r = subprocess.run(['cargo-fastfmt', '--check'], cwd=tmp_path, capture_output=True, text=True)
|
|
27
|
+
assert r.returncode == 1 and 'main.rs' in r.stdout
|