score:904

Accepted answer

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

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

the on/off features have to be turned "on" in eclipse preferences: java > code style > formatter. click on edit, off/on tags, enable enable off/on tags.

it's also possible to change the magic strings in the preferences — check out the eclipse 3.6 docs here.

more information

java > code style > formatter > edit > off/on tags

this preference allows you to define one tag to disable and one tag to enable the formatter (see the off/on tags tab in your formatter profile):

enter image description here

you also need to enable the flags from java formatting

score:-3

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.

score:0

not so pretty but works with default settings and for the first line as well:

string query = "" +
    "select foo, bar, baz" +
    "  from abc          " +
    " where bar > 4      ";

score:2

the phantom comments, adding // where you want new lines, are great!

  1. the @formatter: off adds a reference from the code to the editor. the code should, in my opinion, never have such references.

  2. the phantom comments (//) will work regardless of the formatting tool used. regardless of eclipse or intelij or whatever editor you use. this even works with the very nice google java format

  3. the phantom comments (//) will work all over your application. if you also have javascript and perhaps use something like jsbeautifier. you can have similar code style also in the javascript.

  4. actually, you probably do want formatting right? you want to remove mixed tab/space and trailing spaces. you want to indent the lines according to the code standard. what you dont want is a long line. that, and only that, is what the phantom comment gives you!

score:4

alternative method: in eclipse 3.6, under "line wrapping" then "general settings" there is an option to "never join already wrapped lines." this means the formatter will wrap long lines but not undo any wrapping you already have.

score:4

@xpmatteo has the answer to disabling portions of code, but in addition to this, the default eclipse settings should be set to only format edited lines of code instead of the whole file.

preferences->java->editor->save actions->format source code->format edited lines

this would have prevented it from happening in the first place since your coworkers are reformatting code they didn't actually change. this is a good practice to prevent mishaps that render diff on your source control useless (when an entire file is reformatted because of minor format setting differences).

it would also prevent the reformatting if the on/off tags option was turned off.

score:5

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                          ";

score:6

end each of the lines with a double slash "//". that will keep eclipse from moving them all onto the same line.

score:14

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";

score:16

you have to turn on the ability to add the formatter tags. in the menubar go to:

windows preferences java code style formatter

press the edit button. choose the last tab. notice the on/off box and enable them with a checkbox.

score:31

instead of turning the formatting off, you can configure it not to join already wrapped lines. similar to jitter's response, here's for eclipse sts:

properties → java code style → formatter → enable project specific settings or configure workspace settings → edit → line wrapping (tab) → check "never join already wrapped lines"

save, apply.

enter image description here

score:39

see this answer on so.

there is another solution that you can use to suppress the formatting of specific block comments. use /*- (note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.

/*-
 * here is a block comment with some very special
 * formatting that i want indent(1) to ignore.
 *
 *    one
 *        two
 *            three
 */

source: documentation at oracle.

score:63

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";

Related Query

More Query from same tag