Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use different line wrapping for strings and other items in Eclipse for Java

I would like to use line wrapping after operator for Strings and line wrapping before operator for other items (numbers, custom objects,..) in Eclipse for Java.

When adding numbers with different signs the operators are more important to me than the numbers. It would be nice to have the operators at the front to better read the statement:

A.

int foo =  baaa
           + veryveryveryloooooongexpression
           - shortexpression
           + anotherexpression;

vs.

B.

int foo =  baaa +
           veryveryveryloooooongexpression -
           shortexpression +
           anotherexpression;

On the other hand, when adding Strings, the operator is just used to continue the line and the String items are more important to me. The operator at the end of a line gives a hint that something goes on in the next line. Therefore I would like to use line wrapping after operator for Strings:

B.

String message = "Would you like to use line wrapping at " + position +
                 " ? If you want to keep that behavior press Enter."

vs.

A.

 String message = "Would you like to use line wrapping at " + position
                  +" ? If you want to keep that behavior press Enter."

Related articles:

  • Change how eclipse formatter wraps long strings

  • https://softwareengineering.stackexchange.com/questions/93670/line-break-before-after-operator

  • StringBuilder vs String concatenation in toString() in Java

(In some cases it might be of course better to improve the code and use a single line, to use String.format(...) or use a String Builder. That is not the question here.)

How can I apply different line wrapping settings A.(before operator) and B.(after operator) for the two different cases (first item is a String vs. first item is something else) in Eclipse? Are there some default settings that I did not see? Is there an Eclipse plugin that can do so?

(A few more comments:

  • Edit: Following comment is only valid for Eclipse 4.4.2 (Luna) and already fixed in Eclipse 4.5 (Mars):

I did not get the wrapping after operator (B.) to work correctly when wrapping a String argument inside a function call, even if I would want to apply it for both cases. I enabled the option "Wrap before operator" for Binary expressions and disabled the general option "Never join already wrapped lines". Nevertheless the + operator in following example appears in the next line. I filed a bug report under https://bugs.eclipse.org/bugs/show_bug.cgi?id=466919.

    statusBuilder.append("This set is not yet present in the database!\n"
                    + "You can save it by hitting the 'Save' button below.\n");

  • If I hit Return in the middle of a String, eclipse correctly wraps the line before or after the operator, depending on the setting "Wrap before operator" for Binary expressions.

  • The checkstyle module Whitespace=>Operator Wrap neither supports extra settings for String concatenation.

)

like image 909
Stefan Avatar asked May 07 '15 06:05

Stefan


2 Answers

Use the on/off tags to disable Eclipse's code formatter for specific blocks of code. This forces you to format the code yourself, but it at least gives you total control over how the code looks.

//@formatter:off
String message = "Would you like to use line wrapping at " + position +
                 " ? If you want to keep that behavior press Enter."
//@formatter:on

The on/off features have to be turned "ON". In Eclipse preferences: Java > Code Style > Formatter. Click on "Edit" button, "Off/On Tags", check off "Enable Off/On tags"

like image 148
Michael Avatar answered Oct 14 '22 06:10

Michael


I just found another option: use "+ //" at the end of each line:

String message = "Would you like to use line wrapping at " + position + //
             " ? If you want to keep that behavior press Enter."
like image 29
Stefan Avatar answered Oct 14 '22 08:10

Stefan