使Eclipse的Java代码格式化程序忽略块注释

Pop*_*ops 76 eclipse comments formatter

有没有办法让Eclipse的内置Java代码格式化程序忽略注释?每当我运行它,它会变成这样:

    /*
     * 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
     */
Run Code Online (Sandbox Code Playgroud)

进入这个:

    /*
     * 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
     */
Run Code Online (Sandbox Code Playgroud)

我已经玩过Windows> Preferences> Java> Code Style> Formatter设置,但找不到用于保持注释格式的设置.我正在使用Eclipse 3.4.0.

小智 176

您可以使用另一种解决方案来抑制特定块注释的格式./*-在块注释的开头使用(注意连字符),如果格式化文件的其余部分,格式不会受到影响.

/*-
 * Here is a block comment with some very special
 * formatting that I want indent(1) to ignore.
 *
 *    one
 *        two
 *            three
 */

资料来源:http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141999.html#350

  • 遗憾的是,这只适用于"常规"块注释,而不是Javadoc注释. (14认同)

Von*_*onC 37

更新2010,正如OP指出的,在这个答案中,// @formatter:offEclipse 3.6中的特殊字符串就足够了.

在提问时尚未提供.


原答案:2009年6月,Eclipse 3.4/3.5

使用Java Formatter(Windows > Preferences > Java > Code Style > Formatter),您可以创建新的Formatter配置文件.

在"注释"选项卡(在eclipse3.5中)中,您可以确保在" Javadoc comment settings"中取消选中" Format HTML tags".
另请查看Never join lines" General settings"部分中的" ".

那么你的评论应写成:

/**
 * 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
 */
Run Code Online (Sandbox Code Playgroud)

注意:我做了一个Javadoc注释,而不是一个简单的注释,因为我相信带有那么多文本的注释可能更好地放在方法前面.另外,Javadoc部分有更多格式参数可供使用.
如果是在一个方法(真正的Javadoc)的面前,HTML标签<dl>,<dt>并且<dd>将有助于中的Javadoc视图中正确地呈现它.


Pop*_*ops 24

我刚从一位同事那里了解到,Eclipse提供了可用于此的特殊格式标记:

// @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
Run Code Online (Sandbox Code Playgroud)

请注意,您可能需要通过首选项菜单→Java>代码样式>格式化程序手动启用此功能,单击编辑,选择关/开标签选项卡并选中启用关/开标签().

一个快速的谷歌字符串@formatter:off带我到另一个SO答案,它在禁用代码块的格式化程序的上下文中提到了这个功能.我已经确认它适用于行注释,"普通"块注释和Javadoc块注释.


Tho*_*ler 17

另一种可能性是在Javadoc中使用HTML的<pre>:

/**
 * <pre>
 *    this
 *   is
 *      kept
 *  as
 *    is
 * </pre>
 */
Run Code Online (Sandbox Code Playgroud)

至少这是我倾向于在源代码注释中嵌入我的ASCII艺术:)

  • 请注意,如果 Eclipse 将 &lt;pre&gt;&lt;/pre&gt; 标记的内容视为 java 代码,它将对其进行格式化(除非您取消选中 Window-&gt;Preferences-&gt;Java-&gt;Code Style-&gt;Formatter-&gt;Edit...[Button ]-&gt;注释[Tab]-&gt;“格式化'pre'标签内的Java代码片段”) (2认同)

小智 9

使用<pre> </pre>标记环绕特定文本.