Wrapping chained method calls on a separate line in Eclipse for Java

Johann picture Johann · Jun 8, 2011 · Viewed 32.9k times · Source

I haven't been successful in figuring out how to wrap each method call in Eclipse. For example, I have this:

someObject.A().B().C();

But I really want this:

someObject.A()
          .B()
          .C();

Nothing under the "Line Wrapping" section in Eclipse seems to give me this result.

Answer

Christian García picture Christian García · May 4, 2012

Complementing Deepak Azad's answer, what you exactly need is the following:

Windows: Window → Preferences → Java → Code Style
→ Formatter → Edit → Line wrapping (tab)

Mac OS: ADT → Preferences → Java → Code Style
→ Formatter → Edit → Line wrapping (tab)

Then, in the list at the left, select:

Function Calls → Qualified invocations

Now below this list, set Line wrapping policy to:

Wrap all elements, except first element if not necessary

Check:

Force split, even if line shorter than maximum line width

Finally, set Indentation policy to (thanks @Turbo):

Indent on column

It should give you the exact behavior you asked for.


BONUS: Android Studio / IntelliJ Idea:

Mac OS: Android Studio → Preferences (Cmd +,) → Editor → Code Style → Java → Wrapping And Braces tab → Chained method calls

select

Wrap always

and check

Align when multiline