How to fix ReSharper indentation on object & other initializers
Asked Answered
M

2

23

I have found other duplicates of this question, but they are very old and the solutions are not applicable to R#8, because the settings have changed.

When I do anonymous type or object initialization (any kind of curly brace indentation with content), I want it to be BSD style:

var test = new CustomType
{
    Prop1 = value1,
    Prop2 = value2,
};

...and I set R# Options / Code Editing / C# / Formatting Style / Braces Layout / Array and object initializer to "At next line (BSD style)". However, when I hit enter after adding a new curly brace, I still end up with this:

var test = new CustomType
           {
               Prop1 = value1,
               Prop2 = value2,
           };

...which I believe is Whitesmiths style.

What do I need to do to get R# 8 to not indent braces for initializers?

Manned answered 27/11, 2013 at 15:30 Comment(0)
U
40

There are two settings that together govern this:

  • Array, object and collection initializers: false

Option1

  • Array and Object Initializer: BSD Style

Option2

Update from OP

These settings may have to be set for "This Computer", not just for personal or team-shared options.

Unbridled answered 27/11, 2013 at 15:47 Comment(3)
Those are the settings I have, but they were set in the team-shared and personal settings. Turns out I also needed to change it for "This Computer". Is that to be expected? I thought team-shared were supposed to override computer settings?Manned
Some settings, when at their default setting, are not detected by Resharper as Overwritten, you might have been caught by that 'bug'. In most cases, the most specific settings win (solution, team, personal), but it all depends on the order of the settings files found under "Manage Settings"Unbridled
You know I never knew there was an order to these. I see it now, but my Move up and Move down buttons are disabled for it.Manned
C
0

In my case, I followed the instructions in the accepted answer, and ReSharper was still not formatting the object initializers the way I expected (or the way the answer said it would). After much experimentation, I found that the settings from the StyleCop Extension were driving the formatting instead of those in the team-shared settings file. I had to disable the StyleCop Extension settings layer in order for the desired formatting to occur.

Here's a screenshot from the ReSharper --> Manage Options menu: ReSharper Settings Layers

Carpal answered 24/7, 2018 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.