ReSharper in Detail: Reformatting Code

ReSharper can reformat an arbitrarily selected block of C# or VB.NET code, all code in the current file, all files in a directory, the current project, or even the current solution, according to options configurable in:
 

  • ReSharper | Options | Languages | C# | Formatting Style for C# code, or
  • ReSharper | Options | Languages | Visual Basic .NET | Formatting Style for VB.NET code.

 
Once the formatting settings are configured, you can share them with your fellow team members to ensure uniform code appearance.
 
Whenever you have a file opened in the editor, select a fragment that you want to reformat, or, alternatively, don’t select anything, in which case reformatting will apply to the entire file. Then launch Reformat Code in one of three ways:
 

  • Choose ReSharper | Code | Reformat Code on the main menu.
  • Press Ctrl + Alt + F (no default shortcut in VS scheme).
  • Choose Reformat Code in the context menu.

 

 
The Reformat Code dialog box (shown above) contains several formatting options:
 

  • Optimize ‘using’ directives removes redundant namespace import directives from the source code. Note that this option is disabled when you have a selection – even if it includes the imports section of your file.
  • Shorten references replaces fully qualified names with short names where possible by importing namespaces.
  • Remove redundant ‘this.’ qualifier deletes unnecessary this keywords (note that it doesn’t do the same for VB.NET keyword Me).
  • Reorder type members (for C# code only) reorders type members according to patterns specified in ReSharper Options | Languages | C# | Type Members Layout.

 
After you have reformatted your code, ReSharper saves the state of check-boxes for future use. Subsequently, you can even launch Reformat Code silently, without opening the dialog box, by pressing Ctrl + Shift + Alt + F (or Ctrl+E, F in the Visual Studio keyboard layout).
 
An additional advantage is that you can execute reformatting via Solution Explorer. For example, you can reformat a specific C# code file, or all such files in a project or entire solution. Just right-click the required node and choose Reformat Code….
 
Additional links:
 
Kyle Baley explains how to remove regions with the Reformat Code feature (C# only)
An example of how you can customize a type member layout pattern
Tips for using Reformat Code with aspx pages
Joe White’s detailed review of ReSharper’s reformatting tools

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

11 Responses to ReSharper in Detail: Reformatting Code

  1. Runar says:

    It would be great if the formatter could sort all members by type (like you can today), then by *name*. So what I want is an extra tick box below “Reorder type members” that says “Reorder members alphabetically”.

  2. Joey says:

    KYLE BALEY FTW… that guy gives tips in such an entertaining yet informative way

  3. Mark says:

    There is a default shortcut for reformatting code in VS Scheme, it’s CTRL+K CTRL+F. This is one thing that’s bothered me often about resharper, it’s support for remapping the VS Scheme leaves much to be desired.

  4. rprimrose says:

    I think that the type member layout formatting is fantastic, but it gets killed because of lack of documentation. Of the couple of people who have posted some examples, they even say they don’t understand it.

    Could you please write some posts about how to author this xml to get the most out of the feature. Making the xsd available would also be very helpful.

    Cheers

  5. Jura Gorohovsky says:

    Mark: The shortcut you mentioned must be assigned to VS’s native reformat.

    rprimrose: Thanks for your proposal! I’ll try to get guidelines more specific than those in the default pattern if available. BTW, a more formidable type members layout editor might be implemented after 4.0.

  6. The reformat function is a great addition to resharper. However, don’t use the “reorder type members” option if you have a struct with the Sequential layout. It’ll ruin your calls to native code. Cost me some time to find out what happened with my native calls.

  7. loom says:

    Hello,

    Is there any way to share theses settings among different users (export/import feature)?

    Thanks!

  8. Drew Noakes says:

    The link ‘An example of how you can customize a type member layout pattern’ is dead, unfortunately.

    I’d love to see some documentation that discusses editing the layout pattern.

  9. @Drew Noakes,
    This seems to be the most obscure ReSharper feature, isn’t it?
    We’re planning to either finally author a corresponding help topic in R# 5 help (that would likely reside in “Code Cleanup” section) or post a separate blog entry about it. Either way, expect us to come up with some decent documentation in a few months.

  10. John R says:

    i’m working a contract that’s VB.NET and just upgraded from the c# version to the full version of ReSharper. A bit depressed by the number of features that aren’t available for VB.NET, this being one of the prime ones.

    Any chance that support for type member layouts will make it to the VB refactorings?

  11. @John
    To be honest there’s little chance because, first of all, the cleanup/reformat subsystem is in maintenance mode for quite a while, and second, we don’t get much feature requests on the VB.NET side and subsequently don’t usually schedule getting older features for VB.NET up to par with C#. In most cases however we do make sure that newer features support both C# and VB.NET, such as new refactorings in v8.
    Just to make sure that it’s logged for the future, I suggest that you request making Type Members Layout and other features that you’re missing available for VB.NET by submitting feature requests in ReSharper issue tracker.
    An alternative way to solve your problem could be developing a ReSharper plug-in – that is, if you’re willing to invest time in this endeavour. If you are, we can help you out to get started if the existing guidelines aren’t clear or informative enough.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>