ReSharper in Detail: Reformatting Code

February 10th, 2008 by Jura Gorohovsky

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

Technorati tags: , , , , ,

9 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. Erwin Bovendeur Says:

    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. Jura Gorohovsky Says:

    @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.

Leave a Reply