Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Eclipse code formatter to support fluent interfaces [duplicate]

I've recently started working with an API that uses a "fluent interface". I'm struggling to find how to configure the Eclipse code formatter to support this properly.

What I want to get is this:

Foo myFoo = new Foo()
                  .setThis()
                  .setThat()
                  .setTheOther()
                  .setOneMoreThing();

but I can't hit on the right settings: I end up with something like this:

Foo myFoo = new Foo().setThis().setThat()
                  .setTheOther().setOneMoreThing();

which is nowhere near as readable.

Has anyone already solved this?

like image 988
Dave Mulligan Avatar asked Aug 19 '13 22:08

Dave Mulligan


People also ask

What is code formatting in Java?

The most important things are to be consistent (an editor that indents code for you is helpful) and to conform to the existing style when editting someone else's code. Tapestry is formatted using spaces (not tabs), and an indent of four. All the code currently in the repository has been formatted using the Eclipse IDE.


1 Answers

Sorry: turns out this is a near-duplicate of this: How to indent the fluent interface pattern "correctly" with eclipse?

Here's the answer that worked for me:

The place to set this is on the "Line Wrapping" tab of the code formatting preferences page, in the "Qualified invocations" section of the "Function Calls" section. The settings for what I wanted were:

Line wrapping policy: Wrap all element, except first element if not necessary

Indentation policy: Indent on column

enter image description here

like image 100
Dave Mulligan Avatar answered Sep 20 '22 17:09

Dave Mulligan