Vscode ~ Extension to change coding style

Hi. I am looking for an extension (or a way) on Visual Studio Code that allows me to format code in a specific way.

For example typically people like to code in this style:

fn main() {
// Do stuff
}

However I prefer to code like this:

fn main()
{
// Do stuff
}

I would also like to add extra rules such as spacing.

For example if I am creating a structure:

struct Color
{
	red: u32,
	orange: u32,
}

I would like to create a rule or something that would format it like this:

struct Color
{
	red:        u32,
	orange:     u32,
}

notice how it is spaced out more nicely and evenly. I want something to automate it where it is all evened out.

Does anyone know of such extension(s)?

If you use rustfmt to format your code, you can use these unstable options in rustfmt.toml to customize the behavior:

1 Like

If you use the rust-analyzer extension (which is the recommended extension for Rust nowadays), it will also run rustfmt on save. rustfmt will in turn use its configuration file.

To be honest with you I don't really like this extension because it shows these things:

image

Unless if I can disable these things from showing up, I would rather use an alternative to only format my code :slight_smile: Would you know how to disable those things from showing up?

As @mbrubeck was stating I could use rustfmt.

image

I believe the first option is the correct one to install, am I correct?

Set "rust-analyzer.inlayHints.typeHints": false in your settings.json.

1 Like

Ok thanks that works

Now I wanted to know with rust-analyzer specifically, how would I change how text is formated (e.g. braces formatting) because before I believe you were mentioning it for a different extension so I wanted to know for this specific extention the rust-analyzer how would I change it?

The rust-analyzer extension uses rustfmt to format your code by default, if you have "editor.formatOnSave" enabled.

I need to target a file called rustfmt. So I am looking for the global configeration. I believe it is stored in %userprofile\AppData but where do I go next to get to the global verison of rustfmt ?

You'll need to create a folder called rustfmt inside the AppData\Roaming folder, and then create a file called rustfmt.toml inside of that.

So the whole path will be: %userprofile%\AppData\Roaming\rustfmt\rustfmt.toml

1 Like

Hey man I am getting a little bit of a problem over here

I did store it in %userprofile%\AppData\Roaming\rustfmt\rustfmt.toml as you said, in that file I have only one line:

brace_style = "AlwaysNextLine"

I purposely wrote my code like this:

fn main() {
    let x = 10;
}

and when I go to right click and click on "Format Document" it does nothing.

.

if I did write my code like this however:

fn main() {
}

and I again do "format document", it does change my code to this:

fn main() {}

so yeah I am not too sure what is going on.

Warning: can't set `brace_style = AlwaysNextLine`, unstable features are only available in nightly channel.
Warning: can't set `unstable_features = true`, unstable features are only available in nightly channel. 

it turns out I need to enable cargo nightly I will give that a shot

Ok so cargo nightly has solved the problem thanks :slight_smile:

Now my next concern is that in regards to brace_style it works but it doesn't work for if statements. If anything, it forces it to use the typical egyptian style coding.

e.g.

fn main()
{
    let mut x = 10;
    if x == 10 {
        x += 20;
    }
}

I am not too sure which specific setting to change.

And in regards to struct_field_align_threshold, while it also works, the problem is that it is only limited to the amount of characters, so for instance if I exceed 20 then it will not format it e.g.

struct Color
{
    red: u32,
    blue: u32,
    greenasds: u32,
    asddddddddddssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssdd: i32,
}

I want it to be infinity instead of having some form of limit and on top of that I want it to add even more spaces after the variable and to add a colon not at the variable but just before the type of variable.

e.g.

struct Color
{
    red             : u32,
    blue            : u32,
    greenasds       : u32,
}

Would you happen to know how I would format it like this?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.