Code Cleanup
Ctrl+E, C
JetBrains Rider allows you to apply formatting and other code style preferences in a bulk mode to instantly eliminate code style violations in one or more files, in a project or in the entire solution.
Code cleanup profiles
Code cleanup can have various configurations (profiles) to apply in different cases according to your needs. There are two default profiles available out of the box:
Reformat Code that only applies code formatting rules
Full Cleanup that applies all available cleanup tasks except updating file header.
These profiles help in most cases but you cannot configure them. If you want to run code cleanup with the custom set of cleanup tasks, you need to create a custom profile and specify the set of desired tasks there.
To create a new custom cleanup profile
Press Ctrl+Alt+S, or alternatively choose
on Windows and Linux or on macOS.Go to the Code Cleanup settings page:
.Click Add on the top of the page. JetBrains Rider will create a new cleanup profile.
Select the newly created profile in the list of profiles.
Use the right pane to configure the list of tasks in the selected profile.
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. Click Save.
Your newly created cleanup profile will be available in the Code Cleanup dialog (running code cleanup. You can also set this profile to be used for the silent cleanup.
) when you areRunning code cleanup
Ctrl+E, C
JetBrains Rider can apply code cleanup to an arbitrarily selected block of code, all code in the current file, all files in a directory, the current project, or even the current solution. Make sure that you select the right set of cleanup tasks by choosing the right cleanup profile.
To clean up the selection or the whole file
- Select the scope where you want to clean up code:
Make a selection in the editor to clean up code in the selection.
Set the caret anywhere in the file to clean up code in the file.
Select one or more items in the Solution Explorer to clean up code in files under these items and their child items.
Choose Code | Code Cleanup in the main menu.
In the Code Cleanup dialog that opens, choose the scope for cleaning code and a cleanup profile.
Click OK to clean code in the selected scope.
You can clean up not only an individual file, but also multiple files in a directory, project or entire solution. To do so, you should run code cleanup from the Solution Explorer.
To clean up multiple files, projects, or entire solution
Select one or more items (files, directories, projects, or the solution) in the Solution Explorer.
Choose Code | Code Cleanup in the main menu.
Choose a cleanup profile as described above.
Click OK to clean code in the selected scope.
Silent cleanup
Ctrl+E, F
You can also run code cleanup in the silent mode - that is, without displaying the Code Cleanup dialog. Since in this case you will not be able to choose a profile explicitly, you have to assign some cleanup profile to use for the silent code cleanup.
To assign a profile for silent code cleanup
Press Ctrl+Alt+S, or alternatively choose
on Windows and Linux or on macOS.Go to the Code Cleanup settings page:
.Select a cleanup profile on the left and click Set as default for silent cleanup on the top of the options page.
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. Click Save.
To run code cleanup in silent mode
- Do one of the following:
To run silent code cleanup in a specific code file, open it in the text editor or select it in the Solution Explorer.
To apply silent code cleanup to a set of files (directory, project, or solution), select the corresponding item in the Solution Explorer.
Press Ctrl+Shift+A, start typing
Silent Code Cleanup
, and then choose this action in the list.
Available code cleanup tasks
The table below lists available cleanup tasks. If you need the cleanup to perform only a part of these tasks, you can create a custom cleanup profile for that.
Cleanup task | Description |
---|---|
C# | |
Apply file layout | Reorders type members. |
Update file header | Inserts or updates the file header comment. |
Code Styles | |
Use explicit or implicit modifier definitions for types | Applies the rule for type modifiers - whether on not to use the |
Use explicit or implicit modifier definitions for type members | Applies the rule for type member modifiers - whether on not to use the |
Sort modifiers | Applies the preferred order of type/member modifiers. The rule is configurable on the Code Syntax Style: Modifiers. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Apply arguments style (named | positional) | Applies the preferences for named/positional arguments for specific types of parameters. The preferences are configurable on the Code Syntax Style: Named/Positional Arguments. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Remove redundant parentheses | Removes redundant optional parentheses in cases specified on the Code Syntax Style: Optional Parentheses. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Add parentheses to explicitly specify operation precedence | Adds optional parentheses to clarify precedence of operations in cases specified on the Code Syntax Style: Optional Parentheses. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Add/remove braces around single statements | Adds or removes optional braces around single nested statements as specified on the Code Syntax Style: Braces for Single Nested Statements. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Arrange attributes | Applies the rules for multiple attributes as specified on the Code Syntax Style: Multiple Attributes. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Enforce 'var' keyword usage settings | Applies the rules for 'var' usage during the cleanup. The rules are configurable on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Remove code redundancies | On the Redundancies in Code categories for most languages, JetBrains Rider lists over 50 redundancies that have associated code inspections. Some of them require special attention, and you simply cannot process them in the batch mode. That leaves Code Cleanup with about 30 redundancies that it is able to remove automatically whenever you select Remove code redundancies in your cleanup profile. Specifically, code cleanup:
| page of JetBrains Rider settings (Ctrl+Alt+S), in the
Use auto-property, if possible | Replaces simple properties and corresponding backing fields with auto-properties. For more information, see Using Auto-Properties. |
Make field read-only, if possible | Adds the 'readonly' keyword to fields that are assigned in the declaration or in a constructor and only have read usages. For more information, see Making Fields Readonly. |
Make auto-property get-only, if possible | Removes the redundant |
Arrange qualifiers | Applies the rules for qualifying type members. The rules are configurable on the Code Syntax Style: Optional Member Qualifiers. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Fix built-in type references | Applies the rule for built-in type names (C# keyword or CLR type name). The rule is configurable on the Code Syntax Style: Built-In Type References. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Optimize 'using' directives | Provides the following configurable options:
|
Shorten qualified references | Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Reformat code | Reformats your code according to the preferences that you can configure on the Managing and Applying Code Formatting Rules. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Reformat embedded XML doc comments | Reformats XML doc comments. Note that JetBrains Rider does not reformat plain comments like: |
ASP.NET | |
Optimize @Register directives | Removes unused |
HTML | |
Reformat code | Reformats your code according to the preferences that you can configure on the Managing and Applying Code Formatting Rules. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Normalize quotes around attribute value | Applies the preferred quote style: single quote (') or double quote ("). |
JavaScript/TypeScript | |
Terminates statements | Terminates JavaScript statements with the semicolon. |
Normalize quotes around string literal | Applies the preferred quote style: single quote (') or double quote ("). The preference can be configured on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Correct 'var' to 'let'/'const' where possible | let and const are supported in the following cases:
|
Move block-scope variables to most possible inner scopes | Moves variables defined outside of a function/compound statement, etc. closer to their usages if there are no other usages in outer scopes. |
Convert string concatenations to template strings | In JavaScript code, if the target JavaScript language level is ECMAScript 6 (you can choose it on the template strings. For example: page of JetBrains Rider settings (Ctrl+Alt+S) ), as well as in TypeScript 1.4 or above, this option will convert string concatenations tovar str1 = "Hello,";
var str2 = "Test: " + str1 + " World1"; const str1 = "Hello, ";
const str2 = `Test: ${str1} World1`; |
Reformat code | Reformats your code according to the preferences that you can configure on the Managing and Applying Code Formatting Rules. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Reformat embedded XML doc comments | Reformats XML doc comments. Note that JetBrains Rider does not reformat plain comments like: |
TypeScript specific | |
Remove redundant qualifiers | Use this option to remove redundant type name qualifiers. |
Optimize 'import' statements | Use this option to remove redundant 'import' statements or fully-qualified names according to the settings configurable on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Optimize reference comments | Use this option to remove redundant reference comments. |
Synchronize 'public' presence with code style | This options lets you add or remove explicit 'public' modifier according to settings configurable on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Add explicit 'any' type annotation if needed | This options lets you add explicit 'any' annotation to types according to settings configurable on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Synchronize type annotation presence with code style | This options lets you enforce explicit/implicit typing according to settings configurable on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Fix relative path style in 'require' if needed | Applies the Relative file reference style in 'require' code style preference for relative paths in the |
Use 'as' instead of type assertions | Replaces var bar = <string> foo; var bar = foo as string; |
XAML | |
Collapse empty tags | Removes the closing tag if the tag is empty. |
XML | |
Reformat code | Reformats your code according to the preferences that you can configure on the Managing and Applying Code Formatting Rules. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Visual Basic .NET | |
Remove code redundancies | Removes redundant code the same way as for C# Language |
Optimize 'import' directives | Removes unused namespace import directives. |
Shorten qualified references | Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the page of JetBrains Rider settings (Ctrl+Alt+S). |
Reformat code | Reformats your code according to the preferences that you can configure on the Managing and Applying Code Formatting Rules. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |
Reformat embedded XML doc comments | Reformats XML doc comments. Note that JetBrains Rider does not reformat plain comments like: |
CSS | |
Alphabetize properties | Rearranges CSS properties in the alphabetical order. |
Reformat code | Reformats your code according to the preferences that you can configure on the Managing and Applying Code Formatting Rules. page of JetBrains Rider settings (Ctrl+Alt+S). For more information, see |