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:
- Code formatting engine updates in ReSharper and Rider
- Different code styles for different blocks of code
- Aligning code in columns
- Configuring code formatting from code selection
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:
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.
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:
1 2 3 4 5 |
// @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)
1 2 3 4 5 6 7 8 9 |
// @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).
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.
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!
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.
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?
Right now it doesn’t, unfortunately, but we have that in plans, maybe for 2018.1 or 2. You can consult https://www.jetbrains.com/help/resharper/EditorConfig_Index.html now.
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
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.
Look forward to refactoring this out. I know the type of devs who would use this.