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

EclipseLine

Eclipse Problem Overview


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.

Eclipse Solutions


Solution 1 - Eclipse

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

Solution 2 - Eclipse

Window → Preferences → Java → Code Style → Formater → Edit → Line wrapping → Never join already wrapped lines

Solution 3 - Eclipse

Window > Preferences > Java > Code Style > Formater > Edit > Line wrapping > Function Calls, set the 'Line wrapping policy' as 'Wrap all elements, every element on a new line'.

Solution 4 - Eclipse

For those who have eclipse code formatting settings checked into source control (for consistent code formatting across the whole team), the .settings folder is checked in.

The property that will give you this formatting behavior is in the .settings/org.eclipse.jdt.core.prefs file.

The property is:

org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=80

Solution 5 - Eclipse

If you are willing to do the formatting of those segments yourself, you can prevent eclipse from reformatting those segments by using:

// @formatter:off
...
// @formatter:on

You might need to enable this in your preferences: http://archive.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/eclipse-news-part2.html#JavaFormatter

See also: https://stackoverflow.com/q/1820908/364401

Solution 6 - Eclipse

in: Window->Preferences->Java->Code Style->Formater->Edit->Line wrapping->Function call-> Qualified invocation

select Indentation policy as indent on column

Solution 7 - Eclipse

Customize your eclipse formatter in both java and javascript. increase the max line with that fits your screen. Windows>preferences

Edit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohannView Question on Stackoverflow
Solution 1 - EclipseChristian GarcíaView Answer on Stackoverflow
Solution 2 - EclipseOp De CirkelView Answer on Stackoverflow
Solution 3 - EclipseDeepak AzadView Answer on Stackoverflow
Solution 4 - EclipseAndy GuibertView Answer on Stackoverflow
Solution 5 - EclipseStimView Answer on Stackoverflow
Solution 6 - EclipseShiv Krishna JaiswalView Answer on Stackoverflow
Solution 7 - EclipseJajikanth pydimarlaView Answer on Stackoverflow