How to stop Eclipse formatter from placing all enums on one line
JavaEclipseFormatterJava Problem Overview
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
}
Java Solutions
Solution 1 - Java
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:
Window
>Preferences
>Java
>Code Style
>Formatter
- Click
Edit
- Select the
Line Wrapping
tab - Select the
enum
declaration treenode - Set
Line wrapping policy
toWrap all elements, every element on a new line (...)
so it now says 3 of 3 in the parenthesis. - Uncheck
Force split, even if line shorter than maximum line width (...)
so it now says 3 of 3 in the parenthesis. - Select the
Constants
treenode - 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)
}
Solution 2 - Java
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...'
Solution 3 - Java
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//
};
Solution 4 - Java
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
Solution 5 - Java
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,,,,,
Solution 6 - Java
Just adding latest Eclipse 2018.9
Window > Preferences > Java > Code Style > Formatter
-Edit
- Expand
Line Wrapping
tree node. - Expand
Wrapping settings
- Expand
'enum' declaration
- Edit
Constants
andConstant arguments
.
Constants need to be Wrap all elements, every element on a new line
.
Constant arguments need to be Wrap where necessary
.