JetBrains Rider 2017.2 Help

Managing and Applying Code Formatting Rules

Code | Reformat Code
Ctrl+Alt+Enter

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 JetBrains Rider code formatting rules has a default configuration that takes into account numerous best practices. You can configure every detail of formatting rules and enforce the rules in your code. These rules are applied when JetBrains Rider produces new code with code completion and code generation features, applies code templates and performs refactorings. The formatting rules can also be applied to the existing code in the current selection, current file, or in a larger scope up to the entire solution.

Auto-formatting edited and pasted code

When you type code in the editor, JetBrains Rider 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 Editor | General | Typing Assistance page of JetBrains Rider settings (Ctrl+Alt+S).

JetBrains Rider 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 Editor | General | Editor Behavior page of JetBrains Rider settings (Ctrl+Alt+S).

Reformatting existing code

JetBrains Rider provides several ways to reformat existing code. In short, you can always use Ctrl+Alt+Enter, but there are a few details and alternatives described below.

To reformat existing code in any scope

  1. 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 files under these items and their child items.
  2. Press Ctrl+Alt+Enter or choose Code | Reformat Code 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

  1. In the editor, select a block of code that you want to reformat.
  2. Press Alt+Enter or click on the action indicator to the left of the caret to open the action list.
  3. Select ThemedIcon CodeCleanupOptionPage Screen Gray Format selection in the action list.
    Reformatting currently selected code

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.

To reformat code with Code Cleanup

  1. 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 files under these items and their child items.
  2. Choose Code | Code Cleanup in the main menu.
  3. In the Code Cleanup dialog that opens, select the Default: Reformat Code profile.
  4. Click OK. JetBrains Rider will reformat code in the selected scope according to your formatting preferences.

Configuring code formatting rules

Formatting rules can be configured to a very high level of detail. For example, you can define whether whitespaces should be placed around a specific operator or whether to indent nested using statements.

To configure code formatting rules

  1. Press Ctrl+Alt+S, or alternatively choose File | Settings on Windows and Linux or Rider | Preferences on macOS.
  2. Use the Editor | Code Style | [Language] pages to configure language-specific formatting preferences. On these pages, you can use the preview area at the bottom to view just how JetBrains Rider applies the specific preference to the code.
  3. Click Save to apply the modifications and let JetBrains Rider 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 layer-based settings.

Storing and sharing formatting rules

Rider stores its code formatting preferences using both directory-based settings (for web-related languages) and layer-based settings (for .NET languages). Both setting-management mechanisms support sharing of settings. Layer-based settings are also compatible with ReSharper.
In the Rider settings dialog, you can look at the icons to see which setting-management mechanism is used for specific language:

Rider: Code formatting settings

JetBrains Rider also supports formatting settings defined in EditorConfig. These settings can be stored in .editorconfig files on different levels of your solution hierarchy. The files are normally put under VCS so that settings defined there are shared among the project team.
JetBrains Rider lets you use EditorConfig to define any of its formatting preferences that are available in JetBrains Rider settings (in the Settings dialog). You can find names and descriptions of supported EditorConfig properties in the EditorConfig reference.

It is important to note that any formatting property defined in an .editorconfig file will override the same property defined in JetBrains Rider settings in the scope where this .editorconfig file applies.

Last modified: 21 November 2017