Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse formatter settings for the Builder pattern

I'm extremely frustrated with the Eclipse formatting rules for a series of qualified invocations (i.e., the Builder pattern style). For example, here is my preferred formatting for some code that creates a new Apache Commons CLI Options object:

  Options options = new Options()       .addOption(OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")       .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,           "print version and exit")       .addOption(OptionBuilder.withLongOpt(OPTION_PROPERTIES)                      .hasArg()                      .withArgName("FILE")                      .withType(File.class)                      .withDescription("specify a user properties file")                      .create()); 

I.e., parameters are wrapped and indented if necessary and all qualified invocations except the first, unless necessary, are wrapped and indented if there is more than one. If a parameter list wraps inside a qualified invocation, the invocation should wrap first.

The default formatting in Eclipse ("Wrap only when necessary" for arguments and invocations) yields the following mess:

  Options options = new Options().addOption(       OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")       .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,           "print version and exit").addOption(           OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(               "FILE").withType(File.class).withDescription(               "specify a user properties file").create()); 

Going into "Java Code Style -> Formatter -> Line Wrapping" and the line wrapping setting to "Wrap all elements, except first element if not necessary" for invocations yields:

  Options options = new Options().addOption(       OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")       .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,           "print version and exit")       .addOption(           OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(               "FILE").withType(File.class).withDescription(               "specify a user properties file").create()); 

I don't like that the OptionBuilder expression isn't being wrapped, or that "FILE" gets wrapped without also wrapping withArgName.

Changing the indentation to "Indent on column" yields:

  Options options = new Options().addOption(OPTION_HELP_SHORT, OPTION_HELP,                                      false, "print usage information")                                  .addOption(OPTION_VERSION_SHORT,                                      OPTION_VERSION, false,                                      "print version and exit")                                  .addOption(                                      OptionBuilder.withLongOpt(                                                       OPTION_PROPERTIES)                                                   .hasArg()                                                   .withArgName("FILE")                                                   .withType(File.class)                                                   .withDescription(                                                       "specify a user properties file")                                                   .create()); 

The is breaking the lines where I'd prefer, but pushing things over much too far to the right.

Is there any way to convince Eclipse to apply my preferred formatting style or something closer to it than any of the above?

like image 808
Chris Conway Avatar asked Jan 20 '10 21:01

Chris Conway


People also ask

How do I Auto Arrange in eclipse?

CTRL + SHIFT + F will auto format your code (whether it is highlighted or non highlighted).


1 Answers

Turning off formatting with comments, or inserting line comments is too tedious.

The best way is described here:

... or you can select "Line Wrapping > Never join already wrapped lines" globally. Then, you can break it manually and the formatter will only format inside lines (or add additional line breaks if necessary).

With this setting Eclipse formatter will stop ruining your builder statements.

enter image description here

like image 67
Jakub Bochenski Avatar answered Oct 05 '22 22:10

Jakub Bochenski