How to make Eclipse Formatter to not destroy commented out code? - eclipse

A lot of my co-workers enjoy leaving behind commented out code, how to make Eclipse Formatter to not destroy formatting of that code(now it makes a big blob or block of it).
P.S. I know I can somehow configure the formatter to just ignore that code, I done it once, but I cant remember how.

In the Eclipse Formatter Settings (Preferences/Java/Code Style/Formatter) in the tab Comments UNCHECK the checkbox "Enable Block Comment Formatting" and "Enable Line Comment Formatting" These should be disabled anyway.

One option in 3.6 would be to tell Eclipse not to format these blocks, using /* #formatter:off */
Another option that I use a lot when working in other people's code is to select the lines I'm working on before invoking the formatter - that way I only format the areas I've actually changed, and leave the rest untouched (to get more readable diffs etc.).

For Eclipse 3.7.1, there's a new configuration page (Off/On Tags) in the code format profiles. Make sure you've enabled them. You can also specify the exact comment off and comment on tags.

Related

VSCode inserts space after comment sequence

Whenever VSCode formats code, for almost all languages, it will insert spaces after the comment escape sequence (//, #, et cetera).
Although this does not work for C code, I assume that this might be some kind of global setting.
Is it possible to disable the insertion of this extra space somewhere? I follow a style where I don't use the extra spaces after "code-only" comments.
Any (lightweight) extensions that do this (or similar)? Should I make my own...?
I'd like for this solution to work for all languages, although Java is what I use the most.
I tried searching for some extensions related to comments, but it seems that there are none doing this. Is a linter what I need..?
Sorry and thank you in advance!
VS Code doesn't have a default formatter that works across every language so, there is no global solution for you. You will have to check, for every language you noticed such behavior, if the formatter is built in or not, and look for settings individually.
You commented about using Java, and VS Code Java support doesn't provides a formatter, so I suppose you have some Java extension installed, maybe from RedHat. If that's the case, I've noticed (based on extension's Readme) that It has a Java: Open Java Formatter Settings command, which says opens the Eclipse formatter settings. Creates a new settings file if none exists. It's been a long time I don't use Eclipse, so I'm not sure how its settings works, and if it does support the format rule you want. But I suggest you to take a look.
Hope this helps
There is a setting that should affect all languages:
Editor > Comments: Insert Space
It is enabled by default, uncheck it.
It seems that setting java.format.comments.enabled to false was somewhat helpful:
java.format.comments.enabled setting in VSCode
However, it disables formatting for all comments, even JavaDoc ones.

Disable IDE auto format command

I have a code base that many developers work on. Some people blindly use auto format in their IDE and mess up the formatting of sql statements or other things. It appears that some IDE's like eclipse do have the ability to use //#Formatter:off (which I have put around many things I do want to not get formatted), but by default these are ignored. Thus, is there a way to either enable the setting so that will be used by default, or is there a way to disable any shortcut key linked to auto format using some sort of maven plugin? Basically I am looking for a way to enforce this for anyone who is working on the codebase since everyone can change their own formatter settings.
I found https://code.revelc.net/formatter-maven-plugin/format-mojo.html, but I dont think it can do either of those things.
Thanks in advance!

Looking for an Eclipse Code Formatter

I know the built in Codeformatter in Eclipse but there is one problem with it.
I work in a team of 20 developers and all confirmed to a codeformatting I do not like. E.g. the wrap lines after 80 characters or opening braces on the same line, etc.
So what I want is a formatter which formats the code in 2 ways: When I work on a class, I want the formatter to make me happy and when I push it to SVN it should format it so other wont be bothered.
I hope you already have settings done in your machine. If so, then you can always export the preference via File -> EXPORT -> General/Preferences and ask your colleagues to import it.
If not, then try with this...
https://code.google.com/p/google-styleguide/source/browse/trunk/eclipse-java-google-style.xml
Moreover it is not really a good idea to format while checking it into SVN as it might bother the fellow developers who might not feel comfortable when lots of formatting changes for a minor one line bug fix.

Can I use a different Eclipse Formatter Profile for on-open and on-save?

I love the Eclipse feature that allows the Formatter Profile to be applied on save, but what if my preferred bracing style differs from the project "standard"? Is there a way to apply one Formatter Profile when I open a file, and another when I save it?
I suppose there would be complications when debugging, but maybe there's some brilliant Eclipse Plugin developer that has solved this problem. I can hope, right?
Not to my knowledge for Java sources.
Especially since, at the first saved modification, the bracing style would then revert to the second Formatter profile, hence modifying the reading experience.
Plus that would required a mandatory recompilation on project/source opening, for the debug informations to be correctly stored.
The only kind of formatting I know about are for non-compiled files, like xml ones.
OxygenXml has such a setting in place, with PrettyPrint.
Format and indent the document on open
When checked, the Format and indent the document on open operation will format and indent the document before opening it in the editor panel.

How to configure Eclipse formatter to not insert blank line between field declarations?

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.
The Eclipse i am using is:
Eclipse Java EE IDE for Web Developers.
Version: Indigo Service Release 2
Build id: 20120216-1857
As the picture above shows, there is a 'Between import groups' under the Blank Lines tab and it works for me. You can check it out.
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.
(source: comscigate.com)
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.
..............................
17Jan2020
Just remove the blank line after first field declaration.
..............................
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.