使 Eclipse 的 Java 代码格式化程序忽略块注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1024886/
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
Making Eclipse's Java code formatter ignore block comments
提问by Pops
Is there a way to make Eclipse's built-in Java code formatter ignore comments? Whenever I run it, it turns this:
有没有办法让 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
*/
into this:
进入这个:
/*
* 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
*/
I have already played around with the Windows > Preferences > Java > Code Style > Formatter settings but can't find one for keeping comment formatting. I'm using Eclipse 3.4.0.
我已经尝试过 Windows > Preferences > Java > Code Style > Formatter 设置,但找不到保持注释格式的设置。我正在使用 Eclipse 3.4.0。
采纳答案by VonC
Update 2010, as pointed by the OPand in this answer, the special string // @formatter:off
in Eclipse 3.6 is enough.
更新 2010,正如OP所指出的,在这个答案中,// @formatter:off
Eclipse 3.6 中的特殊字符串就足够了。
It was not availableat the time of the question.
在提问时它不可用。
Original answer: June 2009, Eclipse 3.4/3.5
原答案:2009 年 6 月,Eclipse 3.4/3.5
With the Java Formatter (Windows > Preferences > Java > Code Style > Formatter
), you can create a new Formatter profile.
使用 Java 格式化程序 ( Windows > Preferences > Java > Code Style > Formatter
),您可以创建新的格式化程序配置文件。
In the Comments tab (in eclipse3.5), you can make sure, in the "Javadoc comment settings
", to uncheck "Format HTML tags
".
Check also the "Never join lines
" in the "General settings
" section.
在 Comments 选项卡中(在 eclipse3.5 中),您可以确保在“ Javadoc comment settings
”中取消选中“ Format HTML tags
”。
还要检查Never join lines
“ General settings
”部分中的“ ”。
Then your comment should be written as:
那么你的评论应该写成:
/**
* 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
*/
Note: I have made a Javadoccomment, and not a simple comment, as I believe a comment with that much text in it may be better placed in front of a method. Plus, Javadoc sections have more formatting parameters to play with.
If it is in front of a method (true Javadoc), the HTML tags <dl>
, <dt>
and <dd>
will help to present it properly within the Javadoc view.
注意:我已经做了一个Javadoc注释,而不是一个简单的注释,因为我相信其中包含这么多文本的注释最好放在方法前面。另外,Javadoc 部分有更多的格式化参数可供使用。
如果是在一个方法(真正的Javadoc)的面前,HTML标签<dl>
,<dt>
并且<dd>
将有助于中的Javadoc视图中正确地呈现它。
回答by Michelle Crane
There is another solution that you can use to suppress the formatting of specific block comments. Use /*-
(note the hyphen) at the beginning of the block comment, and the formatting
won't be affected 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
来源:http: //www.oracle.com/technetwork/java/javase/documentation/codeconventions-141999.html#350
回答by Pops
I just learned from a co-worker that Eclipse offers special formatting tags that can be used for this:
我刚刚从一位同事那里了解到 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
Note that you may need to manually enable this feature through the Preferences menu → Java > Code Style > Formatter, clicking on Edit, selecting the Off/On Tagstab and checking Enable Off/On tags(source).
请注意,您可能需要通过 Preferences 菜单 → Java > Code Style > Formatter 手动启用此功能,单击Edit,选择Off/On Tags选项卡并选中Enable Off/On tags( source)。
A quick Google for the string @formatter:offbrought me to this other SO answer, which mentioned this feature in the context of disabling the formatter for code blocks. I've confirmed that it works for line comments, "normal" block comments and Javadoc block comments as well.
对字符串@formatter:off 的快速 Google将我带到了另一个 SO 答案,它在禁用代码块的格式化程序的上下文中提到了此功能。我已经确认它适用于行注释、“普通”块注释和 Javadoc 块注释。
回答by Thomas Keller
Another possibility is to use HTML's <pre> in Javadoc:
另一种可能性是在 Javadoc 中使用 HTML 的 <pre> :
/**
* <pre>
* this
* is
* kept
* as
* is
* </pre>
*/
At least this is how I tend to embed my ASCII-art in source code comments :)
至少这是我倾向于在源代码注释中嵌入我的 ASCII 艺术的方式:)
回答by Philip
Surround the specific text with <pre> </pre>
tags.
用<pre> </pre>
标签包围特定文本。
回答by skaffman
In Eclipse 3.4: Preferences, Java->Code Style->Formatter, then edit profile, comments tab. There's a bunch of options there for controlling comment formatting.
在 Eclipse 3.4 中:Preferences,Java->Code Style->Formatter,然后编辑 profile,comments 选项卡。有很多选项可用于控制注释格式。
回答by Milan Kamboya
Try this one. This worked for me. With the Java Formatter (Windows > Preferences > Java > Code Style > Formatter), you can create a new Formatter profile from existing and then edit it.
试试这个。这对我有用。使用 Java Formatter(Windows > Preferences > Java > Code Style > Formatter),您可以从现有创建一个新的 Formatter 配置文件,然后对其进行编辑。
If Eclipse doesn't allow to save this then create a new one and save in that.
回答by Antoniossss
If you want to supress formatting in eclipse, you can always wrap content that is intended to NOT TO BE FORMATED into <pre>UNFORMATTED CONTENT</pre>
tag. Javadoc formatter will detect that tag, and leave everything between that tags unformatted.
如果您想在 Eclipse 中禁止格式化,您始终可以将不打算格式化的内容包装到<pre>UNFORMATTED CONTENT</pre>
标签中。Javadoc 格式化程序将检测该标签,并保留该标签之间的所有内容未格式化。
Pros:
优点:
- Rest of Javadoc is still formatted
- Javadoc's html output will be "unformatted" as well because of pre tags
- Javadoc 的其余部分仍然是格式化的
- 由于 pre 标签,Javadoc 的 html 输出也将是“未格式化的”
Cons:
缺点:
- Not seeing one
- 一个不见
回答by K. Andy wang
one workaround is to add pretag for the comments that you don't want eclipse to format
一种解决方法是为您不希望 eclipse 格式化的注释添加pre标记
/**
* <pre>
* this part
* is
* out of
* format
*
* </pre>
*/
回答by GH Dev
You can change this in Windows - Preferences - Java - Code Style - Formatter, than click the Edit.. button, find Comments, choose the Never Join Lines.
您可以在 Windows - Preferences - Java - Code Style - Formatter 中更改此设置,然后单击 Edit.. 按钮,找到 Comments,选择 Never Join Lines。
Then, it should be OK.
那么,应该没问题。