Resharper C# Formatting Style shows “new” on new l

2020-06-03 07:11发布

问题:

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>

回答1:

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:

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...