ReSharper 7.1 object initializer formatting
Asked Answered
C

2

8

When doing something like this:

MyObject tmp = new MyObject();
tmp.Prop = "Hello";

ReSharper tells me to 'Use object initializer', so I let it reformat the code, and I get something like this:

MyObject tmp = new MyObject {
                                Prop = "Hello"
                            };

However, I would like the first brace to be on the second line, like this:

MyObject tmp = new MyObject 
                            {
                                Prop = "Hello"
                            };

But I can't find any setting for this anywhere. I have the setting 'C# -> Formatting Style -> Braces Layout -> Array and object initializer' set to 'At next line (BSD style)'

Could there be some other setting interfering with this, preventing the formatting I want?

Edit: If I manually format the code like I want it, it will automatically reformat (to the wrong format) when I enter the semi colon.

Catalan answered 8/2, 2013 at 7:33 Comment(6)
I've recently installed StyleCop, if that changes anything...Catalan
Maybe this question/answer can help you: Resharper C# Formatting Style shows “new” on new line instead of same line when chopping long linesUnwholesome
@KristianHellang It sure helped.Catalan
@KristianHellang But I still can't get the formatting I want...Catalan
That is very strange, I have all options at C# -> Formatting Style -> Braces Layout set to 'At next line (BSD style)', and it formats as you want. Do you have all of them set to this value? Please try to set them that way. Also please try to disable StyleCop.Lanoralanose
The path to the setting was instantly helpful!Marienthal
C
12

So I finally found the setting that was messing with the formatting:

enter image description here

Catalan answered 11/2, 2013 at 7:23 Comment(1)
And if you have multiple properties and you want each to go on a new line, also select to Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping -> Line Wrapping -> Wrap object and collection initializer => Chop Always.Elmaelmajian
F
4

Sometimes i need to set the same option in VS and Resharper.

In Resharper it's like you said:

C# -> Formatting Style -> Braces Layout -> Array and object initializer' set to 'At next line (BSD style)

In Visual Studio:

Tools -> Options... -> Text Editor -> C# -> Formatting -> New Lines -> Place open brace on new line for object initialzers
Foreclosure answered 8/2, 2013 at 9:21 Comment(1)
Without the VS tweak in addition to the RS tweak, reformatting was still doing it wrong, thank you!Fe

© 2022 - 2024 — McMap. All rights reserved.