Detection of code styles and naming with ReSharper

Posted on by Maarten Balliauw

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!

Comments below can no longer be edited.

14 Responses to Detection of code styles and naming with ReSharper

  1. Mad Hatter says:

    December 5, 2018

    That sounds all nice and easy, but feels rather like there’s a lot of code to manage and effort You’ve put in this ‘detection’, while i bet most devs have already these settings sorted out. I think Your dev time could be better spent in other areas, for example delivering some support for SQL.

    Anyway, nice feature.

  2. Stuart says:

    December 5, 2018

    Yes, yes, this is all wonderful.

    But.

    How about them performance improvements? Where are they?

  3. Dew Drop - December 6, 2018 (#2854) - Morning Dew says:

    December 6, 2018

    […] Detection of code styles and naming with ReSharper (Maarten Balliauw) […]

  4. Miroslav Popovic says:

    December 7, 2018

    Any plans for adding Editorconfig support for this – i.e. generate/update Editorconfig file?

  5. Bob Murphy says:

    February 1, 2019

    Is there any way to turn this off? I have to go into the settings and disable it for each solution that I open for the first time since the update. I want to see where code deviates from the standards, but not for the standards to adjust themselves to the code. Also want this to be true for other devs on the team.

    Thanks!

    • Maarten Balliauw says:

      February 4, 2019

      Under e.g. Code Editing | C# | Naming Style, you can save the enable/disable of auto-detecting naming rules to the team shared layer to share it for a solution, or to “This Computer” to se this globally on your box.

    • Alexander Kurakin says:

      February 4, 2019

      @Bob, what ReSharper version do you have installed? In ReSharper 2018.3.2 bug-fix update we’ve fixed very similar issue https://youtrack.jetbrains.com/issue/RSRP-472819 that one could not disable the naming autodetection completely for all solutions.

  6. Joe Nospam says:

    May 25, 2019

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

    That sounds like a contradiction. Is it the team shared one or the user-specific one? It does appear to save to a .user file, which is odd. It even does so when I have the feature disabled.

    • Maarten Balliauw says:

      May 27, 2019

      Good catch, fixed the text!

      • Joe Nospam says:

        June 5, 2019

        But that still doesn’t match the actual behavior, which seems buggy. “It does appear to save to a .user file, which is odd. It even does so when I have the feature disabled.”

Subscribe

Subscribe to .NET Tools updates