Stop Eclipse from mangling my comments
Asked Answered
R

5

26

Is there a way to stop eclipse from mangling the comments in my source files?

My main issue is stopping it from word wrapping them. I've managed to configure eclipse to not word wrap any code but it still seems to do it to comments.

Code that I enter like this:

public int myVariable = 100; // this variable is a very interesting variable and it does lots of stuff

End up like this:

public int myVariable = 100; // this variable
                             // is a very
                             // interesting
                             // variable and
                             // it does lots
                             // of stuff
Rad answered 19/10, 2009 at 11:0 Comment(6)
While I am generally opposed to putting comments after code, the problem is also valid in other ways: Eclipse sometimes mangles HTML in Javadoc or auto-generated non-Javadoc comments for members so that they don't display the same anymore. Very ugly.Blodget
@Johannes: it's easy enough to turn off code formatting for all comments (including Javadoc)Supernational
I'm opposed to comments after code too but our lead developer isn't and eclipse mangling code makes diffing files for changes really difficult.Rad
I've heard of people being opposed to comments, but why after code? Is this a religious thing?Sailboat
This is just a silly religious preference (to tell people that only strategic, and not tactical comments are allowed). If you have a very short tactical comment, then putting it at end of line can be completely valid. Your alternative would be to add a whole new line above, which takes up vertical space. Developers frequently discount the effect of being wasteful with vertical space. I think a method is much easier to read if the entire method is visible on one screen, without vertical scrolling. Tactical comments may help achieve that. No reason to remove tools from the toolbox.Dotdotage
Observing similar behaviour in Oxygen.1 Eclipse 4.7.1. I have one formatter active profile enabled and it does record changes (once I discovered that you have to elsewhere enable Oomph Preference Recording).. But setting "Preserve white space between code and line comments" has no effect. I have also disabled "Line comment formatting". No effect. Switching "Never join lines" no effect. No project-specific settings.Interval
S
16

Go to the "Window" menu -> preferences
choose the editor you're interested in from the list on the left.(e.g. Java)
then there should be a subsection called "Code Style", and under that "Formatter".

Then you'll need to create a new formatter profile. (The built-in profile can't be edited) On the last tab of the profile configuration is options for comment formatting

Supernational answered 19/10, 2009 at 11:10 Comment(0)
S
80

There is another solution that you can use to suppress the formatting of specific block comments. Use /*- (note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.

/*-
 * Here is a block comment with some very special
 * formatting that I want indent(1) to ignore.
 *
 *    one
 *        two
 *            three
 */

Source: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141999.html#680

Sagacious answered 28/3, 2011 at 22:56 Comment(4)
Best response. SHould be answer.Hazy
Lovely - just what I was looking for!Treasonous
No, seems that is no valid for /** comments. Any suggestions?Drennen
Updated url to oracle docs. oracle.com/technetwork/java/javase/documentation/…Anthia
S
16

Go to the "Window" menu -> preferences
choose the editor you're interested in from the list on the left.(e.g. Java)
then there should be a subsection called "Code Style", and under that "Formatter".

Then you'll need to create a new formatter profile. (The built-in profile can't be edited) On the last tab of the profile configuration is options for comment formatting

Supernational answered 19/10, 2009 at 11:10 Comment(0)
S
12
  1. Go to Preferences -> Java -> Code Style -> Formatter
  2. There edit the profile and switch to the last tab called "Comments"
  3. Uncheck the "Enable line comment formatting"
Sycophant answered 19/10, 2009 at 11:8 Comment(2)
Tx... also a checkbox to disable block comment formatting, which is what I wanted...Moldboard
For me it was the "Never join lines" that helpedDelaryd
M
1

Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit and then pick tab Comments unchecked Enable Line comment formatting and Enable block comment formatting

Myke answered 3/4, 2013 at 5:41 Comment(1)
This is identical to the answer @Simon Lehmann made much earlier than you Answered here... -1 for you, +1 for him.Storytelling
B
0

You can use <pre></pre> wrapped, this block will not format.

/*
 * <pre class="prettyprint">
 * public class Activity extends ApplicationContext {
 *     protected void onCreate(Bundle savedInstanceState);
 *
 *     protected void onStart();
 *
 *     protected void onRestart();
 *
 *     protected void onResume();
 *
 *     protected void onPause();
 *
 *     protected void onStop();
 *
 *     protected void onDestroy();
 * }
 * </pre>
 */
Beghtol answered 7/12, 2019 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.