8

So Resharper is putting a line break before the "new" in my code when re-formatting it as follows:

var foo = new Foo
{
    Bar = null,
    Baz =
        new Baz
        {
            Bap = null,
            Bork = null,
            Help =
                new PweaseHelp
                {
                    Korben = null,
                    Dallas = null,
                    Multipass = null
                },
            Me =
                new ClearlyMyAbilityToUnderstandResharperSettingsIs(
                    null),
        }
};

But I'd really like it too do this:

var foo = new Foo
{
    Bar = null,
    Baz = new Baz
    {
        Bap = null,
        Bork = null,
        Help = new PweaseHelp
        {
            Korben = null,
            Dallas = null,
            Multipass = null
        },
        Me = new ClearlyMyAbilityToUnderstandResharperSettingsIs(null),
    }
};

I've delved though all the settings I have in my .DotSettings file(s) and I can't work out what is causing it... Any help would be most appreciated :)

EDIT 2 (UPDATED)

Here are the R# settings that seem to be getting me close to what I have listed, you'll still see the new line after then equals sign (with the listed configuration) unless you select "chop always" for "wrap invocation arguments" and "wrap object and collection initializer" (as suggested by Kristian).

The problem with "chop always" is that you'll have really short method calls and object/collection initializers also chopping all the time, which looks bad, so I think what we want is:

Don't put a new line after the equals sign for method calls / object/collection initializers (but I can't find that setting anywhere, so it could be a bug or feature of R#).

I'll try to raise it on the R# forums / support and report back my findings.

<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTILINE_ARRAY_AND_OBJECT_INITIALIZER/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CONTINUOUS_INDENT_MULTIPLIER/@EntryValue">1</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EMPTY_BLOCK_STYLE/@EntryValue">TOGETHER_SAME_LINE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_ANONYMOUS_METHOD_BLOCK/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TRAILING_COMMENT/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">150</s:Int64>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_PARAMETERS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
5
  • 1
    Have you checked the JetBrains Resharper support forums for help? Commented Dec 21, 2012 at 7:06
  • I haven't, but you make a good point, I've checked Stack Overflow, but not the R# forums. I'll do that and post back here what I find :) thanks. Commented Dec 21, 2012 at 7:38
  • I'm also curious. I find this behavior annoying, and I only started noticing it when I moved to R# 7.1 Commented Dec 21, 2012 at 8:47
  • This is why I don't like resharper...I mean who in the developer community actually writes code like that!? I think I'll stick with StyleCop and FXCop. Commented Dec 21, 2012 at 15:01
  • For future reference, your Edit 2 would have been better as an answer. Also was was a support/YouTrack issue ever created? Commented Oct 10, 2017 at 20:55

1 Answer 1

5

If you are using R# 7.1 it could be due to the new Code Formatting Improvements. All this behavior can easily be configured under R# options:

Resharper C# Formatting Style

EDIT: With my settings, the closest I've come to your proposed solution is setting Wrap object and collection initializer to Chop always, but then, for some reason, it ignores the "Array and object initializer" setting under Braces layout and puts the opening brace on the same line, like this:

var foo = new Foo {
    Bar = null,
    Baz = new Baz {
        Bap = null,
        Bork = null,
        Help = new PweaseHelp {
            Korben = null,
            Dallas = null,
            Multipass = null
        },
        Me = new ClearlyMyAbilityToUnderstandResharperSettingsIs(null),
    }
};

It could be some other settings I have that is causing this, but to me this looks like it's bugged. Maybe you should contact ReSharper Support and have them take a look at it...

Sign up to request clarification or add additional context in comments.

4 Comments

I've looked there and I can't find a single setting that will fix my issue... I'm wondering if it is a new feature in 7.x that doesn't have a corresponding setting?
I've fiddled around with the settings myself and I haven't found a perfect solution either. Check my revised answer.
Actually that works for me! :D Thank you. The R# preview shows it wrapping the open brace on the same line, but when I actually go back to my code and do CTRL+ALT+SHIFT+F it formats it correctly as per my example above.
How did you force it to put only one tab character after opening curly brace in object initializers? In my code it makes offset to be at least as wide, as opening brace positon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.