Code Formatting Rules
An important aspect of code style is how to format the code, i.e. how to use whitespaces and blank lines to arrange and separate code blocks, whether and how to use tabs for indents, whether and how to wrap long lines, etc.
The extensive set of ReSharper code formatting rules has a default configuration that takes into account default Visual Studio formatting options as well as numerous best practices.
You can configure every detail of formatting rules and enforce the rules in your code. These rules are applied when ReSharper produces new code with code completion and code generation features, code templates and refactorings. The formatting rules can be also applied to the existing code in the current selection, current file, or in a larger scope up to the entire solution.
In this topic:
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 auto-formatiing options on the Environment | Editor | Editor Behavior options page.
You can reformat code in the current selection with the corresponding context action.
To reformat code in the current selection
Alternatively, you can use code cleanup to reformat code in any scope.
To reformat code with Code Cleanup
Select the scope where you want to reformat code:
- Make a selection in the editor to reformat code in the selection.
- Set 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 these items and their child items.
Do one of the following:
- Choose on the main menu.
- Press Ctrl+Alt+F.
- 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 box that opens, select the Default: Reformat Code in the Available Profiles area.
- Click Run. ReSharper will reformat code in the selected scope according to your formatting preferences.
If you want to reformat code without opening the Code Cleanup dialog box, you can bind the default Reformat Code profile to the silent cleanup and run it simply by pressing Ctrl+Shift+Alt+F. You can also create a custom cleanup profile that would combine reformatting code with other code style tasks.
Configuring code formatting rules
Formatting rules can be configured to a very detailed level. For example, you can define whether whitespaces
should be placed around a specific operator or whether to indent nested
Your code formatting preferences are saved using the mechanism of shared settings. Among other things, this mechanism allows you to maintain different preferences for different solutions as well as to keep these preferences under a VCS and automatically share them with your team members.
To configure code formatting rules
- Choose in the main menu.
- Use the Code Editing | General Formatting Style page to check and configure the options that are applied in all languages.
- Use the Code Editing | [Language] | Formatting Style pages to configure language-specific formatting preferences. On these pages, you can use the preview area in the bottom to view how exactly ReSharper applies the specific preference to the code.
- Click Save to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To drop-down list. For more information, see Managing and Sharing ReSharper Settings.
This feature is supported in the following languages/technologies:
The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.
- Naming Style
- Navigating Between Code Issues
- Finding Out What Is Wrong with Your Code
- Code Formatting Rules
- File and Type Layout
- File Header Style
- Namespace Imports Assistance
- Implicit/Explicit Typing
- Using Auto-Properties
- Making Fields Readonly
- Attributes Style
- Arranging Modifiers
- Arranging Optional Member Qualifiers
- Reformatting XML Doc Comments
- Removing Code Redundancies