Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...