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:

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)

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!

This entry was posted in How-To's and tagged , , , , , , , , . Bookmark the permalink.

6 Responses to Different code styles for different code blocks in ReSharper and Rider

  1. Sam says:

    Nice! Looking forward to trying it out.

    Very excited to see what column code alignment features you have. AlignBy is really popular but I’ve always found it harder than necessary to set up and use, and then R# just wrecks my formatting afterwards regardless.

  2. Joe White says:

    Does ReSharper offer Intellisense completion for the //@formatter directives? Like, if I type “// @formatter:”, will it pop up a completion dropdown that lists the available options?

  3. Drew Noakes says:

    Really like the “chop line” feature. The inverse “make single line” operation would also be great.

    Why introduce a new comment format for the formatter directives? We already have:

    // ReSharper disable once BlahBlah

    • Dmitry Osinovsky says:

      To make single line, select lines, click Alt-Enter > Format selection > Compact. That would work if resulting single line is less than Right margin, or if Wrap long lines is turned off.

      Why introduce a new comment format. The format is used by default in Eclipse and Intellij IDEA, so we decided to go with it for a first implementation. Maybe we would also allow // ReSharper disable formatter in the next versions.

  4. Dev says:

    Look forward to refactoring this out. I know the type of devs who would use this.

Leave a Reply

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