score:206
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)
}
score:2
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
.
score: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,,,,,
score:5
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
score: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//
};
score: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...'
Source: stackoverflow.com
Related Query
- How to stop Eclipse formatter from placing all enums on one line
- How do I stop JPA Facet on Eclipse from updating all the time?
- How to stop Eclipse formatter from adding trailing whitespaces in Javadoc
- How do I stop Eclipse from moving my line comments after annotations?
- How to copy Eclipse app with it's all projects and space from one Mac into another?
- How to prevent Eclipse formatter from inserting spaces at the beginning of line comment
- How to stop Eclipse from inserting blank line between import groups in Java code
- How to obtain line and/or column from parsed java source from Eclipse JDT Parser from all nodes?
- How to stop Eclipse / Scala IDE from uploading all my projects at once?
- How to specify the option contact all update sites while installing plugins from command line for eclipse
- How do I stop eclipse from auto removing all spacing between my methods?
- How to stop Eclipse from accumulating Tomcat instances each time one is restarted?
- How to format all Java files in an Eclipse project at one time?
- How do you stop Console from popping up automatically in Eclipse
- How to stop Eclipse from trying to run XML files?
- How to generate an Eclipse formatter configuration from a checkstyle configuration?
- How to stop Eclipse from auto-adding parentheses?
- Stop eclipse from line wrapping?
- How to prevent Eclipse formatter from messing up text-bulleted comments?
- How do I setup Eclipse to stop on the line an exception occurred?
- How can I stop Eclipse from asking to install Subversive Connectors
- How do I stop Eclipse from refreshing some dir?
- How do I stop Eclipse (EPIC Perl) from adding DOS CR/LF to my Perl scripts?
- How to stop Eclipse from moving cursor for closing java parenthesis?
- How to configure Eclipse formatter to not insert blank line between field declarations?
- How to stop Eclipse from splitting annotations into new lines
- how to stop Eclipse browser from displaying web page upon restarting tomcat?
- How to launch eclipse from a command line on MacOS with a workspace pathname
- How to read all JARs from Eclipse classpathentry where kind="con"
- How to stop Eclipse CDT from emitting errors due to gcc specific syntax?
More Query from same tag
- How to use scrollbar for a JTextField in JAVA using eclipse GUI drag and drop tool?
- Undefined symbol errors when linking of C++ project using Eclipse
- Not able to get value of clicked spinner item, instead I get the cursor value
- In my newly installed eclipse, 3 folders are missing. Java Resource, JAX-WAS Web Service and Deployment Descriptor
- spring framework: How can i start a web project with eclipse j2ee indigo?
- Inconsistent behavior of Java Maven Modules in Eclipse
- Make eclipse Syntax Coloring exact as visual studio Syntax Coloring
- java.lang.IllegalStateException: can not found call #0 on stack. Invalid call of record method?
- SVN Showing question Mark(?) With each file in eclipse
- m2e Maven Repositories View - cannot see snapshots
- not able to run wildfly 10.1.0 server in windows 10
- Eclipse Rhostudio not showing error messages
- How to programmatically new a java class which implements sepecified interface in eclipse plugin development
- Can't add support library v7 AppCompat
- Luna JBoss Tools JBoss 7.1 AS - server is always in "starting" state
- The import org.eclipse cannot be resolved
- How to run a class anytime a editor page receive focus on Eclipse?
- Restart the application when click on notification
- Build Errors in Eclipse (Java Project) due to multiple references of the same Java class
- Java or Eclipse 'fails' on Whitespaces(?)
- Eclipse IDE running error
- Eclipse version which has BIRT framework by default
- Problem connecting to Hsqldb via Hibernate when running a Eclipse GWT project
- Using a bean class from a servlet in a Java EE application
- Non stopping "Failed to get the memory usage" messages when I run Eclipse IDE from Debian in WSL
- Android NDK in Eclipse :: (Cannot run program "ndk-build": Unknown reason)
- How on earth does he debug a running application like this, and more importantly, how can I?
- Java Multi-Thread racing simulation issue
- eclipse + m2e + junit + infinitest + eclemma?
- tomcat-maven-plugin: Server returned HTTP response code: 403