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

Categories

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

java - How to indent the fluent interface pattern "correctly" with eclipse?

I just created a generator for some fluent interfaces. Now I have lots of code looking like this:

new MyFluentInterface()
    .setFirst( "first" )
    .setSecond( "second" )
    .setThird( "third" )
    .invoke( obj );

I like the indentation shown above, but I can't find a way to configure eclipse to indent this correctly.

eclipse always indents like this:

new MyFluentInterface()
.setFirst( "first" )
.setSecond( "second" )
.setThird( "third" )
.invoke( obj );

How can I configure eclipse so that it indents this fluent interface pattern as shown in my first code example?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

With Eclipse 3.6, this seems doable by configuring your custom Java > Code Style > Formatter profile. Edit it and go to the Line Wrapping tab and select Function Call > Qualified invocations. Then, in the Settings for qualified invocations, configure things like this:

alt text

This will (should) produce the expected result:

SomeEntity e1 = new SomeEntity.Builder()
    .age(10)
    .amount(10.0d)
    .firstname("foo")
    .lastname("bar")
    .build();

But this will obviously affect all the code, which I personally don't like. So I'm using the new Off/On Tags from Eclipse 3.6 (last tab when editing a profile):

alt text

And enclose the parts that don't get formatted as I want and do it myself:

// @formatter:off
SomeEntity e2 = new SomeEntity.Builder()
    .age(10)
    .amount(10.0d)
    .firstname("foo")
    .lastname("bar")
    .build();
// @formatter:on

Pick your poison :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...