Eclipse formatter adds space in empty comment lines
Asked Answered
C

4

41

The eclipse code formatter adds a trailing space in each empty Javadoc comment line (see screenshot).

enter image description here

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.

Can it be a difference between eclipse versions? (I am using Springsource Tool Suite 2.7.1, which is based on Helios, I believe)

Convalescence answered 10/8, 2011 at 9:9 Comment(3)
STS 2.7.1 is based on Indigo by default. Helios is availible as an option.Introspect
I'm also looking for a way to tell Eclipse not to add a space in empty comment lines. "Remove Trailing Spaces" Save Action removes them, but the next time the file is opened the Eclipse formatter adds them back. This drives me CRAZY!Giagiacamo
Apparently the trailing space remover runs first then the the formatter for the Javadoc - duh - yes it is super annoyingDucan
S
12

Go to Preferences -> Java -> Editor -> Save actions and select Format edited lines.

After applying changes all trailing whitespaces in Javadoc will be removed

Note: "Additional actions"->"Remove trailing white space on all lines" must also be enabled.

Sweetbrier answered 10/12, 2012 at 5:27 Comment(4)
That would just format the comments according to the formatting style which by default adds the space, so I don't see how this would help.Stormystorting
Ah I see what you mean now, the setting "Additional actions"->"Remove trailing white space on all lines" must also be enabled, otherwise this do not help. Edited answer to say so.Stormystorting
"Remove trailing white space on all lines" won't work for block comments. At least for the same settings under the JS editor.Moorman
It is true that the trailing whitespaces stay in the javadoc comments.Suburb
K
11

I found that if you go to Preferences → Java → Editor → Content Assist → Save Actions the following configuration works most of the time:

  • ☑ 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.)

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.

Khoury answered 28/8, 2013 at 19:38 Comment(0)
H
9

I've also had this issue and solved it by doing a Search/Replace in Eclipse.

  1. Go to Search > File and be sure Regular Expression is checked.
  2. Enter the string ^([\s]+\*)([\s]+)$ into the Containing text field.
  3. Constrain your search to *.java File name patterns
  4. Press the Replace (not Search) button
  5. Make sure regular expression is checked in the Replace box and use the string $1 as a replacement. Replace at will.

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.

Helfand answered 31/1, 2013 at 21:20 Comment(2)
The regex should be ^([\s]+\*)([\s]+)$, which is how it's written in the post but it got escaped, and apparently edits need to be 6 characters.Lederman
No regular expression necessary: see this solution : #957271Taconite
T
7

see https://bugs.eclipse.org/bugs/show_bug.cgi?id=360523 for a possible workaround.

Tye answered 11/10, 2011 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.