After creating a custom code formatter in Eclipse, as discussed in my previous post How to create and share Eclipse code style formatter, it is now time to put that code formatter in action. Code formatting should be an automatic process. Following are the steps which describe how to enable auto formatting in Eclipse.
Step 1
Open Eclipse and go to Window -> Preferences
Step 2
In Preferences window, go to Java -> Editor -> Save Actions
Step 3
In the right pane of Save Actions, check the following options:
- Perform the selected actions on save
- Format the source code – Within this, select the radio button labelled, Format edited lines. This will prevent formatting the entire code when only a certain section of code is saved after being edited.
Click on Apply and then OK to save the changes. Now the next time you hit save in Eclipse, your code will be automatically formatted. Pretty cool, eh?
How to enable auto formatting in Eclipse