Search code examples
javaeclipseformatter

Prevent linebreaks with eclipse code formatter


I use the Eclipse formatter with the option on save enabled.

Right now code like this:

 int sum = widgets.stream()
                  .filter(b -> b.getColor() == RED)
                  .mapToInt(b -> b.getWeight())
                  .sum();

will be formatted to:

 int sum = widgets.stream().filter(b -> b.getColor() == RED).mapToInt(b -> b.getWeight())
                  .sum();

is there a setting which will do automated linebreak for everything but cascading function calls?


Solution

  • Steps to format cascading function calls:

    1. Window => Preferences => Java => Code Style => Formatter

    2. Click Edit.

    3. Expand Line Wrapping => Wrapping Settings => Function Calls

    4. Change Qualified invocations to Wrap all elements, except first element if not necessary.

    • Additionally, ensure Force split, even if line shorter than maximum line width is toggled on.

    See image below for what it looks like when set correctly:

    Qualified Invocation Location

    5. Change Profile Name if necessary.

    6. Apply and close.