2
Eclipse Java EE IDE for Web Developers.

Version: Mars.2 Release (4.5.2)
Build id: 20160218-0600

Eclipse formatting code does not work for source code containing the following case:

String foo = (bar!=null)? value1 : 
        value2;
System.out.println("hello world");

the value1 and value2 can be very long, so split into two lines.

Java editor: format the code above (CTRL + I), and get the following result:

String foo = (bar!=null)? value1 : 
        value2;
        System.out.println("hello world");

Source code can be very big, and it is convenient to select all and then press (CTRL+I) to format the whole code. Is there a way to avoid the formatting error above? Thanks.

1
  • 1
    Hm, make value1 and value2 small by making them methods perhaps. Commented Jan 1, 2017 at 4:08

1 Answer 1

1

You can use single line comments // to force the eclipse code formatter (and other code formatters) to preserve the line(s) you want. Something like

String foo = (bar != null) ? //
        value1 : //
        value2;
System.out.println("hello world");
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I've done a similar thing today with an enum that I've created, to try to prevent bad formatting.
@Dave I formatted the code in this post with eclipse Mars.2
What is your specific minor version and build id? Thanks
Eclipse IDE for Java Developers - Version: Mars.2 Release (4.5.2) Build id: 20160218-0600

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.