Intellij / rust plugin, "align" fields

  1. Sample code:

pub struct Bad {
    a: f32,
    some_other_field: f32,
    name: String,
}

pub struct Good {
    a:                f32,
    some_other_field: f32,
    name:             String,
}

  1. I am using IntelliJ/Rust. Is there some builtin tool that will change the struct from "bad" to "Good" alignment?

rustfmt has an option for this!

https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#struct_field_align_threshold

2 Likes

I agree that rustfmt is what you want. It's also the go-to formatter for Rust, and you can run it as cargo fmt after installing it. I'm not an IntelliJ user, myself, but I found this guide for using it through the Rust plugin: https://github.com/rust-lang/rustfmt/blob/master/intellij.md. You can then add a rustfmt.toml file to your project and configure it with these settings: rustfmt/Configurations.md at master · rust-lang/rustfmt · GitHub. The struct field alignment, in particular, is controlled by struct_field_align_threshold, as mentioned above.

2 Likes

@AndrewGaspar , @ogeon : Thanks!

So it sounds like the best/general solution is some setup where in IntelliJ, I ahve some type of "on save hook => call rust fmt => reload buffer" ?

The features list makes mention of "a special action for formatting code with rustfmt", but I don't know how to invoke it - I don't have Intellij.