Apply formatting rules
ReSharper | Edit | Reformat Code
CtrlAltEnter (ReSharper_ReformatCode
)
tip
To find and adjust formatting and syntax style settings that affect a specific place in your code, select that code block, press AltEnter and choose Reformat and cleanup | Configure code style.
When you type code in the editor, ReSharper reformats expressions as soon as you type a semicolon ;
and reformats blocks of code as soon as you type a closing brace }
. If necessary, you can disable these kinds of auto-formatting on the Environment | Editor | Behavior page of ReSharper options Alt+R, O.
ReSharper also lets you automatically apply your formatting rules when you paste code. By default, pasted code is not fully reformatted but only indentation settings are applied. If you want to fully format pasted code, or disable formatting on paste, use the Auto-format on paste selector on the Environment | Editor | Behavior page of ReSharper options Alt+R, O.
tip
Even if auto-formatting or auto-indenting on paste are enabled, you can paste code without reformatting it: press Ctrl+Z right after pasting, and only formatting will be undone.
Note that by default, Visual Studio applies its own formatting rules for edited and pasted code. If you are using ReSharper auto-formatting features, we recommend that you disable Visual Studio auto-formatting. You can do it in Visual Studio options (Tools | Options). For C# for example, clear the corresponding check-boxes on the Text Editor | C# | Code Style | Formatting | General options page.
ReSharper provides several ways to reformat existing code. In short, you can always use CtrlAltEnter, but there are a few details and alternatives described below.
tip
If reformatting C# code is not an option, you can enable Virtual Formatter and view reformatted code without actually changing the sources.
Select the scope where you want to reformat code:
Make a selection in the editor to reformat code in the selection.
Place the caret anywhere in the file to reformat code in the file.
Select one or more items in the Solution Explorer to reformat code in files under these items and their child items.
Press CtrlAltEnter or choose ReSharper | Edit | Reformat Code from the main menu. Alternatively, you can press CtrlShift0A, start typing the command name in the popup, and then choose it there.
You can reformat code in the current selection right from the AltEnter list.
In the editor, select a block of code that you want to reformat.
Press AltEnter and choose Reformat and cleanup | Reformat selection.
Alternatively, you can use code cleanup to reformat code in any scope. Code cleanup may be helpful if you want to combine code reformatting with applying other code styles.
note
Code cleanup is not applied to generated code. By default, ReSharper provides the list of file masks and regions for generated code typically produced by Visual Studio (for example, Windows Form Designer-generated code). If necessary, you can edit the list of generated items on the Code Inspection | Generated Code page of ReSharper options Alt+R, O.
Select the scope where you want to reformat code:
Make a selection in the editor to reformat code in the selection.
Place the caret anywhere in the file to reformat code in the file.
Select one or more items in the Solution Explorer to reformat code in files under these items and their child items.
Do one of the following:
Press Ctrl0E,0C or choose ReSharper | Edit | Cleanup Code… from the main menu.
Right-click anywhere in the text editor or right-click the selection and choose Cleanup Code in the context menu.
In the Code Cleanup dialog that opens, select the Built-in: Reformat Code profile.
Click Run. ReSharper will reformat code in the selected scope according to your formatting preferences.
note
The Built-in: Reformat Code profile does not reformat XML doc comments. You can use either Built-in: Full Cleanup profile or create your own custom cleanup profile for this purpose. For more information, refer to Reformat XML Doc comments.
If you want to reformat code without opening the Code Cleanup dialog to choose a profile, you can bind the default Reformat Code profile to the silent cleanup and run it by pressing Ctrl0E,0F. You can also create a custom cleanup profile that would combine code reformatting with other code style tasks.
You can apply formatting preferences every time you save changes in a file to make sure that your edits always comply with your code style.
Select ReSharper | Options from the main menu or press Alt+R O.
Go to the cleanup profiles settings page: Code Editing | Code Cleanup | Profiles.
Select the Built-in: Reformat Code profile and click Set as default (the default profile is also used for silent cleanup).
Go to the Code Editing | Code Cleanup | General options page and select Automatically run cleanup when saving a file.
Optionally, you can restrict automatic cleanup to specific files by a file mask.
You can also select Only changed parts of file to make sure that cleanup is applied to the code affected by your changes, and the rest of the code in the file is not modified.
If you choose to clean up only the changed parts of files, you can fine-tune this behavior with the following options:
Perform full cleanup if partial cleanup is not possible — depending on this option, ReSharper will either clean up the whole file or skip the file if it is not possible to restrict the cleanup to the modified parts of the file.
Allow cleanup on save for shared files — use this option to enable or disable partial cleanup in files that are included in the solution several times, such as files from shared projects.
Click Save in the dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save To selector. For more information, see manage and share resharper settings.
The next time you finish editing and save the file (Ctrl+S) or all files (Ctrl+Shift+S) , ReSharper will clean up the affected files using the selected profile. If the default cleanup profile is not selected, ReSharper will prompt you to choose one of the profiles.
In C# and C++, code formatting style can be also maintained using the code inspection features. ReSharper provides several dozens of formatting inspections, which are all disabled by default.
Depending on the solution, your codebase may have hundreds of minor formatting violations, like different indent sizes across methods, different case-label indents in switch-statements, missing line breaks, and so on. Enabling code inspections for all those violations could be too noisy.
On the other hand, there are formatting problems that decrease code readability or may influence your understanding of the code. For example:
multiple type members on one same line,
multiple statements on one line,
incorrect indent around child statement,
incorrect indent around declaration braces.
For such formatting problems, we recommend enabling code inspections.
You can apply some formatting rules 'softly', meaning that such rules will be applied when ReSharper produces new code with code completion and code generation features, applies code templates and performs refactorings, but these rules will be ignored when you reformat existing code (for example, with CtrlAltEnter).
To keep formatting rules less strict, use the Keep existing..., Keep max blank lines, and Simple wrap preferences for specific contexts. You can check or change the configuration of those preferences in ReSharper options Alt+R, O:
ReSharper also allows you to use explicit actions rather than formatting rules to reformat similar code in different ways. These actions are most helpful when you opt to keep existing formatting for the related rules. However, if you have stricter settings, then the corresponding explicit formatting actions will not work because such configurations mean that formatting rules should always be applied in the affected contexts. For example, if Keep existing... preferences are disabled, then the wrap/chop/spacious/compact commands described below will render the same result. If some wrapping rules are set to Chop..., code in the corresponding contexts will always be chopped even if you invoke wrap/compact commands.
The Wrap long line and Chop long line actions become available when your caret is on a line, which is longer than the allowed maximum line length — you can configure it with the Right margin (columns) preference on the Code Editing | C# | Formatting Style | Line Breaks and Wrapping page of ReSharper options Alt+R, O.
You can press AltEnter and choose one of the actions to add the necessary line breaks.
The Wrap long line will add just enough line breaks to make sure that the configured line length is not exceeded:
Before reformatting
var reportHtml = CreateReport("359CD313-7C7E-49B2-A2AB-E12FDFD1319D", DateTimeOffset.UtcNow.AddMonths(-1), DateTimeOffset.UtcNow, useRichText: true);
After reformatting
var reportHtml = CreateReport("359CD313-7C7E-49B2-A2AB-E12FDFD1319D", DateTimeOffset.UtcNow.AddMonths(-1), DateTimeOffset.UtcNow, useRichText: true);
The Chop long line will add line breaks for each entity on the line:
Before reformatting
var reportHtml = CreateReport("359CD313-7C7E-49B2-A2AB-E12FDFD1319D", DateTimeOffset.UtcNow.AddMonths(-1), DateTimeOffset.UtcNow, useRichText: true);
After reformatting
var reportHtml = CreateReport("359CD313-7C7E-49B2-A2AB-E12FDFD1319D", DateTimeOffset.UtcNow.AddMonths(-1), DateTimeOffset.UtcNow, useRichText: true);
Another set of actions for explicitly reformatting selected code helps you enforce more compact (fewer line breaks) or more spacious (more line breaks) style.
To invoke these actions, select a block of code (you can press CtrlAlt0→ multiple times to select a logical block) , press AltEnter and choose Reformat and cleanup | Reformat selection.
If you have too many line breaks, which do not help read the code, it may be a good idea to remove them. In the example below, the empty getters and setters do not bring any useful information and can be safely placed on single lines. You can do this with the Compact format (fewer line breaks) action:
Before reformatting
public string Name{ get; set;}public int Age{ get; set;}
After reformatting
public string Name { get; set; }public int Age { get; set; }
You may want to use Spacious format (chop) to make a multi-line code block even easier to read. For example, you may want to add more line breaks in a complicated if
statement or a heavy XElement
constructor call:
Before reformatting
summaryTable.Add(new XElement("tr", new XElement("td", XmlHelpers.CreateHyperlink(inspection.Text, CodeInspectionHelpers.TryGetStaticHref(inspection.Id), null, true), new XComment(compoundName)), new XElement("td", GetSeverityLink(inspection.Severity))));
After reformatting
summaryTable.Add(new XElement("tr", new XElement("td", XmlHelpers.CreateHyperlink(inspection.Text, CodeInspectionHelpers.TryGetStaticHref(inspection.Id), null, true), new XComment(compoundName)), new XElement("td", GetSeverityLink(inspection.Severity))));
The Strict format action will reformat the selection ignoring any existing line breaks independently of how the Keep existing... preferences are configured; the Reindent only action will fix indentation without applying any other formatting preferences.