13

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:

(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.

)

Community
  • 1
  • 1
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • Since there does not seem to be a plugin for this I wrote a feature request: https://bugs.eclipse.org/bugs/show_bug.cgi?id=466920 – Stefan May 09 '15 at 18:42
  • TBH, I do all fine-grained code formatting manually. When using tools, it's a tradeoff between getting unexpected results versus spending time configuring the tool. – Nayuki May 09 '15 at 19:49
  • I agree if you work alone. I am in a stepwise process of introducing more formatting and style restrictions in Eclipse for our team to make it easier to share and manage our code base. It also helps to clean up historical code. We prepared a portable Eclipse package and put it under version control. We started with an agreement over common Eclipse warning settings. Then we introduced findbugs and now I am preparing checkstyle settings which will produce the next 2000 warnings. :) I agree that it is a lot of work to configure those plugins and hope that it will pay off in the long run. – Stefan May 11 '15 at 06:05

3 Answers3

3

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"

Sankalp
  • 2,030
  • 7
  • 30
  • 41
Michael
  • 34,873
  • 17
  • 75
  • 109
1

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."
Stefan
  • 10,010
  • 7
  • 61
  • 117
0

This issue has been fixed for version 4.11 M1. Also see

https://bugs.eclipse.org/bugs/show_bug.cgi?id=543079

Stefan
  • 10,010
  • 7
  • 61
  • 117