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:
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.
)
This issue has been fixed for version 4.11 M1. Also see