How to change the formatting of the "Use Object Initializer" refactoring in Resharper?
Asked Answered
O

3

27

When I refactor the following line:

Employee e = new Employee();
e.First = "Frank";
e.Last = "Rizzo";

using Resharper's "Use Object Initializer", I get the following:

Employee e = new Employee
             {
                 First = "Frank",
                 Last = "Rizzo"
             };

I really hate this type of formatting because with longer object names and variables it just gets out of control. How can I get Resharper to do the following?

Employee e = new Employee
{
    First = "Frank",
    Last = "Rizzo"
};
Overpay answered 20/1, 2010 at 20:57 Comment(5)
I wonder who came up with this idea in the first place. No one in their right mind would indent initializers like that.Wateriness
I have it on good authority that it was someone in their left mind.Denticle
no no don't change the Resharper default settings, it's perfect :-)Brachyuran
related: #747851Redfield
Here are some useful Resharper formatting settings… daniellang.net/5-resharper-settings-for-c4-codingGauldin
T
38

You can get very close to what you want in Resharper 4.5.

Resharper>Options

Languages>C#>Formatting Style>Other

under Other uncheck "Indent array, object and collection initializer block."

Tuneful answered 20/1, 2010 at 23:17 Comment(3)
Using this with Version 5 gives 1 tab indent instead of 0. better than nothing...Redfield
found answer to that on #747851Redfield
Does anyone have an answer for ReSharper 7.1.2? It is already unchecked but still indents wrongly.Octa
D
3

In Resharper 7 this option is here:

Resharper / Option / C# / Formatting Style / Braces Layout set Array and object initializer to: "At next line (BSD style)"

Delphine answered 30/4, 2013 at 10:51 Comment(3)
I don't know about RS7, but in RS6 that's quite definitely not the right place — the setting in Formatting > Other determines whether the indent set here is relative to the start of the line or to the initializer statement. The live preview on each setting will show you what I mean better than words can explain it :)Batsman
@David Krmpotic is correct about RS7, and RS8 has it in the same place.Sarson
In Reshaper 8 the setting to not indent the object initializer is Resharper / Option / C# / Formatting Style / Other / Align Multiline Constructs / Array, object and collection initializerKynan
H
1

In R# 2018.2.3 This is in:

ReSharper -> Options -> Code Editing -> C# -> Formatting Style -> Line Breaks And Wrapping -> Arrangement of Initializers

There is multiple settings you can play with there, namely:

  • Keep existing arrangement of initializers
  • Max object and collection initializer elements on a single line
Hakon answered 5/10, 2018 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.