Managing and Applying 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:
- Auto-formatting edited and pasted code
- Reformatting existing code
- Configuring code formatting rules
- Storing and sharing formatting rules
Auto-formatting edited and pasted code
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 page of ReSharper options.
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 to disable formatting on paste, use the Auto-format on paste selector on the page of ReSharper options.
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 you to disable Visual Studio auto-formatting. You can do it in Visual Studio options (). For C# for example, clear the corresponding check-boxes on the options page.
Reformatting existing code
ReSharper provides several ways to reformat existing code. In short, you can always use Ctrl+Alt+L, but there are a few details and alternatives described below.
To reformat existing code in any scope
- Select the scope where you want to reformat code:
- Press Ctrl+Alt+L or choose in the main menu.
You can reformat code in the current selection right from the Alt+Enter action list.
To reformat code in the current selection
Alternatively, you can use code cleanup to reformat code in any scope. Code cleanup may be helpful if you want to combine reformatting code with applying other code styles.
To reformat code with Code Cleanup
- Select the scope where you want to reformat code:
- Do one of the following:
- In the Code Cleanup dialog that opens, select the Default: Reformat Code profile.
- 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, 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
To configure code formatting rules
- Choose in the main menu.
- Use the page to check and configure the options that are applied in all languages.
- Use the 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.
As an alternative to digging through options pages, you can select a block of code and configure only those formatting rules that are applicable to this block. You will be able to observe how modified formatter settings affect code in the selected block right in the editor.
To configure formatting rules for selected code
- In the editor, select a block of code that you want to reformat.
- Press Alt+Enter or click on the action indicator to the left of the caret to open the action list.
- Choose .
- In the Configure format dialog that opens, you will see all formatting rules that affect the selected code block. The code block itself will be surrounded with a dotted frame:
- As you change the formatting rules, you will see how they affect the code in the selected block.
- 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. ReSharper will save your modifications and reformat the code block.
Storing and sharing formatting rules
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.
ReSharper also supports EditorConfig format, where formatting rules can be stored in .editorconfig files on different levels of your solution hierarchy. These files are normally put under VCS so that settings defined there are shared among the project team.
ReSharper lets you use .editorconfig files to define any of its formatting preferences that are available in ReSharper settings (in the Options dialog). You can find names and descriptions of supported EditorConfig properties in the EditorConfig reference.
Another important thing to know when sharing formatting options is that indent style (tabs/spaces) and size are taken from Visual Studio options by default. If you want to share these preferences for specific language, clear the Use indent style and size from Visual Studio check box on the page of ReSharper options and save the change to a shared settings layer.
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
- Managing and Applying Code Formatting Rules
- File and Type Layout
- File Header Style
- Code Syntax Style: Namespace Imports
- Code Syntax Style: Implicit/Explicit Typing
- Using Auto-Properties
- Making Fields Readonly
- Code Syntax Style: Multiple Attributes
- Code Syntax Style: Modifiers
- Code Syntax Style: Optional Member Qualifiers
- Reformatting XML Doc Comments
- Removing Code Redundancies