(new!) Prettier Rust: An opinionated code formatter that autocorrects bad syntax


Prettier code formatter for Rust

Quickstart: Search VSCode extension: Prettier - Code formatter (Rust) [link]

Visit the repository to see other install options (Core extension, as a plugin, in the CLI, as a crate...)

Repository: GitHub - jinxdash/prettier-plugin-rust: Prettier Rust is an opinionated code formatter that autocorrects bad syntax.

Why Prettier?

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.

  • Beautiful, uniform and consistent — Prettier is strongly opinionated, with no style options.
  • There when you need it the most — Prettier can format code that won't compile (e.g. missing annotations)
  • Speed up the day-to-day — Prettier auto-fixes common syntax errors (e.g. missing semicolons, blocks, parentheses)

> input > formatted
const LEET = 1337
/// My WIP code draft
async crate fn foo(arg) {
  arg.0 *= 3.14 + LEET & 1337
  arg.1(|b, c| -> T &c).await
const LEET = 1337;
/// My WIP code draft
crate async fn foo(arg) {
    arg.0 *= (3.14 + LEET) & 1337;
    (arg.1)(|b, c| -> T { &c }).await

Formatting succeeds and fixes 7 syntax errors.


Complete config with default values:

# .prettierrc.toml

useTabs = false
tabWidth = 4
printWidth = 100
endOfLine = "lf"

# Not supported yet
# trailingComma = "es5"
# embeddedLanguageFormatting = "auto"


  • Why not just add syntax autocorrection to rustfmt instead?

    Unfortunately Rustfmt cannot implement those features by design.

    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.

    Prettier Rust however is based on 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 !==)

    Jinx-rust has a little plaidoyer in its readme arguing for Rust Tooling not to use the official rustc parser here.

  • When exactly does Prettier Rust change code syntax?

    The Prettier Rust syntax autocorrection feature is intended to be an adaptation of how Prettier Typescript autocorrects javascript code with missing semicolons.

    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")

    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.

  • This is an "opinionated formatter". But it's brand new! How reliable are those opinions?

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


2 Questions:

  • Does it handle macros_rules? (rustfmt doesn't)
  • Does it format in a way that rustfmt does not need to do further changes if it runs?
1 Like

i'm using "rust-analyzer" extension in VS code and found its autocompletion obstrusive.

how is the autocompletion for this extension?

  • Does it handle macros_rules? (rustfmt doesn't)

    Not all of it, the "transformed" righthand side part isn't supported yet. Formatting that part requires tinkering with the parser it's based on, jinx-rust. It can be done and I'll get to it if Prettier Rust gets traction

  • Does it format in a way that rustfmt does not need to do further changes if it runs?

    It probably could if you added some lines to your rustfmt config, I might try to find out if that's something people want

  • how is the autocompletion for this extension?

    I've not used rust-analyzer a lot myself so I can't compare, but Prettier Rust doesn't reorganize attributes, imports or comments. It only deals with syntax and whitespace.

In what way exactly? Also you can disable various kinds of autocompletion using the rust-analyzer.completion.* config options

in its autocompletion. it would choose by itself what should be the word or command.

um, how do you get to "rust-analyzer.completion.*" ?

In the configuration file of the editor you can add settings like "rust-analyzer.completion.autoimport.enable": false to disable autoimport completions, or "rust-analyzer.completion.postfix.enable": false to disable postfix completions like expr.dbg -> dbg!(expr). You can find the full list at User Manual

1 Like

cc Implement pattern-based formatter for rust-code on top of rust-analyzer · Issue #1665 · rust-lang/rust-analyzer · GitHub

I'd personally love to see an error-tolerant, line break preserving code formatter for Rust.

rust-analyzer's parser is in a good enough shape for that imo.