How to stop Resharper from line breaking after return keyword for long lines?
Asked Answered
S

2

32

When I auto format with Resharper CTRL + ALT + SHIFT + F for lines longer than max line length (in my case say it's 80 characters), I get the following:

    return
        View(new ViewModel
        {
            Identifier = identifier,
            Files = service.AllFiles()
        });

But what I really want is it not to wrap after the "return" keyword (i.e. not have the return keyword on a line all on its own), like so:

    return View(new ViewModel
    {
        Identifier = identifier,
        Files = service.AllFiles()
    });

Does anyone know how to "configure" Resharper to make this happen? :)

Here's another example, here's what I'm seeing now:

    return
        repository.Session.CreateCriteria(typeof(SomeType))
                  .Add(Expression.Eq("Identifier", identifier))
                  .UniqueResult<SomeType>();

When I really want to see:

    return repository.Session.CreateCriteria(typeof(SomeType))
                     .Add(Expression.Eq("Identifier", identifier))
                     .UniqueResult<SomeType>();

UPDATE:

Here is "chop always":

    return View(new OrganisationFileLoadViewModel
    {
        Identifier = identifier,
        AllExistingOrganisationFiles = nmdsOrganisationFileLoadService.AllNMDSOrganisationFiles()
    });

Here is "chop if long":

    return
        View(new OrganisationFileLoadViewModel
        {
            Identifier = identifier,
            AllExistingOrganisationFiles = nmdsOrganisationFileLoadService.AllNMDSOrganisationFiles()
        });
Skivvy answered 11/12, 2013 at 3:4 Comment(4)
Have you reported this bug to JetBrains?Tjaden
I'm happy to if it's definitely a bug and not just a setting that I have incorrect...?Skivvy
It's definitely a bug.Tjaden
There are similiar issues already reported, although none of them were fixed - See youtrack.jetbrains.com/issue/RSRP-274953Leyden
F
43

Resharper -> Options -> (Code Editing) C# -> Formatting Style -> Line Breaks and Wrapping

There are a lot of settings for line wrapping. The default for Wrap long lines is normally 120 characters. This may be triggering your break since you are set to 80 or Resharper 8.0 may have a newer option for return. The path above is for 7.0, but I believe it is the same or at least similar to 8.0.

The nice is that they show you examples for the changes you make so you don't have to test it right away.

Figure answered 11/12, 2013 at 3:12 Comment(4)
I'm looking for an option to stop R# from putting the "return" keyword on a line on its own (as shown in my example above). I can't find that option in the section you're referring to.Skivvy
This will just make the problem less likely to appear. It doesn't fix it. I have ReSharper set to break after 120 characters, but very long lines still break in awkward places.Denims
Also, I'm wrapping at 150 characters (fits on my 1920x1200 screen with big font nicely), but of course I still get wrapping occasionally and I find myself manually introducing intermediate variables just to avoid the wrapping "gunk" in R#, which is just terrible and I hate myself for doing it...Skivvy
Yea, I wasn't necessarily saying it could be done. Just pointing to where the options were for line wraps. You may want to request an option for a future release with JetBrains.Figure
S
2

There is no special option to turn "wrapping after return" OFF.

1) I was not able to reproduce a similar code formatting as shown in the first code snippet. However, I recommend you trying to change this setting to "Simple Wrap": ReSharper | Options | Code Editing | C# | Formatting Style | Line Breaks and Wrapping | Line Wrapping | Wrap invocation arguments.

2) In my case, the following changing helps me: ReSharper | Options | Code Editing | C# | Formatting Style | Line Breaks and Wrapping | Line Wrapping | Wrap chained method calls | Select "Chop always".

Saville answered 11/12, 2013 at 7:38 Comment(5)
I've changed them all to "Simple Wrap", which didn't fix the break after the return...Skivvy
Then I also changed them all back to "chop if long" that doesn't help either (return is still on a line by itself).Skivvy
Then when I go to "chop always" I get the correct behavior (i.e. it doesn't put the return on a line on its own)! #WINNING, but then all my code looks terrible as it's always chopping, which makes what should be a small chained method call or object / collection initializer (i.e. should fit on one line) take up 3-4 lines unnecessarily...Skivvy
What I really want is a) "chop if long" + c) "but don't put 'return' on a line on its own"... It seems like a defect to me, as it does the correct thing on "chop always" but the wrong thing on "chop if long"...Skivvy
Added UPDATE above which shows the difference in behaviour between "chop always" and "chop if long"...Skivvy

© 2022 - 2024 — McMap. All rights reserved.