Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Eclipse formatter from placing all enums on one line

I have enums like:

public static enum Command { login, register, logout, newMessage } 

When formatting the file, the output becomes:

public static enum Command  { login, register, logout, newMessage } 
like image 468
MetaChrome Avatar asked Jul 13 '11 08:07

MetaChrome


People also ask

How do I turn off auto format in Eclipse?

For this got to Window->Preferences> Java -> Editor->Save Actions-> click on "Configure project specific Settings ". On the bottom of the window select the check box "Show only project with project specific settings " . Then select a module/project -> Ok-> Un check "Enable project specific settings"--> Apply.


2 Answers

The answer by @wjans worked fine for normal enums, but not for enums with arguments. To expand on his answer a bit, here's the settings that provided the most sensible formatting for me in Eclipse Juno:

  1. Window > Preferences > Java > Code Style > Formatter
  2. Click Edit
  3. Select the Line Wrapping tab
  4. Select the enum declaration treenode
  5. Set Line wrapping policy to Wrap all elements, every element on a new line (...) so it now says 3 of 3 in the parenthesis.
  6. Uncheck Force split, even if line shorter than maximum line width (...) so it now says 3 of 3 in the parenthesis.
  7. Select the Constants treenode
  8. Check Force split, even if line shorter than maximum line width

This sets the 3 subnodes for the enum treenode to the same wrapping policy, and the same force split policy except for the Constants treenode, so your enums with arguments will be formatted each on their own line. The arguments will only wrap if they exceed maximum line width.

Examples:

@wjans

enum Example {     CANCELLED,     RUNNING,     WAITING,     FINISHED }  enum Example {     GREEN(         0,         255,         0),     RED(         255,         0,         0) } 

Solution described above:

enum Example {     CANCELLED,     RUNNING,     WAITING,     FINISHED }  enum Example {     GREEN(0, 255, 0),     RED(255, 0, 0) } 
like image 99
PolyTekPatrick Avatar answered Sep 28 '22 02:09

PolyTekPatrick


You can specify this in your formatter preferences:

  • Preferences: Java -- Code Style -- Formatter
  • Click Edit
  • Select the 'Line Wrapping' tab
  • Select 'enum' declaration -> Constants in the box on the left
  • Set Line wrapping policy to 'Wrap all elements, every element on a new line'
  • Check 'Force split...'
like image 24
wjans Avatar answered Sep 28 '22 03:09

wjans