views:

352

answers:

2

I am using Eclipse 3.5.1 (Galileo) and am having trouble with one of the auto-formatting settings.

At the moment when I auto-format a blank line is inserted between each and every field declaration. The behaviour I'm looking for is to organise field declarations without a blank line between. Except in the case where there is already one or more blank lines between the declarations, in that case I'd like the formatter to respect the space, and reduce >=1 lines to 1 line.

I know this was possible at some point with Eclipse (can't remember the version but I have used a setting for this), but I can't seem to find the correct settings in Window->Preferences->Java->Code Style->Formatter.

Which setting do I configure to enable this behaviour?


EDIT: It seems the behaviour I'm describing is the default, and for some reason I couldn't find a way to return to that without removing the .settings directory. It also couldn't be reproduced, which suggests that it's something weird with the project settings (it's checked out from an open source project). Although I've found a hacky way to fix this, I'm still looking for suggestions and will be able to verify and accept an answer.

+1  A: 

Strange, I cannot reproduce that behavior with my own formatter profile (eclipse3.5.1).

There was a setting back in 2003 (bug 38523) to "Insert new line after each parameter if line is too long", but this has been integrated into the "Line Wrapping" panel a long time ago.

Are you sure the newlines are not added because of a wrapping issue (due to a maximum line length too short)?

The tab "Blank Lines" does have an "Existing blank lines" section, with a "Number of empty lines to preserve" field which can be set to 1, and should address the later part of your requirements.
In that same tab "Blank Lines", check also the value of the "Before field declarations": "1" could explain the blank lines you see.

alt text


The formatter options are in the org.eclipse.jdt.ui.prefs file, which is:

  • either in your workspace (.metadata.plugins\org.eclipse.core.runtime.settings)
  • or in the .settings of your project.

You can try and empty that file to check if the problem remains. It that clear up the issue, try adding some lines into that file, up to the point where the problem comes back.

VonC
Thanks for your answer. I'm pretty sure it's not line wrapping - I would have had to mistakenly alter that to something really small, as `int a; int b;` get a new line between them. The strange thing is, in my version of the blank lines tab, the example is actually as I want it, with no extra lines, but still the formatting inserts an extra line. I have other workspaces with the same Eclipse, so I'll check their settings to see what's different. Cheers.
Grundlefleck
To clarify, when I said "`int a; int b;` get a new line between them", I meant a and b are already on separate lines, and an extra, blank line is inserted.
Grundlefleck
@Grundlefeck: understood. This is quite mysterious, then. May be an interaction with an installed plugin? Do you observe the same thing on the fresh installation of Eclipse (with your settings exported/re-imported)? Anyway, if you find the answer, I am interested ;)
VonC
+1  A: 

I found a poor solution to my problem.

I went to the project and removed the .settings directory and restarted Eclipse. In the process I lost all my other formatting rules of course, which is why it sucks. But the blank lines are no longer are added, and the behaviour I described is the default behaviour (probably why I was used to it).

I still hope to accept a better solution for this, I have kept the old settings around to verify any suggestions.


EDIT: I think I've found the problem.

For some reason, possibly related to the project settings being checked out, any settings which I selected through Preferences were not actually being honoured. So although the preferences suggested by Von C were right, changing to them had no effect. I had the same problem trying to change the auto-generated comments, but didn't put two and two together (what actually alerted me to this was finally seeing "ARGH" being generated within a comment I couldn't seem to remove :-p).

So in this case, the problem was not actually the settings, but something else within Eclipse I don't even want to investigate.

Grundlefleck
+1. Could you copy back your formatting setting file, section by section (with a restart between each addition) to see if one of those formatting sections has any effect on this "blank line" issue?
VonC
See my edit, I don't think that'll actually help with what the underlying problem is. Thanks VonC.
Grundlefleck
@Grundlefleck I just added to my answer the exact setting file with the formatting options. It is more for me to remember, because you say in your edit you cannot easily reproduce the problem, but it is still worth a try to empty/fill back that setting file just in case.
VonC
The actual setting that brings back the blank line is `Before Field Declarations: 1`, as in your answer. But `diff`ing the two different prefs file, it looks like 3.5.1 handles the values differently from whatever version of Eclipse was used to originally check them in. I think it was a case of the different Eclipses not handling settings well in general, rather than a specific setting. "Before Field Declarations" is the one though, as you said.
Grundlefleck