How to prevent Eclipse formatter from messing up text-bulleted comments?

廉价感情. 提交于 2021-02-15 10:18:21

问题


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.

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.

Hope this Helps.




回答5:


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>
 */



回答6:


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>
 */



回答7:


How about:

/**
 * The quick brown fox jumped over the lazy dog.
 *
 * <p>Notes:
 * <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>
 */

And then turn of "Format HTML tags" in Windows -> Preferences -> Java -> Code Style -> Formatter -> Edit... -> Comments

This still does the line width line breaking.

EDIT:

I used the following block-comment:

/*
 * 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 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 quick brown fox jumped over the lazy dog.
 */

I'm using Eclipse Neon.3 for Java EE and my Formatter cor comments is set to a width of 150 and "Never join lines" selected. Formatting this block comment gives me the following result which I think is what you might want (Though I know that you don't want to "never join lines").

/*
 * 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
 * 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 quick brown fox jumped over the lazy dog.
 */

There is just 1 small problem. When I add some more text to the first bulleted list and then formatting again I get the following result.

/*
 * 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
 * SOME MORE TEXT TO WRAP
 * 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 quick brown fox jumped over the lazy dog.
 */

That is because I told the formatter "not to join lines". When I unselect "Never join lines" I apparently get the same result as you have.




回答8:


Specifically, you want to set:

org.eclipse.jdt.core.formatter.comment.format_block_comments=false

This is accessible somewhere in Eclipse settings, for VS Code, inside the root of the project you need a .settings directory, with a "org.eclipse.jdt.core.prefs" file that contains that line. This file would contain all your Eclipse settings that VS code leverages. This setting would apply similarly for other editors that use the Eclipse settings.



来源:https://stackoverflow.com/questions/12578398/how-to-prevent-eclipse-formatter-from-messing-up-text-bulleted-comments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!