Code Formatting Improvements in ReSharper 7.1

Code formatting is, and will always remain, a sensitive subject: with many companies having their own specific standards, it’s very difficult to please everyone. In ReSharper 7.1, we’ve taken another shot at smoothing out the formatting experience with more options and settings, many of which were requested by our customers.

Aligning Multiline Constructs

It has long been an issue that long method calls, whether it be in fluent interfaces or LINQ, resulted in code that looked less than agreeable. What we’ve done to remedy this is added two options to control the way long method calls are wrapped.

The first option is one to Wrap chained method calls:

The three supported options are as follows:

  • Chop always, as its name suggests, ensures that chain method calls are always split up on the period operator.
  • Simple wrap wraps method calls when the maximum line length has been reached. Multiple chained calls can appear on a single line.
  • Chop if long wraps method calls when the maximum line length of the call chain has been reached, with each of the calls appearing on a separate line.

ReSharper also has an option called Prefer wrap after “.” in method calls just in case you want parts of the chain to always end with the dot rather than start with one:

Whether or not the methods are actually aligned on the dot is controlled by yet another option under Other|Align Multiline Constructs called Chained method calls. Turning this off causes the wrapped line to be indented instead of dot-aligned:

XML and XML Comment Formatting

In ReSharper 7.1, XML formatting options have had a bit of a redesign. Also, we’ve given XML Doc Comments a separate category, so you can now control the two independently.

Broadly speaking, the formatting style of both ordinary XML and XML Doc Comments is supported via the following categories:

  • Line wrapping determines how XML entities are wrapped.
  • Processing instructions can also be formatted according to a set of rules.
  • Elements that are Inside of tag header (i.e., attributes and spacing between them) can also be controlled. For example, you can determine if there’s a space after the last attribute, or whether attributes on new lines are indented or not.
  • Tag content settings determine what happens to text that’s placed between various XML tags. You can control wrapping, line breaks and indentation.
  • Finally, settings for elements Around tags determine if and when tags get placed on a separate line. XML Doc Comments also have a setting that lets you specify tags that are always placed on a separate line regardless of the overall settings — handy if you want tags like <summary> to always occupy a separate line.

Wе’vе mаdе RеShаrреr асt а littlе lеss аggrеssivе with rеsресt tо yоur linе brеаks, choosing to respect them where it makes sense. If for some reason you prefer the old behavior, you will need to navigate to XML | Formatting Style in ReSharper’s options and:

  • Set the value in Tag content | Line breaks after header and before footer when tag is longer then to 20
  • Set the value the Attributes format to ‘On single line’ under both the Inside of tag header and Processing Instructions headings
  • Set the check box in Around tags | Place singleline tags on new line

Alignment and Indentation Improvements

Many users have corporate policies that are very strict with regard to tabs and spaces that are used for both indentation and alignment. To provide a better experience for our users, we have introduced two options that impact the way indentation is carried out:

The first of these options concerns indentation in multi-language files. You can have ReSharper use a global indentation setting for every type of additional language that’s being used in a multi-language file; or, alternatively, you can indent each language using its own settings as specified in ReSharper’s options under Code Editing | [Language] | Formatting Style.

The second setting concerns the uses of tabs. If the ‘Keep tabs’ option is selected under Visual Studio’s Options (under Text Editor | All Languages | Tabs), these settings determine how tabs are used for indentation and alignment. The options are as follows:

  • Use spaces — this tells ReSharper use tabs for indentation and spaces for alignment.
  • Use only tabs only uses tabs for alignment, which may cause misalignment in case a block of code does not fall on a tab boundary.
  • Mix tabs and spaces for optimal fill uses a combination of tabs and spaces for precisely aligning code.

Web Formatting Improvements

By popular demand, we have added a setting for ASP.NET and Razor projects, so you can use the K&R brace style in ASP.NET while continuing to use the BSD style in C#:

We’ve also added several options to the JavaScript formatter. Specifically, we’ve added options for specifying brace layout for functions passed as parameters into other functions, and have added an option to indent object literals, arrays and nested expressions. Additionally, we’ve introduced an option to add an empty space in braces inside an object expression.

Also, as previously mentioned, web languages can now elect whether or not to use the indentation settings of their parent file. This means that a block of C# code embedded in an HTML file can either use the C# settings or the HTML settings depending on the chosen option.

Odds and Ends

For all fans of the K&R brace style, we’ve added several new settings concerning the rules for inserting blank lines in code:

We’ve also added some options for specifying whether or not attributes should be placed on the same line as single- and multi-line property accessors:

And finally, we’ve put in extra work on proper splitting up of compound conditions in if, while and do statements:

It’s all you!

Most of the features described here have been implemented based on user feedback, for which we thank you! If there’s something else that you find missing, please let us now by posting your use case on our issue tracker.

If you’re impatient to try out these and other improvements, download ReSharper 7.1 nightly builds.

This entry was posted in How-To's, Other, ReSharper Tips&Tricks and tagged , , . Bookmark the permalink.

9 Responses to Code Formatting Improvements in ReSharper 7.1

  1. Mike Ward says:

    We have a corporate policy that all “if” statements have to use braces. This makes single line statements impossible with the ReSharper formatter. Example:

    if (something) return; // Resharper formatter leaves this unchanged

    if (something) { return; }

    Resharper formatter will reformat as follows:

    if (something)
    {
    return;
    }

    Or if you’re using K+R style then

    if (something) {
    return;
    }

    Here’s wishing for a single line if override

  2. Dmitri Nesteruk says:

    @Mike I see what you’re saying. Seems like out of the four “Braces in ‘if-else’ statement” options that we have, none would let you preserve the if (x) { return y; } notation. I’ve added a feature request on our issue tracker, please feel free to vote for it.

  3. Amir says:

    Trying to work with Linq Expressions in Resharper is always tough. The standard way people do linq expressions is as below

    Expression.Property(
    Expression.Call(
    someExpression,
    someMethodInfo,
    param1,
    param2
    ),
    somePropertyInfo
    )

    Note the ending parenthesis. I’d like an option in resharper that could match that. Resharper always adds an extra tab before each ending parenthesis.

  4. Amir says:

    Apparently the formatting got screwed up, I’m going to explicitly type t for each tab character I wanted.

    Expression.Property(
    tExpression.Call(
    ttmyExpression,
    ttmyMethodInfo,
    ttparam1,
    ttparam2
    t),
    tmyPropertyInfo
    )

  5. patrick says:

    It’d be cool if there was an option to have closing parentheses not indented for multi-line calls, e.g.

    glorious(
    arg1,
    arg2,
    arg3
    )

    vs

    glorious(
    arg1,
    arg2,
    arg3
    )

    (if there is a way to do this please let me know :D)

  6. Dmitri Nesteruk says:

    @Amir @patrick Thanks for your comments! We continue to improve the formatter – meanwhile, you can post feature requests http://youtrack.jetbrains.net and we’ll consider them for future releases. Thanks!

  7. Dmitri Nesteruk says:

    @Amir @patrick A colleague of mine has pointed out that what you describe is a known problem described here — feel free to vote for it.

  8. Mike Ward says:

    Multiple using statements get formatted as follows:

    using (var x = …)
    {
    using (var y = …)
    {

    }
    }

    Would prefer:

    using (var x = …)
    using (var y = …)
    {

    }

    To reduce nesting.

  9. Dmitri Nesteruk says:

    @Mike we do actually have a setting in Code Editing → C# → Formatting Style → Other → Indent nested “using” statements. However, if blocks are already delimited by curly braces, the formatter will not remove those for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">