tags:

views:

625

answers:

7

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.

+1  A: 

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
A: 

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
+1  A: 

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.

Chris Cameron
+1  A: 

Did you ever find an answer to this? Eclipse is just killing me right now. I use reformatting occasionally and don't have a problem with it, but every so-often, I get a section like this:

final Rectangle[] videoScales = 
{   
    new Rectangle(50, 50, 500, 400), // Largest
    new Rectangle(100, 50, 200, 200), 
    new Rectangle(0, 0, 120, 100) // Smallest
};

Where Eclipse just wants to TRASH it. It comes up with no acceptable format under any options.

Bill K
Eclipse also loves to wreck inline SQL/HTML that i carefully wrap and space out for readability.
Chris Nava
+1  A: 

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! :)

furtelwart
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
A: 

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
+2  A: 

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
Since 3.6 (Helios). See new and noteworthy page for details: http://download.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/eclipse-news-part2.html#JavaFormatter
Zoltán Ujhelyi