简体   繁体   中英

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?

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:

合格的调用位置

5. Change Profile Name if necessary.

6. Apply and close.

For the general case, add comments:

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

This works for any kind of code, especially large strings:

String str = "some long text" //
    + "some more text" //
    + "still more";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM