Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers

How-To's

Different code styles for different code blocks in ReSharper and Rider

In the previous post about the ReSharper and Rider code formatting engine, we saw an overview of new functionality that is available. In this post, it’s time to get our hands dirty and look at a couple of things in more detail!

This post is part of a series around code formatting changes in ReSharper 2017.3 EAP and Rider 2017.3 EAP:

Enable/disable formatter on selected blocks

One of the most-voted requests was to turn off code formatting on selected code blocks. Many users want to use different formatting styles for different blocks of code, or disable formatting altogether.

Here’s an example. In general, I prefer my methods to be multi-line. However, for simple classes like custom exceptions, I like to make its constructors “one-liners”. In these cases, ReSharper’s formatter respects my general preference and moves the curly braces to new lines:
ReSharper formatter places curly braces on new line

For this case, I’d like to disable code formatting to be able to have single-line constructors. ReSharper and Rider 2017.3 EAP make it possible to do this!

There is a new option Keep existing arrangement for declaration blocks (under Code Editing | C# | Formatting Style | Line Breaks and Wrapping), which gives us the freedom to format single line/multi line as we want. Also there are now options to keep existing arrangements for attributes, expression bodied members, embedded statements, blocks and initializers.

We can disable the formatter for just a block of code, by adding a simple comment: // @formatter:off to disable, // @formatter:on to enable.
Disable code formatter with comment

Unfortunately, this is a bit crude. In essence, we’re telling ReSharper to not enforce common code styles for these blocks.

Instead of disabling the formatter, we can configure it using inline EditorConfig-style settings (see overview of supported settings) and override only one or several settings, keeping common code style settings active.

In the above example, we could keep the default code formatter behavior but override the place_simple_method_on_single_line option:

// @formatter:place_simple_method_on_single_line true
public class BlogPostException : Exception
{
    public BlogPostException() { }
}

All these options (enable/disable formatter, change options with comments, “keep existing arrangement”) allow us to use different code styles for different code blocks.

Some other examples: (note we can also use the value restore to mark the end of an override in code formatter settings)

// @formatter:off/on
...

// @formatter:max_line_length 1000
...
// @formatter:max_line_length restore

// @formatter:keep_user_linebreaks true
...

The formatter can be configured inline for several languages: C#, C++, JavaScript, TypeScript, HTML and Protobuf. A complete list of available options and examples is available from the ReSharper web help.

Wrapping and chopping long lines

Sometimes, a line of code is just too long. A colleague of mine has a simple rule: if you need to scroll right when checking a code review on a phone, lines are too long and should be wrapped.

ReSharper and Rider 2017.3 EAP add two new context actions that can help us wrap and chop long lines of code based on code formatting settings (like max. line length and many more). The Wrap long line action will add just enough line breaks to make sure we do not exceed the configured line length (compact mode), Chop long line uses a more spacious formatting style to determine where to add a newline (spacious mode).
Wrap long line / Chop long line

The compact mode (fewer line breaks) and spacious mode can be applied to any block of code (as long as Keep existing … options are enabled). We can format an if statement, properties and other class members, …  After pressing Alt+Enter, we can use the Format selection action and select how we want to format it.
Format selection - compact or spacious

In the next post, we’ll look at some other new code formatting features, such as aligning code in columns. Stay tuned for more!

Download ReSharper 2017.3 EAP or Rider 2017.3 EAP! We’d love to hear your feedback!

image description