124

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Johann
  • 1,977
  • 3
  • 19
  • 18
  • Several years down the line I still don't think Eclipse does this well. The answers all have unwanted side effects or fail to do the job. I don't have an answer either (so I won't post one), but my workaround is to end each line with comments: someObject.A() // (new line) .B() // (new line) .C(); which works. – ewramner Jul 15 '21 at 17:36

7 Answers7

210

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

Community
  • 1
  • 1
Christian García
  • 3,939
  • 1
  • 26
  • 26
  • 2
    Thanks. This doesn't work for `return new SomeClass().A().B().C();` with each method call on a separate line. – Harvey Oct 31 '14 at 19:17
  • 3
    @ChristianGarcía +1 for the great answer. One observation from me (Eclipse neon): When I set `Indent on column`, it sometimes add extra space; for example `session.createQuery` becomes `session .createQuery` and so on. Reverting to the default option sorted the problem for me though. – Sayan Pal Nov 29 '16 at 07:16
  • This inserts a tab after the first invocation. To avoid it, I had to check "Use spaces to indent wrapped lines" under Indentation. – Daniel Nitzan Jun 29 '20 at 13:22
  • As @DanielNitzan mentioned, this will insert tabs. I solved this by not setting the indentation policy to "Indent by one" rather than "Indent on column". This makes it so that function calls one new lines will be indented one tab deeper than the start of the statement and not hang in the middle of the page – Markus Rohlof Nov 22 '20 at 09:26
  • I don't see *indentation policy* :(* Eclipse 2018-12 (4.10.0) – mlt Feb 03 '21 at 12:32
  • Thanks for the solution. Is there a way to not wrap inside a statement? like in a `if (x.a().b() && y.c().d())` or a `while (x.a().b() && y.c().d())` – Alex Chen Feb 19 '22 at 00:54
  • Is there a way to only wrap after two chained calls (wrap `test.toString().strip().strip()` but not `test.toString().strip()`) ? – gruvw Feb 22 '22 at 14:23
16

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

double-beep
  • 5,031
  • 17
  • 33
  • 41
Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
  • 4
    I am sorry i did not explain: It won't format it. It will simply not mess it up, if you do it manually. – Op De Cirkel Jun 08 '11 at 08:08
  • I now understand. Eclipse can't actually format chained methods but it will retain the formatting as I have typed it. Thanks. – Johann Jun 08 '11 at 08:13
  • nothing specific to chained calls. If you force function call wrapping, it will wrap everywhere – Op De Cirkel Jun 08 '11 at 08:15
  • @Johann That is not correct, Eclipse can format chained method calls. – Deepak Azad Jun 08 '11 at 08:40
  • 3
    @OpDeCirkel You should add your comment in the answer itself. Make it easier for people to notice it : This comment `I am sorry i did not explain: It won't format it. It will simply not mess it up, if you do it manually. –` –  May 28 '20 at 18:02
8

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
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • `82` to be aligned exactly like he wants. [Eclipse Source](https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java#n38) – CoYoT3 Jul 04 '23 at 18:40
8

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'.

Deepak Azad
  • 7,903
  • 2
  • 34
  • 49
  • This will put on new line every function call, even if they are not chained, and if you format your file with Ctrl+Shift+f, it will mess up everything\ – Op De Cirkel Jun 08 '11 at 08:45
  • 2
    Other policies are also available, e.g. "Wrap all elements, except first element if not necessary". One of them should work for you :) – Deepak Azad Jun 08 '11 at 08:52
3

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: How to turn off the Eclipse code formatter for certain sections of Java code?

Community
  • 1
  • 1
Stim
  • 1,455
  • 14
  • 28
0

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

select Indentation policy as indent on column

-1

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

Edit

Jajikanth pydimarla
  • 1,512
  • 13
  • 11