113

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
}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
MetaChrome
  • 3,210
  • 6
  • 31
  • 48

7 Answers7

210

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)
}
Nikita Bosik
  • 851
  • 1
  • 14
  • 20
PolyTekPatrick
  • 3,122
  • 1
  • 25
  • 19
  • 1
    Still looks wrong in the eclipse previewer for me but when I tried it on the actual source it worked like a charm. Thanks. – Ben Thurley Oct 11 '13 at 11:53
  • 2
    Thank you, could you post the corresponding XML markup from a eclipse-codeformatter.xml? – Tob Feb 16 '22 at 10:39
51

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...'
Manbeardo
  • 544
  • 4
  • 7
wjans
  • 10,009
  • 5
  • 32
  • 43
  • I had this problem for months. The thing I didn't try was "force split". Not sure why it should be needed as you'd think "wrap all elements" would actually wrap all elements! Thanks for the solution. – LegendLength Jun 28 '17 at 10:17
11

It's slightly ugly too, but if your company policy prevents you from changing the formatter, you can just put comments at the end of lines you don't want to be wrapped.

public static enum Command 
{
    login,//
    register,//
    logout,//
    newMessage//
};
Raku
  • 591
  • 2
  • 13
6

It's not nice but you can turn the Eclipse formatter off for some sections of code...

// @formatter:off
public static enum Command {
    login,
    register,
    logout,
    newMessage
};
// @formatter:on

the option is in the Windows->Preferences->Java->Code Style->Formatter->Edit->Off/On Tags panel

pillingworth
  • 3,238
  • 2
  • 24
  • 49
  • 1
    This works...kind of. The compiler now thinks its an error. Putting them in comments overcomes this problem like // @formatter:off – Bob Kuhar Jan 11 '12 at 21:46
4

You need to set the line wrapping policy under enum declaration for "Constants."

Set the wrapping policy to

  • Wrap all elements, every element on a new line

AND

  • Check the box that says "Force Split, even if line shorter than,,,,,
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
2

Just adding latest Eclipse 2018.9

  1. Window > Preferences > Java > Code Style > Formatter - Edit
  2. Expand Line Wrapping tree node.
  3. Expand Wrapping settings
  4. Expand 'enum' declaration
  5. Edit Constants and Constant arguments.

Constants need to be Wrap all elements, every element on a new line. Constant arguments need to be Wrap where necessary.

Ondrej Burkert
  • 6,617
  • 2
  • 32
  • 27
0

in my case set org.eclipse.jdt.core.formatter.alignment_for_enum_constants to 17 works with maven formatter plugin: https://code.revelc.net/formatter-maven-plugin/

    <setting
      id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants"
      value="17"
    />

https://gitlab.inria.fr/stopcovid19/submission-code-server/-/blob/develop/.etc/eclipse-formatter-java.xml

ninjahoahong
  • 2,624
  • 22
  • 20