25

I have (Java) comments such as:

/* 
 * The quick brown fox jumped over the lazy dog.
 *
 *
 * Notes:
 * - The quick brown fox jumped over the lazy dog. The quick brown fox
 *   jumped over the lazy dog. The quick brown fox jumped over the lazy
 *   dog.
 * - The second quick brown fox jumped over the lazy dog. The quick brown
 *   jumped over the lazy dog. The quick brown fox jumped over the lazy
 *   dog.
 */

The Eclipse auto-formatter sets the comment line width properly, but makes it:

/*
 * The quick brown fox jumped over the lazy dog.
 * 
 * 
 * Notes: - The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy
 * dog. The quick brown fox jumped over the lazy dog. - The second quick brown fox jumped over the
 * lazy dog. The quick brown jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
 */

How do I the code formatter to leave the bulleted lists as-is, while still otherwise processing the comment?

Notes:

  • I don't want to turn off line joining in comments, because I do want my comment lines to be fit to the maximum length. So turning off line joining, or turning off formatting for Javadoc comments, or temporarily suspending autoformatting, are not acceptable solutions.
  • Possibly related question.
Community
  • 1
  • 1
einpoklum
  • 86,754
  • 39
  • 223
  • 453
  • I'm having the same problem with Scala in Eclipse, but the solutions below are Java-specific, as the Scala code formatter tab has different options. Is there a way to turn off comment-reformatting for Scala? – Glenn Strycker Oct 31 '14 at 18:44
  • @GlennStrycker: Well, since nobody's given me an acceptable answer, we're pretty much in the same boat for now :-( – einpoklum Nov 01 '14 at 18:18

8 Answers8

41

This answer says that you add a hypen right after the opening /*, i.e.:

/*- 
 * The quick brown fox jumped over the lazy dog.
 *
 *
 * Notes:
 * - The quick brown fox jumped over the lazy dog. The quick brown fox
 *   jumped over the lazy dog. The quick brown fox jumped over the lazy
 *   dog.
 * - The second quick brown fox jumped over the lazy dog. The quick brown
 *   jumped over the lazy dog. The quick brown fox jumped over the lazy
 *   dog.
 */

This worked for me too.

Community
  • 1
  • 1
Varun Achar
  • 13,274
  • 7
  • 53
  • 71
15

The answer to your question is probably the same as here: How to turn off the Eclipse code formatter for certain sections of Java code?

Since Eclipse 3.6 you can use the

// @formatter:off
...
// @formatter:on

annotations to disable code formatting.

Update:

Alternatively, you could also change the comment settings in the Preferences: in Java/Code Style/Formatter edit the formatter settings, and check the Comments page for the following settings:

  • Enable Javadoc formatting (General settings)
  • Indent Javadoc tags (Javadoc settings)

Btw, this kind of manual list does not translate into a list in the generated code. It might make sense to use a html list for this reason.

Community
  • 1
  • 1
Zoltán Ujhelyi
  • 13,598
  • 2
  • 30
  • 36
  • Well, that's kind of what I specified would not be a solution for me. I don't want to dirty my files like that, nor add two lines to every such comment... – einpoklum Sep 25 '12 at 14:38
  • I updated my answer to provide some alternatives (e.g. disabling the entire Javadoc formatting). – Zoltán Ujhelyi Sep 25 '12 at 16:01
  • Can you explain how the 'Indent Javadoc tags' can help me? – einpoklum Feb 17 '14 at 14:55
  • I am not sure, but I was hoping it would not change line breaks/indentation if turned off. If not, than sorry again for suggesting something that does not work. Sadly, the only foolproof solution is to define HTML lists; they will work, but will less easy to read in the Java source code. – Zoltán Ujhelyi Feb 17 '14 at 18:54
6

You can make changes to how Eclipse formats comments and have special handling for block comments.

Got to Window -> preferences. Java > Code style > Formatter. Click on "New" to create a new template. Then under the tab "Comments" disable block comment formatting.

This will however never perform any formatting on block comments.

Fredrik
  • 10,407
  • 5
  • 43
  • 78
  • The point is that I _do_ want block comment formatting. I just don't want it to mess up these parts of my comments. – einpoklum Sep 25 '12 at 14:39
6

I know the question is old but, I can also see nobody gave a satisfactory answer.

This resolved my issue.

Go to Preference->Java->Code Style->Formatter. Here, go to Edit of whatever Formatter style you are using. In the Edit Dialog you will find a checkbox Enable Block Comment Formatting. Uncheck this. Change Profile name as I have done. Apply and OK. You are done.

Please refer to this below Image. enter image description here

Hope this Helps.

Jafar Ali
  • 907
  • 1
  • 14
  • 35
4

Picking up what Bananeweizen said, you could also wrap the relevant comment block with <pre></pre> tags, keeping every tab and space in it's place:

/* 
 * The quick brown fox jumped over the lazy dog.
 *
 *
 *<pre>Notes:
 * - The quick brown fox jumped over the lazy dog. The quick brown fox
 *   jumped over the lazy dog. The quick brown fox jumped over the lazy
 *   dog.
 * - The second quick brown fox jumped over the lazy dog. The quick brown
 *   jumped over the lazy dog. The quick brown fox jumped over the lazy
 *   dog.</pre>
 */
actc
  • 794
  • 1
  • 6
  • 21
  • This will prevent the Eclipse formatter to apply the appropriate line width to these comments... – einpoklum Mar 24 '15 at 20:12
  • Note, there is a preference to toggle whether or not Eclipse will format the contents of a
     tag. I'm on Eclipse Neon (Mac), and the preference is under "Java | Code Style | Formatter." Once there, edit the active profile, and select the "Comments" tab. Make sure the "Format Java code snippets inside 'pre' tags" is unchecked if you want to prevent this formatting.
    – chaserb May 16 '17 at 14:45
2

In Java there are 2 different kinds of comments:

  • block comments: They have no standard format and are therefore formatted like floating text.
  • JavaDoc comments: Those have a common structure, and their formatting depends on the layout tags (like <br> or <p>) used in the comment.

In JavaDoc your example could be written like below (and would be formatted as shown):

/**
 * The quick brown fox jumped over the lazy dog.
 * 
 * <p>
 * Notes:
 * <ul>
 * <li>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown
 * fox jumped over the lazy dog.</li>
 * <li>The second quick brown fox jumped over the lazy dog. The quick brown jumped over the lazy dog. The quick
 * brown fox jumped over the lazy dog.</li>
 * </ul>
 * </p>
 */
Bananeweizen
  • 20,962
  • 8
  • 64
  • 86
  • Well, you're both right and wrong. We don't yet compile Javadoc (in my team project), I'm trying to get that adopted. So for starters I haven't used the HTML markup. But supposing that I did, and I had a `
    ` after my `Notes:`, and a `
  • ` instead of ` - ` - I'd still have the same problem. Right?
  • – einpoklum Sep 26 '12 at 18:31
  • 1
    If you start the comment with `/**` (2 stars!) then Eclipse will recognize this as JavaDoc and will format it according to the tags. You can then use `Notes
    • Item1
    • Item2
    ` for the list, which will be _rendered_ in the Eclipse hover like a bullet list. In the source it will be formatted with line breaks, but admittedly it is still not as readable like your own format.
    – Bananeweizen Sep 26 '12 at 18:46
  • Even if I make it a regular comment (`/**`), I still get the same behavior. – einpoklum Sep 26 '12 at 18:51
  • Enter this and run the formatter. It will format that as separate paragraph with a list of bullet items. `/** headline

    Notes:

    • item1
    • item2
    */`
    – Bananeweizen Sep 26 '12 at 19:00