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

28 浏览
0 Comments

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

有没有办法让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
     */

变成这个:

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

我已经在Windows > Preferences > Java > Code Style > Formatter设置中进行了试验,但找不到保留注释格式的选项。我正在使用Eclipse 3.4.0。

admin 更改状态以发布 2023年5月20日
0
0 Comments

更新 2010,正如问问者指出的这个答案,在Eclipse 3.6中使用特殊字符串// @formatter:off就足够了。

在提问时,该功能还没有推出。


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

通过Java Formatter功能(Windows > Preferences > Java > Code Style > Formatter),您可以创建新的Formatter配置文件。

在注释选项卡中(在eclipse3.5中),您可以确保在“Javadoc注释设置”中不勾选“格式化HTML标记”。
在“常规设置”部分也要勾选“永不加入行”。

然后您的注释就应该写成:

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

注:我使用了Javadoc注释,而不是简单的注释,因为我认为这样长的文本注释最好放在方法前面。此外,Javadoc部分还有更多的格式化参数可供使用。
如果放在方法前面(真正的Javadoc注释),则可以使用HTML标记

帮助在Javadoc页面中正确呈现。

0
0 Comments

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

/*-
 * 这是一个带有一些非常特殊的格式的块注释,我希望indent(1)忽略它。
 *
 *    one
 *        two
 *            three
 */

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

0