33

When I ctrl+shift+f up the project code, its doing its job just fine, everywhere.

But its messing the CSS, specially those filter parts and adding bunch of spaces.

Also how do I specify some portion of Java code to not be formated by that command ?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 2
    Do you have a CSS inside your .java class? – Grzegorz Oledzki Feb 25 '11 at 08:31
  • nope. .css but i want the annotation or stuff like given here @formator:on and off but dont know how to implement it http://stackoverflow.com/questions/1820908/how-to-turn-off-the-eclipse-code-formatter-for-certain-sections-of-java-code/3353765#3353765 –  Feb 25 '11 at 08:35

2 Answers2

86

To prevent specific portions of Java code from being formatted, go to "Window > Preferences > Java > Code Style > Formatter". Click the "Edit..." button, go to the "Off/On Tags" tab and enable the tags. Afterwards, you can simply embed those tags in Java code to disable the formatting in-between them. If you don't change the default tags, something like this will do :

//@formatter:off
this.
    portion.of(code
); // will not be touched by the formatter
//@formatter:on
but this will be
       reformatted.

IIRC, this option only exists since Eclipse 3.6.

As for css code, if you have installed Eclipse WTP, go to "Window > Preferences > Web > CSS Files > Editor" and you will find some basic formatting options there.

Kellindil
  • 4,523
  • 21
  • 20
  • 1
    awesome . thanks , its aint in Galileo but its in Helios ., –  Feb 25 '11 at 08:40
  • Yes, that was my "only exists since Eclipse 3.6" part :). I wasn't sure, but your comment did confirm that this option wasn't in Eclipse 3.5 (Galileo). – Kellindil Feb 25 '11 at 09:53
  • Note this needs to be enabled in your preferences formatter settings. – Philip Rego Feb 08 '18 at 18:59
  • Any short cut to do this for a block of code with out typing formatter:off and on? – Java_deep Jan 04 '19 at 15:03
  • Nice one. Is there a similar feature for organise imports. As I do not want static imports * to be removed. For eg. import static io.restassured.RestAssured.*; – Jay Feb 03 '19 at 08:06
  • It is not working with my Eclipse 2021-03 installation. What could be the problem? – Park JongBum May 25 '21 at 21:47
5

You cannot configure the formatting at class level, but you can configure at the project level. To do the configuration follow this path in eclipse

window->preferences-> java -> code style -> formatter

now select the configure project specific settings and do your changes.

HenryFBP
  • 13
  • 1
  • 3
GuruKulki
  • 25,776
  • 50
  • 140
  • 201