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:
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.
)
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"
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With