42

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.

Paul Adamson
  • 2,011
  • 2
  • 18
  • 22

7 Answers7

61

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 */
stm
  • 626
  • 6
  • 3
  • @ZoltánUjhelyi previous link no longer works. See [Eclipse API Documentation Formatter](https://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-java-editor-formatter.htm) – Mr00Anderson Jan 23 '19 at 16:04
9

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);
    }
}
matt burns
  • 24,742
  • 13
  • 105
  • 107
2

I only know the answer for comments:

Eclipse is smart enough to only re-format the comments where the generated JavaDoc wouldn't change (i.e. where whitespace doesn't matter):

/**
 * foo <i>
 * bar </i>
 * 
 * <pre>
 *   foo
 * bar
 * </pre>
 */

will be reformatted into

/**
 * foo <i> bar </i>
 * 
 * <pre>
 *   foo
 * bar
 * </pre>
 */

Note how the content of the <pre> tags is not reformatted.

For long String literals, I'd suggest that they might be an indication that you should be externalizing some of them.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Please note that Eclipse will format contents of
     tag if it sees it as a java code (unless you uncheck Window->Preferences->Java->Code Style->Formatter->Edit...[Button]->Comments[Tab]->"Format Java code snippets inside 'pre' tags")
    – gswierczynski Aug 05 '14 at 23:31
1

I just found this question because I'm also annoyed by the lack of this feature.

A small search showed this question and the following answer: Stop eclipse from line wrapping?

Eclipse 3.5 supports this. (It hit release candidate a a few days ago, so could be worth checking out)

Good luck with this! :)

Community
  • 1
  • 1
guerda
  • 23,388
  • 27
  • 97
  • 146
  • Good reference, although I won't be able to use it because I'm working on a stupid framework built on top of eclipse 3.2 – Bill K Aug 26 '09 at 21:00
1

I stumbled upon this, 'cause I'd love to see section coloring in Eclipse. Imagine you could simply give some background color to sections within your code. Wouldn't it make it more accessible, especially when you're faded at 4 a.m.? :)

arieltools
  • 1,136
  • 3
  • 15
  • 21
  • nice idea! i'd also love to be able to grey-out the uninteresting bits of code to draw the eye to the important bits. even a 'bold' feature would be good for this! – Paul Adamson Dec 23 '10 at 16:04
1

I am not sure about the exact feature you're referring to, but you can change the line wrap policy of expressions, which may help you with your strings problem. See:

Window->Preferences->Java->Code Style->Formatter

Click "Edit..." Button

Click "Line Wrapping" Tab

In the tree, choose Expressions->Assignments, then change the indent policy at the bottom of the window.

Of course there are myriad other options inside the formatter, you may find many of those useful as well for other rules, such as where to put your braces, or how to indent control blocks.

0

You can mark the code you want to format and selct format with right mouse click in this section. This is a "whitlist" solution, perhaps it helps...

powtac
  • 40,542
  • 28
  • 115
  • 170