For this to work correctly across multiple computers and developers, each repo must have an .editorconfig
file that determines what the code looks like when formatted. For an example, see the repo linked below.
https://github.com/neogeek/csharp_editorconfig
Rider has reformatting built into the editor's context menu without any additional setup. Select a block of code, or the entire file, and press ALT
+ Enter
(or Option
+ Enter
for macOS developers). This will open the context menu. The first three options will pertain to formatting code: "Format selection", "Apply syntax style", and "Cleanup selection".
The key command we will be focusing on for this tutorial is "Reformat Code" found in the "Code" menu. This runs all formatting options on your code. The key command for this is Ctrl
+ Alt
+ L
(or Option
+ Command
+ L
for macOS developers).
For ease of use, we are going to bind the "Reformat Code" to a macro that will also save the file.
- First, open the "Edit" menu, then "Macros" (at the bottom of the list), then "Start Macro Recording".
- Then without doing anything else, press the key commands for "Reformat Code" which is
Ctrl
+Alt
+L
(orOption
+Command
+L
for macOS developers), and then "Save All" which isCtrl
+S
(orCommand
+S
for macOS developers). - Then navigate back to the "Edit" then "Macros" menu and press "Stop Macro Recording".
- Give the macro a name like "Reformat Code + Save All".
- Then open the application preferences and then navigate to the "Keymap" section.
- Search for "Reformat Code + Save All".
- Right-click on the macro and hit "Add Keyboard Shortcut".
- Press
Ctrl
+S
(orCommand
+S
for macOS developers). - When the dialog appears asking you if you want to remove all other commands with that keyboard shortcut, say yes.
- Hit "Save".
Now, the code will reformat to look the same for everyone whenever you save. This helps prevent unnecessary whitespace changes from developer to developer, keeping PRs smaller and focused on the actual changes.