Resharper C# Formatting Style shows "new" on new line instead of same line when chopping long lines
Asked Answered
S

1

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>
Selfregulating answered 21/12, 2012 at 7:4 Comment(5)
Have you checked the JetBrains Resharper support forums for help?Dunt
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.Selfregulating
I'm also curious. I find this behavior annoying, and I only started noticing it when I moved to R# 7.1Clark
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.Laurellaurella
For future reference, your Edit 2 would have been better as an answer. Also was was a support/YouTrack issue ever created?Recall
A
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...

Andiron answered 21/12, 2012 at 9:37 Comment(4)
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?Selfregulating
I've fiddled around with the settings myself and I haven't found a perfect solution either. Check my revised answer.Andiron
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.Selfregulating
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 positonHebdomad

© 2022 - 2024 — McMap. All rights reserved.