Turn off Eclipse formatter for selected code area? [duplicate]
Asked Answered
I

2

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 ?

Irruptive answered 25/2, 2011 at 8:27 Comment(2)
Do you have a CSS inside your .java class?Hurff
nope. .css but i want the annotation or stuff like given here @formator:on and off but dont know how to implement it #1821408Irruptive
V
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.

Valvule answered 25/2, 2011 at 8:35 Comment(6)
awesome . thanks , its aint in Galileo but its in Helios .,Irruptive
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).Valvule
Note this needs to be enabled in your preferences formatter settings.Raconteur
Any short cut to do this for a block of code with out typing formatter:off and on?Whitebait
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.*;Aporia
It is not working with my Eclipse 2021-03 installation. What could be the problem?Superimpose
P
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.

Paltry answered 25/2, 2011 at 8:35 Comment(1)
Quite helpful thanks!!Pyriform

© 2022 - 2024 — McMap. All rights reserved.