Eclipse formatter settings for the Builder pattern
Asked Answered
R

6

51

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?

Resign answered 20/1, 2010 at 21:16 Comment(2)
+1 Good question. I'd like this as well, working a lot with Google Protobufs.Discomposure
Please consider changing the accepted answer to this: https://mcmap.net/q/345897/-eclipse-formatter-settings-for-the-builder-patternPacifa
H
33

Use comments:

   Object o = foo() //
      .bar() //
      .toString();
Halftrack answered 20/1, 2010 at 22:0 Comment(7)
And has the advantage of being perfectly generalizable to any question of the form "how can I get my IDE to break lines in place X under circumstances Y?"Resign
It made my day ! Thanks. That was the only reason I would not activate auto format upon save like the rest of the team.Omentum
We have been to the moon and back, but to tame a code formatter we still use comments in 2020 :(Margarethe
@Margarethe Which formatter do you think should be the standard formatter for Java and how should it be invoked?Paulus
Good questions that I don't know the answers for. But if I had to pick, it would be the one that can handle a method chain without extra scaffolding.Margarethe
@Margarethe As long as we cannot agree on which formatted to use, we need to use things in the language itself. Comments is all we have.Paulus
@alex if you don’t control the formatter this is the best you can do. Things have somewhat changed though since 2010 when the answer was written. You might want to contribute with a less laughable 2021-answer?Paulus
W
44

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

Windstorm answered 19/9, 2013 at 11:9 Comment(1)
Jakub's solution here does the trick. Been working around this issue for years with comments. With this fix, the challenge is reduced to enforcing team-wide import of formatter standards file instead of training each dev to use some syntax of comments. Thanks man.Tortoise
H
33

Use comments:

   Object o = foo() //
      .bar() //
      .toString();
Halftrack answered 20/1, 2010 at 22:0 Comment(7)
And has the advantage of being perfectly generalizable to any question of the form "how can I get my IDE to break lines in place X under circumstances Y?"Resign
It made my day ! Thanks. That was the only reason I would not activate auto format upon save like the rest of the team.Omentum
We have been to the moon and back, but to tame a code formatter we still use comments in 2020 :(Margarethe
@Margarethe Which formatter do you think should be the standard formatter for Java and how should it be invoked?Paulus
Good questions that I don't know the answers for. But if I had to pick, it would be the one that can handle a method chain without extra scaffolding.Margarethe
@Margarethe As long as we cannot agree on which formatted to use, we need to use things in the language itself. Comments is all we have.Paulus
@alex if you don’t control the formatter this is the best you can do. Things have somewhat changed though since 2010 when the answer was written. You might want to contribute with a less laughable 2021-answer?Paulus
H
11

Update for 2021. It is possible to change, navigate to: Code Style -> Formatter -> Line Wrapping -> Wrapping settings -> Function Calls -> Qualified invocations and change value to "Wrap all elements, except first element if not necessary"

Holophytic answered 4/7, 2021 at 15:59 Comment(1)
This should be upvoted and changed to the accepted answer as this works. Thank you so very much for this!Pacifa
H
4

In Eclipse 3.6 It's possible to turn off formatting for a region of code. See my answer to

How to turn off the Eclipse code formatter for certain sections of Java code?

Hardspun answered 28/7, 2010 at 14:20 Comment(0)
S
3

AFAIK, this is known problem with Eclipse Formatter: https://bugs.eclipse.org/bugs/show_bug.cgi?id=59891

Styrene answered 21/1, 2010 at 9:17 Comment(2)
Well, you can try to extend ICodeFormatter and create your own formatter: help.eclipse.org/galileo/topic/org.eclipse.jdt.doc.isv/…Styrene
Hei... it's fixed on Eclipse 3.6M7!Styrene
C
-4

On the menu select Window -> Preferences when the window opens select Java -> Code Style -> Formatter and from there you can create your own format style to use by selecting the new or edit option. When editing a formatting profile a new window opens that gives you a lot of different options to use.

Caroncarotene answered 20/1, 2010 at 21:23 Comment(4)
@ChadNC: I'm describing different settings in the Formatter preferences. The problem is the settings aren't flexible enough AFAICT.Resign
please add a comment with a reason when down voting. Especially when you down vote something that is a valid response. I created a format profile before I posted the answer and it does work just fine.Caroncarotene
@Chris, didn't see your comment when I posted my response to your down vote. You did not mention that fact that you were already using the formatter profiles and that is the reason I suggested using it. BTW you have the same name as my brother.Caroncarotene
Chris is aware of the format options, but he cannot make them work dor him. If the profile you created formats according to Chris' preference, can you edit your answer with the settings in that profile?Oysterman

© 2022 - 2024 — McMap. All rights reserved.