Stop eclipse formatter putting all enums in one line

While working with eclipse you would have experienced that if one tries to put enums on different lines and by chance someone formats the code using eclipse formatter( ctrl+shift+f), all the manual formatting is removed. Of course you can undo it. But what if someone else working on your code removes your manual formatting and adds more code on top of that. Then there is no going back but to do it again manually. So in this tutorial I will show a simple trick to make eclipse formatter remember the formatting you did manually. Let us now see with a simple example,

  •  I created  an enum and manually formatted it as shown in picture below
manually_formatted
Manually formatted enum
  • Next I pressed ctrl+shift+f, to format the code using eclipse. You can see from the below picture that all my formatting was removed.
after_eclipse_formatting
After formatting by eclipse

So as to stop this behavior, you can a add a comment at the end of the each enum as shown in below image

formatting_tip
Formatting issue solution

After you do the above trick, eclipse would no longer  put  the enums in the same line.

There are other ways of doing it like using //@formatter:off   or going to eclipse preferences and adjusting the formater settings,

Window > Preferences > Java > Code Style > Formatter

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.