0

Using the Rust-Analyzer and VS code, auto-complete snippets use default rust formatting, i.e fn gets replaced with

fn () {

}

However, I have never liked this brace style, nor do I plan on sharing my code, and for my own work I want to be able to use my own preferred brace placement style of

fn ()
{

}

I have looked through the settings for Rust-Analyzer and cant seem to find out where/how to do this. Is it possible? I found rustfmt but from my knowledge that is meant to be run on files after they are written.

MooseTech
  • 1
  • 1
  • 2
    `rustfmt` is called by `rust-analyzer` under the hood. `rustfmt` has the [`brace_style`](https://rust-lang.github.io/rustfmt/?version=v1.5.1&search=brace#brace_style) option, but it is still unstable, requiring you to opt into the nightly toolchain and judging from the [tracking issue](https://github.com/rust-lang/rustfmt/issues/3376), it may not always work just yet. – Jonas Fassbender Mar 10 '23 at 09:29
  • i just popped `brace_style = "AlwaysNextLine"` into rustfmt.toml and it formatted all my code back into standard rust format, so yeah it seems like its definitely something that still needs to be worked on, but thanks for trying – MooseTech Mar 10 '23 at 09:41
  • Have you used the nightly toolchain? Could be that `rustfmt` simply ignores nightly features when they are used with the stable toolchain – Jonas Fassbender Mar 10 '23 at 09:44
  • 3
    In my personal experience it was worth it to ditch my own preferences in favour of the style everyone else is using. That way, the code I read is always in the same style, which speeds up reading it. If your code is different than everyone else's, you need to get used to two coding styles when reading code, and code is read a lot more often than it is written. – Sven Marnach Mar 10 '23 at 10:02
  • Note that code rust-analyzer's code generation does not emit formatted code directly. It will only be formatted using `rustfmt` the next time you run the formatter. – Sven Marnach Mar 10 '23 at 10:19
  • Note that it is not enough to compile the project with nightly, you need to make sure rust-analyzer is also using nightly (you can do that by creating a `rust-toolchain` file at the project root with the content `nightly` and maybe via other ways too). – Chayim Friedman Mar 10 '23 at 13:14

0 Answers0