Eclipse 格式化程序在空注释行中添加空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7008340/
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
Eclipse formatter adds space in empty comment lines
提问by Elbonian
The eclipse code formatter adds a trailing space in each empty Javadoc comment line (see screenshot).
eclipse 代码格式化程序在每个空的 Javadoc 注释行中添加一个尾随空格(见截图)。
The formatter of a colleague is always removing those spaces (and thus producing annoying SVN diffs). He is claiming to use the same formatter settings (XML file). Nevertheless, I tried all the options in the comments tab of the formatter settings page - without success.
同事的格式化程序总是删除这些空格(从而产生烦人的 SVN 差异)。他声称使用相同的格式化程序设置(XML 文件)。尽管如此,我尝试了格式化程序设置页面的评论选项卡中的所有选项 - 没有成功。
Can it be a difference between eclipse versions? (I am using Springsource Tool Suite 2.7.1, which is based on Helios, I believe)
eclipse 版本之间可以有区别吗?(我使用的是基于 Helios 的 Springsource Tool Suite 2.7.1,我相信)
回答by ZhekaKozlov
Go to Preferences -> Java -> Editor -> Save actionsand select Format edited lines.
转到Preferences -> Java -> Editor -> Save actions并选择Format encrypted lines。
After applying changes all trailing whitespaces in Javadoc will be removed
应用更改后,Javadoc 中的所有尾随空格都将被删除
Note: "Additional actions"->"Remove trailing white space on all lines" must also be enabled.
注意:还必须启用“附加操作”->“删除所有行的尾随空格”。
回答by Steve Pomeroy
I found that if you go to Preferences → Java → Editor → Content Assist → Save Actions
the following configuration works most of the time:
我发现如果你Preferences → Java → Editor → Content Assist → Save Actions
使用以下配置大部分时间都可以工作:
- ? Perform the selected actions on save
- ? Format source code
- ? Additional actions
- Remove trailing white spaces on all lines
- ? 在保存时执行选定的操作
- ? 格式化源代码
- ? 附加操作
- 删除所有行的尾随空格
(That is to say, turn off the Format source code
option in Save Actions
, but keep the Remove trailing white spaces on all lines
in Additional actions
on.)
(也就是说,关闭Format source code
的选项Save Actions
,但保持Remove trailing white spaces on all lines
在Additional actions
上。)
The formatter and save actions duke it out, but save actions seems to win most of the time. The downside of this is that you need to periodically manually format the code.
格式化程序和保存操作将其一决高下,但保存操作似乎在大多数情况下获胜。这样做的缺点是您需要定期手动格式化代码。
This is using Eclipse Juno.
这是使用 Eclipse Juno。
回答by Pawn
I've also had this issue and solved it by doing a Search/Replace in Eclipse.
我也遇到过这个问题,并通过在 Eclipse 中执行搜索/替换来解决它。
- Go to Search> Fileand be sure Regular Expression is checked.
- Enter the string
^([\s]+\*)([\s]+)$
into the Containing text field. - Constrain your search to *.javaFile name patterns
- Press the Replace (not Search) button
- Make sure regular expression is checked in the Replace box and use the string $1 as a replacement. Replace at will.
- 转到“搜索”>“文件”并确保选中“正则表达式”。
^([\s]+\*)([\s]+)$
在包含文本字段中输入字符串。- 将搜索限制为*.java文件名模式
- 按替换(不是搜索)按钮
- 确保在替换框中选中正则表达式并使用字符串 $1 作为替换。随意更换。
What this is doing is searching for lines that start with blank space characters that contain a * and then further blank space and no other content. It then replaces the whole line with the first matched part of the regex, which is just the initial spacing and * of the comment line.
这样做是搜索以包含 * 的空格字符开头的行,然后是更多的空格并且没有其他内容。然后用正则表达式的第一个匹配部分替换整行,这只是注释行的初始间距和 *。
回答by Ayushman
see https://bugs.eclipse.org/bugs/show_bug.cgi?id=360523for a possible workaround.
有关可能的解决方法,请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=360523。