0

How to configure Eclipse to format all java code except Strings declaration?

Example:

Before Ctrl+I:

    String sql = 
        "Select column " + 
        "from   table  " +
        " where column1 > 0 AND column2 < 0";

After Ctrl+I:

    String sql = "Select column " + "from   table  "
            + " where column1 > 0 AND column2 < 0";

Thank you!

edit:

Has any way to configure using Formatter preferences (Window > Preferences > Java > Code Style > Formatter)?

2 Answers 2

1

You can enable the @formatter:on and @formatter:off tags in the formatter preferences:

// @formatter:off
String s = "..." + 
           "..." ...
// @formatter:on
Sign up to request clarification or add additional context in comments.

Comments

1

You could use // to prevent the formatting like this:

String sql = //
        "Select column " + //
        "from   table  " + //
        " where column1 > 0 AND column2 < 0";

That's not very nice but works in every case (i.e. when your coworkers don't use exactly the same settings in Eclipse).

1 Comment

That's what I do for enums as well. +1 for the point about coworkers.

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.