Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make eclipse's autoformatter ignore a section of code?

Tags:

eclipse

i remember there being a way of marking a section of code in eclipse (special comment or annotation?) which made the autoformatter ignore that section. Or I may have drempt this...

Used mainly when I have strings which wrap onto several lines and i don't want the autoformatter to rearrange this.

like image 597
Paul Adamson Avatar asked Dec 09 '08 12:12

Paul Adamson


2 Answers

Since eclipse 3.5 (or 3.6) this is possible: - Go to project properties -- Java Code Style -- Formatter -- Edit... - choose the tab marked "Off/On Tags", - include the tags in comments in your source code, like

/* @formatter:on */ 
like image 186
stm Avatar answered Sep 20 '22 19:09

stm


You can wrap the code you don't want auto-formatted between these special comments:

normal code  /* @formatter:off */ strangely laid out code /* @formatter:on */  normal code 

Here's a basic usage example that makes a json string (slightly) more readable:

public class SomeTest {      @Test     public void can_deserialize_json() {         /* @formatter:off */         String json = "" +         "{" +         "   \"id\" : 123," +         "   \"address1\" : blah," +         "   \"shippingInfo\" : {" +         "      \"trackingUrl\" : null," +         "      \"price\" : 350" +         "   }," +         "   \"errorMessage\" : null" +         "}";         /* @formatter:on */         MyClass.deserializeJson(json);     } } 
like image 24
matt burns Avatar answered Sep 20 '22 19:09

matt burns