How to turn off the Eclipse code formatter for certain sections of Java code? How to turn off the Eclipse code formatter for certain sections of Java code? java java

How to turn off the Eclipse code formatter for certain sections of Java code?


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


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


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.