Custom Brace formatting with Resharper
Asked Answered
B

5

79

I'm using Resharper 4.5 and I need custom formatting of braces when writing an array or object initializer. Resharper supports some styles:

Gnu Style:

int[] array = new int[]  
                  {  
                      1, 2, 3  
                  }  

but I need:

int[] array = new int[] 
{  
    1, 2, 3  
}  

Is there any way to customize this templates?

Burden answered 14/4, 2009 at 12:41 Comment(0)
A
166

You can customize ReSharper to do just that, you'll need to do the following (All in ReSharper -> Options -> C# -> Formatting Style):

  1. In Braces Layout, set Array and object initializer to At Next line (BSD Style).
  2. In Other, make sure that Continuous line indent multiplier is set to 1.
  3. In Other, make sure that Indent array, object and collection initializer block is unchecked.

You should get the style you want.

Abnormality answered 4/5, 2009 at 9:11 Comment(8)
Too bad someone can't mark this as the correct answer because it obviously is.Expulsion
No, this isn't a solution. This setting doesn't put the first brace on the next line.Fleecy
This needs a step 4: In ''Other'', make sure that 'Indent array, object and collection initializer block' is ''unchecked''.Fleecy
this worked perfectly for me; this has always bugged me so your answer is very much appreciated, thanksOctillion
Any other idea here? I set my resharper as you described, but still having the ugly code here :/Plectognath
Check the answer by @Farah for more recent versions.Alto
In Resharper 8, for step 3, the first word "Indent" was removed. So you need to look for "Array, object and collection initializer block" and uncheck that.Strategic
If you prefer this style for Array, object and collection initializer, there's also an option for Anonymous method body which you probably want to uncheck too.Biamonte
F
9

As of R#7.1 some Option attributes were changed:

  1. In Braces Layout, set Array and object initializer to At Next line (BSD Style).
  2. In Other, make sure that Continuous line indent multiplier is set to 1.

like in older versions and

  1. In Other, at Align Multiline Constructs make sure that Array, object and collection initializer is unchecked.
Farah answered 4/12, 2012 at 9:9 Comment(0)
S
2

This article seems to have an answer similar to what you're looking for:

ReSharper: Fixing array and object initializer indentation

Sibeal answered 29/4, 2009 at 19:30 Comment(0)
H
0

(continuation of previous answers by @igal and @mbx)

As of R# 2017.1 (and possibly earlier versions), do the following:

  1. In Formatting Style -> Braces Layout, set Array and object initializer to At Next line (BSD Style)
  2. In Formatting Style -> Other -> Indentation, make sure that Continuous line indent multiplier is set to 1
  3. In Formatting Style -> Other -> Align Multiline Constructs, make sure that Array, object and collection initializer is unchecked. You may also want to uncheck Anonymous method body if that conforms to the same style.
Hellraiser answered 23/5, 2017 at 15:41 Comment(0)
A
-4

as far as i know you cant, Resharper comes with predefined styles, but maybe if you create your own live template and set it the way you want it will works.

hope this helps.

Aric answered 14/4, 2009 at 16:32 Comment(1)
it seems you really can't....live template does something else though, has no effect on code formatting.Burden

© 2022 - 2024 — McMap. All rights reserved.