Code Cleanup
- 3 minutes to read
Code Cleanup allows you to specify which Refactorings and Code Formatters CodeRush should apply to a document during cleanup. This feature helps you refactor a large amount of code.
Configure Cleanup
Open the Editor | C# (Visual Basic) | Code Cleanup options page.
Choose rules CodeRush should apply when you call code cleanup and enable “Apply In Action”.
Choose rules CodeRush should apply in code cleanup before a file is saved and enable “Apply on Save”.
Note
CodeRush does not clean up a file when you close the unsaved file or solution.
Enable the Apply Code Cleanup when saving a document option to allow CodeRush to clean up a file before it is saved.
Click OK to save and apply the settings.
Exclude Generated Code from Code Cleanup
You can specify files and regions that contain generated code on the Editor | All Languages | Generated Code page.
CodeRush skips the specified items in code cleanup and code analysis.
Note
The Generated Code page already includes commonly used file masks and regions for generated code (for example, the “*.Designer.cs” file).
Clean a File Up
Follow the steps below to run code cleanup in an open file:
- Place the caret anywhere in your code. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
- Choose Code Cleanup from the menu and press Enter.
The screencast below shows the code cleanup with applied Organize members and Format document rules.
You can also use the following methods to execute code cleanup:
Visual Studio menu
Open the CodeRush | Edit menu and choose Cleanup Document.
Visual Studio toolbar
Click the Run Code Cleanup button on the CodeRush Visualize toolbar.
Clean a File Up on Save
Click the Save button or press Ctrl + S in the editor.
The screencast below shows the code cleanup with applied Organize members and Format document rules.
Clean a Project Up
Follow the steps below to run code cleanup in a project:
- Right-click the project you want to clean in the Solution Explorer.
Select CodeRush Cleanup in the context menu.
CodeRush shows a progress window when it calculates changes that Code Cleanup should apply in code.
If you cancel the process, the code remains unchanged.
Note
Use Visual Studio’s Undo action (Edit | Undo menu item) to undo the cleanup after its completion. This action is available when a document is open.
CodeRush applies the changes to documents when the calculation is finished.
Clean a Folder Up
You can run Code Cleanup for a selected folder (folders) in the Solution Explorer.
Right-click the folder (folders) in which you want to clean files and select CodeRush Cleanup.
You can cancel code cleanup while it is still in progress, all code remains unchanged.