How to indent the fluent interface pattern “correctly” with eclipse?

前端 未结 1 1517
野趣味
野趣味 2020-12-07 15:36

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

new MyFluentInterface()
    .setFirst( \"first\" )
    .set         


        
相关标签:
1条回答
  • 2020-12-07 16:17

    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 :)

    0 讨论(0)
提交回复
热议问题