Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers

How-To's

Detection of code styles and naming with ReSharper

While ReSharper comes with often-used defaults for code formatting and naming, some developer teams want to use different settings. And while ReSharper can be configured to pretty much any code style out there, there are many settings to tweak: ReSharper has 278 formatting and 76 naming settings for C#!

The latest ReSharper 2018.3 EAP build makes it easier and faster to customize code style by detecting formatting and naming settings from existing code.

Want to configure braces layout? Arrange embedded statements differently? Using the new detect formatting settings from existing code action, ReSharper can analyze and suggest code formatting settings based on existing code. Let’s see!

Detect formatting settings from existing code

After opening some code in the editor, we can use the ReSharper | Edit | Detect formatting settings menu to have ReSharper detect code formatting settings. To detect settings for individual scopes, like for files, projects or the entire solution, we can also use the Detect formatting settings from the context menu in solution explorer.

We can also select a block of code, and then from the Alt+Enter menu select Format Selection | Detect formatting settings…

In the file we have here, ReSharper will detect that we are using a braces layout for types, namespace and method declarations (“At end of line”) that is different from the default.

Detect code formatting from existing code

Code formatter settings can be reviewed and applied to the ReSharper settings on our machine, or saved to a team-shared settings layer so that other developers on our team will have the same code formatting configuration when opening the current solution.

We can also Save as comments, which overrides formatting settings in the current file only.

Save formatting settings as comments to override in the current file only

Having detection of code formatting settings based on existing code makes it quicker to tailor ReSharper to your team’s code style.

Detect naming rules from existing code

In terms of naming conventions, ReSharper defaults to using the Microsoft naming guidelines. However, when a project deviates from these rules, ReSharper and Rider will now automatically detect naming settings in the current solution and update their settings accordingly:

Detect naming settings from existing code

Auto-detected naming settings are saved to a team-shared user-specific settings layer (a .sln.DotSettings file next to our solution).

Current limitations and future improvements

While formatting detection works on small samples (e.g. a couple lines of code), it’s better to provide it a larger sample. The more code ReSharper can use, the better the results will be. Of course, detection will take more time on larger samples.

In the next ReSharper versions, we are planning to enhance the detection of code style settings. Formatting settings should be detected automatically when opening new solutions, just like naming settings.

Download ReSharper 2018.3 EAP and give it a try! Please give us your thoughts about these new features – we’d love to hear your feedback!

image description

Discover more