views:

505

answers:

7

Most often the cleanup rules (Preferences > Java > Code Style > Clean Up) in Eclipse work perfectly and create nice-looking code.

But sometimes, especially with comments and concatenated string snippets (like inline SQL queries), the cleanup just messes things up, and destroys my formatting.

Is there a way to say to Eclipse "Don't touch this block of text! I have formatted it just the way I like, and you would make it just less readable"?

A: 

No. (To the best of my knowledge, and I have had the same problem and have looked many times hard and long...)

Epaga
+1  A: 

I don't know the answer.

But when I don't want to reformat a full document, I often format only a portion. First, I select the lines; then I press Ctrl-Shift-F. The unselected portion of the file is not modified, :-)

Banengusk
Unfortunately, it's our company's policy to have these cleanups as a save action (it's a great thing, when the formatter doesn't work against you!)
Henrik Paul
+1  A: 

I have experienced the same problem, and while I don't have a solution, I can tell you how I work around the problem.

Because of how formatting works, I deliberately avoid lines of code that are excessively long. In general, when I keep lines short, it makes better decisions as to how to format the code. This can even work with SQL statements, for example:

public static final String SELECT_SOMETHING = "SELECT"
  + "OBJECTID, THIS, THAT, THEOTHER, THING"
  + " FROM DBNAME.DBSCHEMA.TABLE_T"
  + " WHERE ID = ?";

This statement formats reasonably, because where possible items were split apart and concatenated together. When I don't do this, I get unpredictable results:

public static final String SELECT_SOMETHING = "SELECT OBJECTID, SOMETHING FROM DBNAME.DBSCHEMA.TABLE_T WHERE ID = ?";

For comments, I place them all on a single line when possible, and allow it to word wrap when it does the formatting.

Also, it is possible to change the style using the code formatter to make things work better for your coding style. You may want everyone on the team to use the same format, just to avoid conflicts. Because it is easier to compare changes with other developers, or prior versions using your source control tool, even if it makes parts of your code less readable, using the formatter has still been to my advantage.

Still, I understand your frustration when the formatter makes bad decisions!

srclontz
A: 

Feeling iffy about replying to my own question, but there's a workaround I currently do (note: I have these cleanup rules as a save-action):

Save (with Ctrl/Cmd-S, don't know if it matters how you save) the code, and let Eclipse mess up your formatting. Then just press Ctrl/Cmd-Z to undo, and immediately re-save. The format reverts back to its original format and seems to be saved as intended.

Henrik Paul
A: 

(for Javadoc comments only)

If I have a block of text that formatted just the way I like, I enclose them by the <pre></pre> tags.

A: 

if you don't want a field to become final (i.i: because you want to change it while debugging), you just assign it to itself on the constructor. This would get an eclipse warning, but your field will stay non-final.

A: 

For SQL statements in code, you can put a single-line comment character at the end of each line. Then the formatter can't reformat it. It's uglier than not having to do it, but it's prettier than if Eclipse formats it.

StringBuffer sql = new StringBuffer() //
   .append("SELECT whatever \n") //
   .append("FROM some_table");