如何防止 Eclipse 格式化程序弄乱文本项目符号注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12578398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:03:21  来源:igfitidea点击:

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

eclipseformattingcomments

提问by einpoklum

I have (Java) comments such as:

我有(Java)评论,例如:

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

Eclipse 自动格式化程序正确设置了注释行宽,但使其:

/*
 * 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.
  • 我不想关闭评论中的行连接,因为我确实希望我的评论行适合最大长度。因此,关闭行连接,或关闭 Javadoc 注释的格式,或暂时暂停自动格式化,都不是可接受的解决方案。
  • 可能相关的问题

回答by Varun Achar

This answersays 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.

这对我也有用。

回答by Zoltán Ujhelyi

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?

您的问题的答案可能与此处相同:如何关闭 Java 代码的某些部分的 Eclipse 代码格式化程序?

Since Eclipse 3.6 you can use the

从 Eclipse 3.6 开始,您可以使用

// @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:

或者,您也可以在首选项中更改注释设置:在 Java/代码样式/格式化程序中编辑格式化程序设置,并检查注释页面中的以下设置:

  • Enable Javadoc formatting (General settings)
  • Indent Javadoc tags (Javadoc settings)
  • 启用 Javadoc 格式(常规设置)
  • 缩进 Javadoc 标签(Javadoc 设置)

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.

顺便说一句,这种手动列表不会在生成的代码中转换为列表。出于这个原因,使用 html 列表可能是有意义的。

回答by Fredrik

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

您可以更改 Eclipse 格式化注释的方式并对块注释进行特殊处理。

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.

转到窗口 - >首选项。Java > 代码风格 > 格式化程序。单击“新建”以创建新模板。然后在“注释”选项卡下禁用块注释格式。

This will however never perform any formatting on block comments.

然而,这永远不会对块注释执行任何格式化。

回答by actc

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:

按照 Bananeweizen 的说法,您还可以用<pre></pre>标签包装相关的评论块,将每个选项卡和空格保留在其位置:

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

回答by Jafar Ali

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 suing. In the Edit Dialog you will find a check box Enable Block Comment Formatting. Uncheck this. Change Profile name as I have done. Apply and OK. You are done.

我知道这个问题很老,但我也可以看到没有人给出满意的答案。这解决了我的问题。去Preference->Java->Code Style->Formatter。在这里转到您要起诉的任何格式化程序样式的编辑。在编辑对话框中,您将找到一个复选框启用块注释格式。取消选中此项。像我一样更改个人资料名称。申请并确定。你完成了。

Please refer this below Image. enter image description here

请参考下面的图片。 在此处输入图片说明

Hope this Helps.

希望这可以帮助。

回答by Bananeweizen

In Java there are 2 different kinds of comments:

在 Java 中有两种不同类型的注释:

  • 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.
  • 块注释:它们没有标准格式,因此像浮动文本一样格式化。
  • JavaDoc 注释:它们具有共同的结构,它们的格式取决于注释中使用的布局标记(如<br><p>)。

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

在 JavaDoc 中,您的示例可以如下编写(并且格式如下所示):

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

回答by Naxos84

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

然后在 Windows -> Preferences -> Java -> Code Style -> Formatter -> Edit... -> Comments 中关闭“Format HTML tags”

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").

我将 Eclipse Neon.3 用于 Java EE,并且我的 Formatter cor 注释设置为 150 的宽度并选择了“从不加入行”。格式化此块注释为我提供了以下结果,我认为这是您可能想要的(尽管我知道您不想“永不加入行”)。

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

只有 1 个小问题。当我向第一个项目符号列表添加更多文本然后再次格式化时,我得到以下结果。

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

那是因为我告诉格式化程序“不要加入行”。当我取消选择“从不加入行”时,我显然得到了与您相同的结果。

回答by jomamaxx

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.

这可以在 Eclipse 设置中的某处访问,对于 VS Code,在项目的根目录中,您需要一个 .settings 目录,其中包含一个包含该行的“org.eclipse.jdt.core.prefs”文件。该文件将包含 VS 代码利用的所有 Eclipse 设置。此设置将类似地应用于使用 Eclipse 设置的其他编辑器。