如何在 Eclipse 自动格式化中删除多行注释星号后面的尾随空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25198091/
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 21:51:31  来源:igfitidea点击:

How to remove trailing space behind multiline comment's asterisk in Eclipse autoformat

eclipsecheckstyle

提问by Min Naing Oo

enter image description here

在此处输入图片说明

Java -> Code Style -> Code Templates -> Comments -> Methods -> Edit

I have removed whitespace in above template and save the setting but space wasn't removed when I press Ctrl+Shift+Ffor Eclipse autoformat.

我已经删除了上面模板中的空格并保存了设置,但是当我按Ctrl+ Shift+F进行 Eclipse 自动格式化时没有删除空格。

Checkstyle is complaining me about this space and I can't remove manually in every java file. Do I need to use regex to replace instead?

Checkstyle 向我抱怨这个空间,我无法在每个 java 文件中手动删除。我需要使用正则表达式来代替吗?

采纳答案by greg-449

One possibility is to configure the Java editor save actions to remove trailing spaces.

一种可能性是配置 Java 编辑器保存操作以删除尾随空格。

This is configured in 'Preferences > Java > Editor > Save Actions'. Check 'Addition actions' and click 'Configure...'. On the 'Code Organizing' tab you can configure the 'Remove trailing whitespace' option.

这是在“首选项 > Java > 编辑器 > 保存操作”中配置的。选中“添加操作”并单击“配置...”。在“代码组织”选项卡上,您可以配置“删除尾随空格”选项。

回答by barfuin

When I try it in Eclipse Juno SR2, greg-449's suggestionto use save actions works for me. Be sure to select the "all lines" radio button, though. An empty line of Javadoc is considered an empty line despite the single asterisk, so "ignore empty lines" is not a good setting for you.

当我在 Eclipse Juno SR2 中尝试它时,greg-449 的使用保存操作的建议对我有用。不过,请务必选择“所有行”单选按钮。尽管有单个星号,Javadoc 的空行仍被视为空行,因此“忽略空行”对您来说不是一个好的设置。

There is also the "Clean up" feature in Eclipse, which allows you to batch run this on all your files in order to save time. What "Clean up" does can be configured like so (note "all lines" is selected again):

Eclipse 中还有“清理”功能,它允许您对所有文件批量运行以节省时间。可以像这样配置“清理”所做的事情(注意再次选择“所有行”):

Eclipse Clean Up

日食清理

Last but not least, if you just want to stop Checkstyle from complaining, you can reconfigure the check so that it does not flag a single trailing space in an otherwise empty line of Javadoc. Configure the RegexpSinglelinecheck like this (explanation of the regex):

最后但并非最不重要的一点是,如果您只想阻止 Checkstyle 抱怨,您可以重新配置检查,使其不会在 Javadoc 的空行中标记单个尾随空格。像这样配置RegexpSingleline检查(正则表达式的解释):

<module name="RegexpSingleline">
    <property name="format" value="^(?!\s+\* $).*?\s+$"/>
    <property name="message" value="Line has trailing spaces."/>
</module>