A: 

This hack works:

String x = "s" + //Formatter Hack
    "a" + //
    "c" + //
    "d";

I would suggest not to use the formatter. Bad code should look bad not artificially good. Good code takes time. You cannot cheat on quality. Formatting is part of source code quality.

Thomas Jung
Not using the formatter is just a bad idea; the formatter helps to catch errors and keeps the code in a consistent state.
Francis Upton
An interesting suggestion, but I don't see how formatting tells us whether code is good or not.
Chris
I think what he's saying is that he feels that poorly written, poorly formatted code should be kept as-is rather than formatting it in the hope of "improving it." Poorly written, poorly formatted code should "stick out" somehow so that it can be easily identified. Not quite sure that I totally agree, but I think that's the idea.
Greg Mattes
@Francis - Bugs: How can auto-formatted code finding bugs? Consistency: Consistency is a nice argument but overall code quality is more important. You can define a nice consistent process for hamburger flipping but it will never work for haute cuisine. Cooking a can be a reasonably complicated activity or trivial if you ignore enough facts. If you think that software development is like hamburger flipping tools enforcing consistency are for you. This is not an argument against formatting guide lines but if the developers don't care about these guidelines the won't care about other essentials.
Thomas Jung
+7  A: 

AFAIK from Eclipse 3.5 M4 on the formatter has an option "Never Join Lines" which preserves user lines breaks. Maybe that does what you want.

Else there is this ugly hack

String query = //
    "SELECT FOO, BAR, BAZ" + //
    "  FROM ABC"           + //
    " WHERE BAR > 4";
jitter
So in addition to setting the "Never Join Lines" option I *also* have to write these "phantom" comments? Shouldn't the "Never Join Lines" part work by itself?
Greg Mattes
Yes, of course it should. The phantom comments are an alternative approach (in case it doesn't exist, or you're stuck with an earlier version, etc.).
Chris
I've used this approach in tandem with a custom formatting template in TOAD to allow me to strip the old SQL from JAVA code, reformat it and get all the extraneous comments and then throw it back into JAVA. It's a pain, but it's allowed us to auto-format on save our Java code now. Thanks for the suggestion!
jnt30
+2  A: 

If you put the plus sign on the beginning of the line, it formats differently:


  String query = 
    "SELECT FOO, BAR, BAZ" 
    +    "  FROM ABC"           
    +    " WHERE BAR > 4";

CPerkins
This might be an interesting compromise. In general, I'd like to refrain from changing the formatting of the code too much due to some undesirable behavior of one tool.In this case, the string concatenation operators are more of an accident rather than the essence of what's going on with the SQL. That's why I prefer to write them at the end of each line. I feel that the SQL should be emphasized as the beginning of the line. But this may be a good way to go in the absence of a solution that lets me preserve my desired formatting. Thanks!
Greg Mattes
You're welcome. Actually, I've been putting my + signs at the front of lines for decades, and not to fool the formatter. I prefer them at the front, because it makes what's happening clearer to me: what's at the end of a line sometimes gets lost. It was the project standard someplace way back when we used woodburning compilers, and it's stuck with me.
CPerkins
+1  A: 

Apparently this feature was added in Eclipse 3.6M6: https://bugs.eclipse.org/bugs/show_bug.cgi?id=27079

Guillaume Alvarez
A: 

I'm using fixed width string-parts (padded with whitespace) to avoid having the formatter mess up my SQL string indentation. This gives you mixed results, and won't work where whitespace is not ignored as it is in SQL, but can be helpful.

    final String sql = "SELECT v.value FROM properties p               "
            + "JOIN property_values v ON p.property_id = v.property_id "
            + "WHERE p.product_id = ?                                  "
            + "AND v.value        IS NOT NULL                          ";
Guus
+7  A: 

Eclipse 3.6 allows you to turn off formatting by placing a special comment, like

// @formatter:off
...
// @formatter:on

It's possible to change the magic strings in the preferences 1.

xpmatteo
Nice, didn't know of this...
Yuval A