Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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+F for Eclipse autoformat.

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?

like image 988
Min Naing Oo Avatar asked Aug 08 '14 07:08

Min Naing Oo


People also ask

How do you get rid of leading and trailing white spaces?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

How do I ignore whitespace in Eclipse?

Go to Preferences and then search for Compare. There should be a setting to ignore whitespace. It will be in Preferences->General->Compare/Patch. Check the "Ignore White space" option.

Why is trailing whitespace bad?

Whitespace is not always insignificant. In some cases, trailing whitespace can significantly change the meaning of a line of code or data. In most cases whitespace is there to format the code for human readers.


2 Answers

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

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.

like image 198
greg-449 Avatar answered Nov 02 '22 22:11

greg-449


When I try it in Eclipse Juno SR2, greg-449's suggestion to 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.

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 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 RegexpSingleline check like this (explanation of the regex):

<module name="RegexpSingleline">
    <property name="format" value="^(?!\s+\* $).*?\s+$"/>
    <property name="message" value="Line has trailing spaces."/>
</module>
like image 29
barfuin Avatar answered Nov 02 '22 22:11

barfuin