Configuring code formatting from code selection with ReSharper

In our previous posts, we looked at a few examples of the various improvements we made to the code formatting engine in ReSharper 2017.3 EAP and Rider 2017.3 EAP.

While we now have great control over how our source code is formatted, it may be like an airplane cockpit with too many options. In this post, we’ll look at the easiest way to tune the code formatting engine to our liking!

This post is part of a series around changes in the ReSharper and Rider code formatter:

Figuring out which code style settings apply to a block of code

With all of the existing (and new) code formatter changes, figuring out which settings apply to a block of code can be overwhelming. Fortunately, ReSharper provides an easy way to do this. Note that the following is currently only available in ReSharper, and is yet to be implemented in Rider.

To see which settings affect a code block, we can select it, press Alt-Enter and choose Format selection | Configure. ReSharper will then provide an overview of all settings that apply to that selection:
View formatter settings for block

We can use this knowledge to configure the code style to our liking, by editing them directly in the ReSharper options or by adding a specific code style in comments.

The Configure format action not only works for C#, but also for C++, JavaScript, TypeScript, HTML and Protobuf! And it’s not read-only either. We can open the dialog and tweak formatting settings, seeing their effect on our own code in the editor!
Preview code changes on our own code

We can then save our formatting changes for our own use, or if we’re looking to modify a team-wide code style, save them to a solution team-shared settings layer.

This concludes our series about the code formatter improvements made in ReSharper and Rider 2017.3 Early Access Programs. Download ReSharper 2017.3 EAP and give it a try!

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

3 Responses to Configuring code formatting from code selection with ReSharper

  1. Joe White says:

    Wow, that looks freaking awesome!

    Does the “Save to” dropdown have an option to “save” the settings as comment directives?

  2. Andrei Vinersan says:

    Nice solution

Leave a Reply

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