Replacing the Eclipse Java Code Formatting Blog

Is there a way to force Eclipse inline Java code to format ignore comments? Whenever I run it, it turns like this:

/* * PSEUDOCODE * Read in user 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 */ 

in it:

  /* * PSEUDOCODE Read in user 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 already played with Windows settings> Settings> Java> Code Style> Formatter, but I can not find it to save the formatting of comments. I am using Eclipse 3.4.0.

+73
eclipse comments formatter
Jun 21 '09 at 10:25
source share
12 answers

The 2010 update, as pointed out by OP and in this answer , the special line //@formatter:off in Eclipse 3.6 is enough.

This was not available at the time of the issue.




Original answer: June 2009, Eclipse 3.4 / 3.5

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

On the Comments tab (in eclipse3.5), you can verify that in the " Javadoc comment settings " section, clear the " Format HTML tags " checkbox.
Check also the " Never join lines " in the " General settings " section.

Then your comment should be written like this:

 /** * PSEUDOCODE * Read in user 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 made a Javadoc comment, not a simple comment, because I believe that a comment with so much text can be better placed before the method. In addition, Javadoc sections have more formatting options for playback.
If it is in front of the method (true Javadoc), the HTML tags <dl> , <dt> and <dd> will help to correctly represent it in the Javadoc view.

+35
Jun 22 '09 at 5:45
source share

There is another solution that can be used to suppress the formatting of individual block comments. Use /*- (note the hyphen) at the beginning of the block comment and the formatting will not change 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

+171
Mar 28 '11 at 23:19
source share

I just learned from a colleague that Eclipse offers special formatting tags that you can use 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 

Please note that you may need to manually enable this function using the "Settings" menu β†’ Java> Code Style> Formatter by clicking Modify by selecting the Off / On tab . and by checking Enable off / on Tags ( source ).

A quick google for the @formatter: off line led me to this other SO answer that mentioned this function in the context of disabling formatting for code blocks. I confirmed that it works with line comments, β€œnormal” block comments, and Javadoc block comments.

+23
Aug 28 '12 at 16:33
source share

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

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

At least that's how I tend to embed ASCII art in source code comments :)

+15
Jan 03 '13 at 14:00
source share

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

+8
Feb 10 '10 at 17:57
source share

In Eclipse 3.4: Settings, Java-> Code Style-> Formatter, then edit the profile, comments tab. There are many options for controlling the formatting of comments.

+2
Jun 21 '09 at 22:31
source share

Answer checked only in Eclipse:

  • The problem with the accepted answer (Java Formatter (Windows> Preferences> Java> Code Style> Formatter), you can create a new Formatter profile), you affect every javadoc in your code. Perhaps this is undesirable for other parts of the code that need to be formatted.
  • The problem with the / * solution is that it only works with "regular" block comments, not Javadoc comments (as pointed out by Pops).

My suggestion would be to just use html tags. You can, for example, wrap text after breaking with p tags to see an additional line feed line in the displayed html. Here's what your example looks like (I added some more text and used p for case 1):

 /** * PSEUDOCODE<br> * * Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy * eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam * voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet * clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit * amet. <br> * * Three cases are possible:<br> * * Case 1: foobar<br> * <p> * Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy * eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam * voluptua. At vero eos et accusam et justo duo dolores et ea rebum. * </p> * * Case 2: fred hacker do case 2 things<br> * * Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy * eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam * voluptua. At vero eos et accusam et justo duo dolores et ea rebum.<br> * */ 
+1
Oct 29 '13 at 9:35 on
source share

If you want to suppress formatting in eclipse, you can always wrap content that is NOT FORMED in the <pre>UNFORMATTED CONTENT</pre> . The Javadoc formatter will detect this tag and leave everything between these tags unformatted.

Pros:

  • The rest of the javadoc is still formatted
  • Javadoc html output will also be "unformatted" due to pre-tags

Minuses:

  • I do not see one
+1
Nov 26 '15 at 11:13
source share

one way is to add a pre tag for comments that you do not want to format eclipse.

 /** * <pre> * this part * is * out of * format * * </pre> */ 
+1
Sep 06 '17 at 2:34 on
source share

It depends on the language.

For example, if you are working with javascript, you should go to "Window -> Preferences -> Javascript -> Code Style -> Format", and then edit the formatting options.

Change (reflecting changes in OP questions

To edit the formatting of java code, go to "Window β†’ Settings β†’ Java β†’ Code Style β†’ Formatter"

At the top of the panel you will see

Active profile:
Eclipse [built-in]

From there you have one button on the right, "Change" and two below "New ..." and "Import ...". I would recommend editing an existing profile.

The edit dialog has a series of tabs at the top. The last tab is Comments. To completely disable comment formatting, clear the "Enable Javadoc comment formatting", "Enable block comment formatting", "Enable line comment formatting", and "Enable comment comment formatting" checkboxes.

0
Jun 21 '09 at 22:31
source share

You can change this in Windows - "Settings" - "Java" - "Code Style" - "Formatter", than click the "Change" button, find "Comments", select "Never join lines."

Then everything should be in order.

0
Jan 01 '19 at 12:51
source share

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

If Eclipse does not allow you to save this, create a new one and save to it. enter image description here

0
Jun 16 '19 at 18:17
source share



All Articles