Making Eclipse's Java code formatter ignore block comments
Asked Answered
C

11

86

Is there a way to make Eclipse's built-in Java code formatter ignore comments? Whenever I run it, it turns this:

    /*
     * PSEUDOCODE
     * Read in user's string/paragraph
     * 
     * Three cases are possible
     * Case 1: foobar
     *         do case 1 things
     * Case 2: fred hacker
     *         do case 2 things
     * Case 3: cowboyneal
     *         do case 3 things
     *         
     * In all cases, do some other thing
     */

into this:

    /*
     * PSEUDOCODE Read in user's string/paragraph
     * 
     * Three cases are possible Case 1: foobar do case 1 things Case 2: fred
     * hacker do case 2 things Case 3: cowboyneal do case 3 things
     * 
     * In all cases, do some other thing
     */

I have already played around with the Windows > Preferences > Java > Code Style > Formatter settings but can't find one for keeping comment formatting. I'm using Eclipse 3.4.0.

Carabin answered 21/6, 2009 at 22:25 Comment(4)
It's right there in the Formatter config, I don't know how you're missing it. Edit the profile, there's a dialog box with 8 tabs, the last tab is for comment formatting.Louannlouanna
I do see the comment tab, but the formatting problems happen no matter what combination of checkboxes I use.Carabin
possible duplicate of How to turn off the Eclipse code formatter for certain sections of Java code?Eosinophil
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?Arkhangelsk
B
42

Update 2010, as pointed by the OP and in this answer, the special string // @formatter:off in Eclipse 3.6 is enough.

It was not available at the time of the question.


Original answer: June 2009, Eclipse 3.4/3.5

With the Java Formatter (Windows > Preferences > Java > Code Style > Formatter), you can create a new Formatter profile.

In the Comments tab (in eclipse3.5), you can make sure, in the "Javadoc comment settings", to uncheck "Format HTML tags".
Check also the "Never join lines" in the "General settings" section.

Then your comment should be written as:

/**
 * PSEUDOCODE
 * Read in user's string/paragraph
 * 
 * Three cases are possible:
 * <dl>
 *   <dt>Case 1: foobar</dt>
 *     <dd>        do case 1 things</dd>
 *   <dt>Case 2: fred hacker</dt>
 *     <dd>        do case 2 things</dd>
 *   <dt>Case 3: cowboyneal</dt>
 *     <dd>        do case 3 things</dd>
 * </dl>        
 * In all cases, do some other thing
 */

Note: I have made a Javadoc comment, and not a simple comment, as I believe a comment with that much text in it may be better placed in front of a method. Plus, Javadoc sections have more formatting parameters to play with.
If it is in front of a method (true Javadoc), the HTML tags <dl>, <dt> and <dd> will help to present it properly within the Javadoc view.

Bensky answered 22/6, 2009 at 5:45 Comment(4)
The relevant part of this answer, for me, was that the HTML tags are NOT automatically inserted at line breaks and the like. Probably obvious to most, but I didn't realize I had to hand-code things like <br>s until I saw it here.Carabin
@Lord Torgamus: not only you have to add thosetags, but the first sentence requires a periode and a space, before the <br />! (see bugs.sun.com/bugdatabase/view_bug.do?bug_id=4165985)Bensky
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?Arkhangelsk
this does not answer the question askedRogers
E
196

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#350

Euchromosome answered 28/3, 2011 at 23:19 Comment(5)
Never seen that before, even out of context. +1!Carabin
Sadly, this only works with "regular" block comments, not Javadoc comments.Carabin
Perfect for Version: Juno Service Release 1. Thank you, my savior!Neuropath
and even this is useful when you have your own formatter which is appending stars for each line start and you dont want default formatter to do it then you could use /*- ...... */ which is very usefulManger
Still works in 2020 (Version: 2020-06 (4.16.0)) :-) very nicePoulterer
B
42

Update 2010, as pointed by the OP and in this answer, the special string // @formatter:off in Eclipse 3.6 is enough.

It was not available at the time of the question.


Original answer: June 2009, Eclipse 3.4/3.5

With the Java Formatter (Windows > Preferences > Java > Code Style > Formatter), you can create a new Formatter profile.

In the Comments tab (in eclipse3.5), you can make sure, in the "Javadoc comment settings", to uncheck "Format HTML tags".
Check also the "Never join lines" in the "General settings" section.

Then your comment should be written as:

/**
 * PSEUDOCODE
 * Read in user's string/paragraph
 * 
 * Three cases are possible:
 * <dl>
 *   <dt>Case 1: foobar</dt>
 *     <dd>        do case 1 things</dd>
 *   <dt>Case 2: fred hacker</dt>
 *     <dd>        do case 2 things</dd>
 *   <dt>Case 3: cowboyneal</dt>
 *     <dd>        do case 3 things</dd>
 * </dl>        
 * In all cases, do some other thing
 */

Note: I have made a Javadoc comment, and not a simple comment, as I believe a comment with that much text in it may be better placed in front of a method. Plus, Javadoc sections have more formatting parameters to play with.
If it is in front of a method (true Javadoc), the HTML tags <dl>, <dt> and <dd> will help to present it properly within the Javadoc view.

Bensky answered 22/6, 2009 at 5:45 Comment(4)
The relevant part of this answer, for me, was that the HTML tags are NOT automatically inserted at line breaks and the like. Probably obvious to most, but I didn't realize I had to hand-code things like <br>s until I saw it here.Carabin
@Lord Torgamus: not only you have to add thosetags, but the first sentence requires a periode and a space, before the <br />! (see bugs.sun.com/bugdatabase/view_bug.do?bug_id=4165985)Bensky
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?Arkhangelsk
this does not answer the question askedRogers
C
30

I just learned from a co-worker that Eclipse offers special formatting tags that can be used for this:

// @formatter:off
/*
 * ╔════════╦═══════╦══════════╗
 * ║ Month  ║ Sales ║ Position ║
 * ╠════════╬═══════╬══════════╣
 * ║ June   ║ 44k   ║ 2nd      ║
 * ║ July   ║ 39k   ║ 2nd      ║
 * ║ August ║ 49k   ║ 4th      ║
 * ╚════════╩═══════╩══════════╝
 *
 * This comment shouldn't be formatted, and will now be ignored by the formatter.
 */
// @formatter:on

Note that you may need to manually enable this feature through the Preferences menu → Java > Code Style > Formatter, clicking on Edit, selecting the Off/On Tags tab and checking Enable Off/On tags (source).

A quick Google for the string @formatter:off brought me to this other SO answer, which mentioned this feature in the context of disabling the formatter for code blocks. I've confirmed that it works for line comments, "normal" block comments and Javadoc block comments as well.

Carabin answered 28/8, 2012 at 16:33 Comment(3)
How did you make it work for JavaDoc comments? I've found I had to put line comments before and after the JavaDoc comments for this to work, like you did for the block comment in this answer. I was hoping I could turn off formatting just for a section of a JavaDoc comment.Brink
@MichaelScheper it's been a while, and I don't remember exactly... did you try VonC's advice?Carabin
This is excellent, because it tells eclipse to dont format code as well. Useful when you want to keep your own formatting.Shimmy
G
20

Another possibility is to use HTML's <pre> in Javadoc:

/**
 * <pre>
 *    this
 *   is
 *      kept
 *  as
 *    is
 * </pre>
 */

At least this is how I tend to embed my ASCII-art in source code comments :)

Glowworm answered 3/1, 2013 at 14:0 Comment(1)
Please note that Eclipse will format contents of <pre></pre> tag if it sees it as a java code (unless you uncheck Window->Preferences->Java->Code Style->Formatter->Edit...[Button]->Comments[Tab]->"Format Java code snippets inside 'pre' tags")Edirne
C
9

Surround the specific text with <pre> </pre> tags.

Communism answered 10/2, 2010 at 17:57 Comment(0)
F
3

Try this one. This worked for me. With the Java Formatter (Windows > Preferences > Java > Code Style > Formatter), you can create a new Formatter profile from existing and then edit it.

If Eclipse doesn't allow to save this then create a new one and save in that. enter image description here

Filberto answered 16/6, 2019 at 18:17 Comment(1)
This is the answer with recent Eclipse : you can now disable any comment formatting (javadoc, block or inline).Adamina
L
2

In Eclipse 3.4: Preferences, Java->Code Style->Formatter, then edit profile, comments tab. There's a bunch of options there for controlling comment formatting.

Louannlouanna answered 21/6, 2009 at 22:31 Comment(0)
M
2

one workaround is to add pre tag for the comments that you don't want eclipse to format

/**
 * <pre>
 *    this part
 *   is
 *      out of
 *  format
 *    
 * </pre>
 */
Mammal answered 6/9, 2017 at 2:34 Comment(1)
True... but I don't see what this adds to what Philip or Thomas Keller already said in their answers years ago.Carabin
K
1

If you want to supress formatting in eclipse, you can always wrap content that is intended to NOT TO BE FORMATED into <pre>UNFORMATTED CONTENT</pre> tag. Javadoc formatter will detect that tag, and leave everything between that tags unformatted.

Pros:

  • Rest of Javadoc is still formatted
  • Javadoc's html output will be "unformatted" as well because of pre tags

Cons:

  • Not seeing one
Kidron answered 26/11, 2015 at 11:13 Comment(0)
L
0

It is language-dependent.

For example, if working with javascript, you would go to "Window -> Preferences -> Javascript -> Code Style -> Formatter" and then edit the formatter options.

Edit (reflecting changesin OP Questions

For editing java code formatting, go to "Window -> Preferences -> Java -> Code Style -> Formatter"

At the top of the panel you will see

Active Profile:
Eclipse [built-in]

From there you have one button to the right, "Edit", and two below, "New..." and "Import...". I would recommend Editing the existing profile.

In the edit profile dialog, there are a series of tabs along the top. The last tab is "Comments". To completely disable comment formatting, uncheck "Enable Javadoc comment formatting", "Enable block comment formatting", "Enable line comment formatting", and "Enable header comment formatting".

Lixivium answered 21/6, 2009 at 22:31 Comment(0)
A
0

You can change this in Windows - Preferences - Java - Code Style - Formatter, than click the Edit.. button, find Comments, choose the Never Join Lines.

Then, it should be OK.

Aquavit answered 1/1, 2019 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.