Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers

How-To's

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.

image description

Discover more