14

I would like Eclipse to format some code which calls a builder as follows:

final Item item = new Item.Builder()
                          .name("something")
                          .field("a value")
                          .build();

i.e. aligning on the '.' character from the first line. I can manually convince Eclipse to do this by aligning the second line (the .name("something")) by hand, but any auto-formatting destroys this.

I've played around with all of the various formatting options that I can find in Eclipse and none of them seem to do what I want. Any ideas on if there are format options to lay out the code in this way?

  • personally I think this is a bad way to format code. If you rename item to carPart then your formatting will be off or you'll have to touch all following lines. If you are in a team of ppl and you use SCM than this will be an ugly check in, because you touched lines irrelevant to your actual change, the rename. Not a problem in your pet project. A big problem when trying to pinpoint the changes after a production issue when the boss is breathing down your neck. – hidralisk Jan 29 '13 at 18:08
  • Well any change of length of a line could cause knock-on effects to subsequent lines if you don't allow lines to have unlimited length so I don't see how this is a special case. And if you're refactoring in the way that you say then you should do it as a separate commit so that it's separate from any other code changes. –  Jan 29 '13 at 18:23

3 Answers3

5

Aligning exactly at the dot position is not possible, but the linebreaks for each method call can be done automatically. I've created this output

void format() {
    Test test = new Test()
            .a()        // this call can also be configured to be one line above
            .b()
            .c();
}

by selecting the Line Wrapping tab in the formatter settings, selecting Function Calls, Qualified invocations, switching the Line wrapping policy in the combo box to Wrap all elements, every element on a new line and finally checking the checkbox Force split, even if line shorter than maximum line width.

You can get the first method call into the first line by selecting the "...except first element" policy instead.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
  • can you try it with this: private static String getErrorMessage(final MyFile file) { return new StringBuilder(ERROR_MESSAGE).append(" ").append( file.getDataType()).append(" ").append(FILE_NAME).append( file.getFileNameNoPathNoSuffix()).toString(); } and I think you will see that the result is really ugly - at least with Eclipse Luna. What version are you using? – Adam Feb 12 '15 at 17:09
0

Create a new formater:

Properties -> Formater -> Configure Workspace Settings -> New

then on the tab Off/On Tags set Enable Off/On tags

And then just surround your code like this:

/* @formatter:off */
final Item item = new Item.Builder()
                          .name("something")
                          .field("a value")
                          .build();
/* @formatter:on */
isvforall
  • 8,768
  • 6
  • 35
  • 50
  • Thanks for the response. Unfortunately I have a large amount of code which is badly formatted and I want Eclipse to format it as per the standards in the question rather than have to carry it out manually. –  Jan 29 '13 at 15:10
0

I think, you can achieve this by using 'jalopy'. There is a plugin for Maven, if you use Maven.

In case, you would like to use jalopy then, the setting that you would be interested is <methodCallChain> in the alignment element whose value you should set to true.

Edited: maven jalopy plugin. Make sure you target the clean phase and use the goal format

Horizon
  • 548
  • 3
  • 7