@live-codes/prettier-plugin-rust 0.1.9
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.
- package/LICENSE +21 -0
- package/README.md +290 -0
- package/index.cjs +4281 -0
- package/index.d.ts +18 -0
- package/index.global.js +44 -0
- package/index.js +4272 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present jinxdash <jinxdash.github@gmail.com> (https://github.com/jinxdash)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img alt="Prettier Rust" height="256px" src="https://user-images.githubusercontent.com/109366411/181039409-b66d6a4c-bbc7-4fbb-8a79-d7bb1af87a63.png">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">Prettier Rust</h1>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
|
|
9
|
+
 [](https://www.npmjs.com/package/prettier-plugin-rust) [](https://marketplace.visualstudio.com/items?itemName=jinxdash.prettier-rust)  [](https://twitter.com/jinxdash)
|
|
10
|
+
|
|
11
|
+
_The massively popular [Prettier](https://prettier.io/) code formatter, now with [Rust](https://www.rust-lang.org/) support!_
|
|
12
|
+
|
|
13
|
+
**Get Started:** Install [VSCode Extension](https://marketplace.visualstudio.com/items?itemName=jinxdash.prettier-rust) `Prettier - Code formatter (Rust)`
|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
> **Note:** This is a fork of [jinxdash/prettier-plugin-rust](https://github.com/jinxdash/prettier-plugin-rust), migrated to support [Prettier v3](https://prettier.io/blog/2023/07/05/3.0.0.html) and newer. All credit for the original plugin goes to [@jinxdash](https://github.com/jinxdash).
|
|
18
|
+
|
|
19
|
+
## Why Prettier?
|
|
20
|
+
|
|
21
|
+
> What usually happens once people start using Prettier is that they realize how much time and mental energy they actually spend formatting their code. No matter how incomplete or broken the code you're working on is, with the Prettier Editor Extension you can always just press the `Format Document` key binding and \*poof\*, the code snaps right into place.
|
|
22
|
+
|
|
23
|
+
<br>
|
|
24
|
+
|
|
25
|
+
- **Beautiful, uniform and consistent** — Prettier is strongly opinionated, with no style options.
|
|
26
|
+
- **There when you need it the most** — Prettier can format code that won't compile _(e.g. missing annotations)_
|
|
27
|
+
- **Speed up the day-to-day** — Prettier auto-fixes common syntax errors _(e.g. missing semicolons, blocks, parentheses)_
|
|
28
|
+
|
|
29
|
+
<br>
|
|
30
|
+
|
|
31
|
+
<table align="center">
|
|
32
|
+
<tr> <th>> input</th> <th>> formatted</th> </tr>
|
|
33
|
+
<tr>
|
|
34
|
+
<td>
|
|
35
|
+
|
|
36
|
+
<!-- prettier-ignore -->
|
|
37
|
+
```rs
|
|
38
|
+
const LEET = 1337
|
|
39
|
+
/// My WIP code draft
|
|
40
|
+
#![feature(crate_visibility_modifier)]
|
|
41
|
+
async crate fn foo(arg) {
|
|
42
|
+
arg.0 *= 3.14 + LEET & 1337
|
|
43
|
+
arg.1(|b, c| -> T &c).await
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
</td>
|
|
48
|
+
<td>
|
|
49
|
+
|
|
50
|
+
<!-- prettier-ignore -->
|
|
51
|
+
```rs
|
|
52
|
+
const LEET = 1337;
|
|
53
|
+
#![feature(crate_visibility_modifier)]
|
|
54
|
+
/// My WIP code draft
|
|
55
|
+
crate async fn foo(arg) {
|
|
56
|
+
arg.0 *= (3.14 + LEET) & 1337;
|
|
57
|
+
(arg.1)(|b, c| -> T { &c }).await
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
</td>
|
|
62
|
+
</tr>
|
|
63
|
+
</table>
|
|
64
|
+
<div align="center">
|
|
65
|
+
|
|
66
|
+
_Formatting succeeds and fixes 7 syntax errors._
|
|
67
|
+
|
|
68
|
+
</div>
|
|
69
|
+
|
|
70
|
+
<br>
|
|
71
|
+
|
|
72
|
+
## Configuration
|
|
73
|
+
|
|
74
|
+
https://prettier.io/docs/en/configuration
|
|
75
|
+
|
|
76
|
+
<!-- prettier-ignore -->
|
|
77
|
+
```json5
|
|
78
|
+
// .prettierrc.json
|
|
79
|
+
{
|
|
80
|
+
"useTabs": false,
|
|
81
|
+
"tabWidth": 4,
|
|
82
|
+
"printWidth": 100,
|
|
83
|
+
"endOfLine": "lf",
|
|
84
|
+
|
|
85
|
+
// -- Not supported yet --
|
|
86
|
+
// "trailingComma": "es5",
|
|
87
|
+
// "embeddedLanguageFormatting": "auto",
|
|
88
|
+
|
|
89
|
+
// Example override
|
|
90
|
+
"overrides": { "files": ["tests/*.rs"], "options": { "printWidth": 80 } }
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
<details>
|
|
95
|
+
<summary>See alternative configuration using a TOML file</summary>
|
|
96
|
+
|
|
97
|
+
```toml
|
|
98
|
+
# .prettierrc.toml
|
|
99
|
+
|
|
100
|
+
useTabs = false
|
|
101
|
+
tabWidth = 4
|
|
102
|
+
printWidth = 100
|
|
103
|
+
endOfLine = "lf"
|
|
104
|
+
|
|
105
|
+
# -- Not supported yet --
|
|
106
|
+
# trailingComma = "es5"
|
|
107
|
+
# embeddedLanguageFormatting = "auto"
|
|
108
|
+
|
|
109
|
+
# Example override
|
|
110
|
+
overrides = [
|
|
111
|
+
{ files = ["tests/*.rs"], options = { printWidth = 80 } }
|
|
112
|
+
]
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
</details>
|
|
116
|
+
|
|
117
|
+
### How to ignore things
|
|
118
|
+
|
|
119
|
+
- Add `// prettier-ignore` or `#[rustfmt::skip]` above it
|
|
120
|
+
- Add `#![rustfmt::skip]` inside blocks or files
|
|
121
|
+
- Create a `.prettierignore` file to glob-match files, like `.gitignore`
|
|
122
|
+
|
|
123
|
+
### How are macros formatted?
|
|
124
|
+
|
|
125
|
+
- Curlies `!{}` format like blocks, `![]` and `!()` like comma-separated expressions
|
|
126
|
+
- Formatting inside macro invocations is more conservative, since macros can be token-sensitive
|
|
127
|
+
- Popular/built-in macros with original syntax rules get custom formatting (e.g. `matches!`, `if_chains!`...) _[Not implemented yet]_
|
|
128
|
+
- Macro Declarations are only partially formatted (the transformed part isn't yet, but could be in the future)
|
|
129
|
+
- Macros that can't be formatted are silently ignored
|
|
130
|
+
|
|
131
|
+
### Are nightly features supported?
|
|
132
|
+
|
|
133
|
+
Yes! Prettier Rust formats most nightly features. Support depends on [`jinx-rust`](https://github.com/jinxdash/jinx-rust).
|
|
134
|
+
|
|
135
|
+
<br>
|
|
136
|
+
|
|
137
|
+
## Editor integration
|
|
138
|
+
|
|
139
|
+
- ### `Recommended` Extension Standalone
|
|
140
|
+
|
|
141
|
+
_Easy install + auto-updates_
|
|
142
|
+
|
|
143
|
+
- VSCode | Search and install `Prettier - Code formatter (Rust)` [[direct link]](https://marketplace.visualstudio.com/items?itemName=jinxdash.prettier-rust)
|
|
144
|
+
|
|
145
|
+
- _Request your favorite editor:_ [[file an issue]](https://github.com/jinxdash/prettier-plugin-rust/issues/new)
|
|
146
|
+
|
|
147
|
+
- ### `Alternative` Core Extension Plugin
|
|
148
|
+
|
|
149
|
+
_Requires [NodeJS](https://nodejs.dev/download/) + [Prettier Extension](https://prettier.io/docs/en/editors.html)_ (built-in Jetbrains IDEs)
|
|
150
|
+
|
|
151
|
+
```sh
|
|
152
|
+
npm install --global prettier-plugin-rust prettier
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
_Restart IDE after installing._
|
|
156
|
+
_To update (manual only!!):_ `npm upgrade --global prettier-plugin-rust prettier`
|
|
157
|
+
_To check installed version:_ `npm ls -g --depth=0 prettier-plugin-rust prettier`
|
|
158
|
+
_To check latest version:_ `npm info prettier-plugin-rust version`
|
|
159
|
+
|
|
160
|
+
<br>
|
|
161
|
+
|
|
162
|
+
## Project integration
|
|
163
|
+
|
|
164
|
+
- ### Command line
|
|
165
|
+
|
|
166
|
+
_Requires [NodeJS](https://nodejs.dev/download/)_
|
|
167
|
+
|
|
168
|
+
- Install `prettier` and `prettier-plugin-rust` globally
|
|
169
|
+
|
|
170
|
+
```sh
|
|
171
|
+
npm install --global prettier-plugin-rust prettier
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
- Use the [prettier CLI](https://prettier.io/docs/en/cli.html) to format rust files. E.g. run:
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
prettier --write **/*.rs
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
- ### NodeJS package
|
|
181
|
+
|
|
182
|
+
_Requires [NodeJS](https://nodejs.dev/download/)_
|
|
183
|
+
|
|
184
|
+
- Install `prettier` and `prettier-plugin-rust` in the project
|
|
185
|
+
|
|
186
|
+
```sh
|
|
187
|
+
npm install --save-dev prettier-plugin-rust prettier
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
- Link to the plugin's location in your prettier config:
|
|
191
|
+
|
|
192
|
+
```json
|
|
193
|
+
"plugins": ["./node_modules/prettier-plugin-rust"]
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
- Use the [prettier CLI](https://prettier.io/docs/en/cli.html) to format rust files. E.g. run:
|
|
197
|
+
|
|
198
|
+
```sh
|
|
199
|
+
npx prettier --write **/*.rs
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
- You can also use the plugin programmatically:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import prettier from "prettier";
|
|
206
|
+
import * as rustPlugin from "prettier-plugin-rust";
|
|
207
|
+
|
|
208
|
+
await prettier.format(code, { plugins: [rustPlugin] });
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
> Requires Prettier v3 or newer. Prettier's `format`/`formatWithCursor` APIs are asynchronous in v3, so remember to `await` (or `.then(...)`) the result.
|
|
212
|
+
|
|
213
|
+
- ### Browser
|
|
214
|
+
|
|
215
|
+
_No build step — just `<script>` tags_
|
|
216
|
+
|
|
217
|
+
The plugin ships a self-contained, minified [IIFE](https://developer.mozilla.org/docs/Glossary/IIFE) bundle. It exposes a `prettierPluginRust` global, and also registers itself on `globalThis.prettierPlugins` like Prettier's own browser bundles do. Load it alongside [Prettier's standalone build](https://prettier.io/docs/en/browser):
|
|
218
|
+
|
|
219
|
+
```html
|
|
220
|
+
<script src="https://unpkg.com/prettier@3/standalone.js"></script>
|
|
221
|
+
<script src="https://unpkg.com/prettier-plugin-rust"></script>
|
|
222
|
+
|
|
223
|
+
<script>
|
|
224
|
+
const code = "async crate fn foo(arg) { arg.0 *= 3.14 + LEET & 1337 }";
|
|
225
|
+
|
|
226
|
+
// either pass the plugin explicitly...
|
|
227
|
+
prettier
|
|
228
|
+
.format(code, { parser: "rust", plugins: [prettierPluginRust] })
|
|
229
|
+
.then((formatted) => console.log(formatted));
|
|
230
|
+
|
|
231
|
+
// ...or use the registered global (same pattern as Prettier's own plugins)
|
|
232
|
+
prettier.format(code, { parser: "rust", plugins: prettierPlugins });
|
|
233
|
+
</script>
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
The plugin registers itself on `globalThis.prettierPlugins` under the key `rust`, so `prettierPlugins.rust` (and `plugins: prettierPlugins`) work without any extra setup.
|
|
237
|
+
|
|
238
|
+
The `unpkg`/`jsdelivr` fields point at `index.global.js`, so the bare package URL resolves to the browser bundle. It can also be loaded from a local copy (`node_modules/prettier-plugin-rust/index.global.js`) or any CDN that serves the file.
|
|
239
|
+
|
|
240
|
+
The parser is registered under both `"rust"` and `"jinx-rust"`, and is inferred automatically from a `.rs` filepath. Note that plugins given as **strings** do not resolve in the browser — Prettier's standalone build has no file resolver, so a config containing `"plugins": ["prettier-plugin-rust"]` has no effect there; pass the plugin object (or `prettierPlugins`) instead.
|
|
241
|
+
|
|
242
|
+
> Prettier's browser API is asynchronous, so `format` returns a `Promise`.
|
|
243
|
+
|
|
244
|
+
- ### Rust crate
|
|
245
|
+
|
|
246
|
+
_No crate yet. Above options are available in the meantime._
|
|
247
|
+
|
|
248
|
+
<br>
|
|
249
|
+
|
|
250
|
+
## Q&A
|
|
251
|
+
|
|
252
|
+
- ### _Why would I use this and not the established `cargo fmt` ?_
|
|
253
|
+
|
|
254
|
+
_It's all about the Editor Integration_ — Having the ability to format your code while you work on it really makes for a great developer experience, and autocompletion for Rust's strict syntax is such a massive time save. Once you've tried the extension there really is no coming back.
|
|
255
|
+
|
|
256
|
+
All-in-all the difference in code style is minimal, so adopting Prettier Rust won't drastically change your codebase. The real downside is the harsher integration with the Rust ecosystem, but it'll get better eventually.
|
|
257
|
+
|
|
258
|
+
Point by point:
|
|
259
|
+
|
|
260
|
+
- the extension streamlines your work in the editor
|
|
261
|
+
- it can format code that won't compile _(e.g. code with missing type annotations)_
|
|
262
|
+
- it autocorrects syntax errors _(e.g. missing semicolons, blocks, parentheses...)_
|
|
263
|
+
- it is strongly opinionated with no style options, so code is uniform across projects.
|
|
264
|
+
- it produces more readable code in some cases (e.g. condition chains, compound expressions, patterns)
|
|
265
|
+
- it supports everything out-of-the-box (e.g. nightly features, macros)
|
|
266
|
+
- it consistently prints code in the same way, whereas Rustfmt preserves arbitrary style at places
|
|
267
|
+
- it can be used for other languages (e.g. markdown, html, typescript, java, python, ruby)
|
|
268
|
+
- it formats language embeds. So rust code blocks in non-rust files (e.g. markdown), and supported languages in rust doc comments. _[NOTE: the latter is not yet implemented]_
|
|
269
|
+
|
|
270
|
+
- ### _Why not just add those features to rustfmt instead?_
|
|
271
|
+
|
|
272
|
+
Unfortunately Rustfmt cannot implement those features by design.
|
|
273
|
+
|
|
274
|
+
Rustfmt parses code with rustc. Rustc is strict and unforgiving as it always assumes code is at its "final version", thus every slight deviation from the accepted syntax crashes the parser. There's also that rustc has many lint-like checks within the parser. The intention is to save work for the compiler down the line, unfortunately it also means that rustc sometimes fails to parse syntactically correct code.
|
|
275
|
+
|
|
276
|
+
Prettier Rust however is based on [jinx-rust](https://github.com/jinxdash/jinx-rust). Jinx-rust is built specifically for Rust tooling. Hence it's designed to tolerate a wide range of syntax errors, supports missing nodes and sometimes even infers user intent (e.g. Javascript's `!==`)
|
|
277
|
+
|
|
278
|
+
Jinx-rust has a little _plaidoyer_ in its readme arguing for Rust Tooling _not_ to use the official rustc parser [here](https://github.com/jinxdash/jinx-rust#why-jinx-rust-and-why-in-typescript).
|
|
279
|
+
|
|
280
|
+
- ### _When exactly does Prettier Rust change code syntax?_
|
|
281
|
+
|
|
282
|
+
The Prettier Rust syntax autocorrection feature is intended to be an adaptation of how Prettier Typescript autocorrects javascript code with missing semicolons.
|
|
283
|
+
|
|
284
|
+
You can effectively think of Prettier Rust syntax autocorrection as auto-applying the Rust compiler's suggested syntax fixes automatically (e.g. "semicolon missing here", "parenthesize this" or "add a block around that")
|
|
285
|
+
|
|
286
|
+
Otherwise if your codebase compiles, then Prettier Rust is just a formatter like any other. It won't change the syntax of valid rust code. Moreover, it doesn't reorganize imports, split comments or combine attributes.
|
|
287
|
+
|
|
288
|
+
- ### _This is an "opinionated formatter". But it's brand new! How reliable are those opinions?_
|
|
289
|
+
|
|
290
|
+
Rest assured, Prettier Rust actually does not take style decisions on its own. Prettier Rust is essentially a 1:1 adaptation of Prettier Typescript, hence the opinions it implements have been battle tested and agreed-upon by [millions and millions of users](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) already.
|