Intellij / rust plugin, "align" fields


#1
  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?

#2

rustfmt has an option for this!


#3

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: https://github.com/rust-lang/rustfmt/blob/master/Configurations.md. The struct field alignment, in particular, is controlled by struct_field_align_threshold, as mentioned above.


#4

@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” ?


#5

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.