revelc/formatter-maven-plugin

Whitespace in Javadoc reported

Closed this issue · 4 comments

Describe the bug
Trailing whitespace in Javadoc is reported as an error, but neither Eclipse 2022-06 nor the Intellij plugin "EclipseCodeFormatter" remove the whitespace from the JavaDoc. To me this looks like a too strict validation of this formatter.

Versions (OS, Maven, Java, and others, as appropriate):

  • Affected version(s) of this project: 2.18.0 and newer
  • OS: Linux Mint 20.3
  • JDK: Azul JDK 11.0.10
  • Maven: 3.8.4
  • Eclipse: 2022-06
  • IntelliJ Plugin EclipseCodeFormatter: 22.0.203.000.0-Eclipse_2021-0

To Reproduce

  • Checkout the example project https://github.com/bennypi/formatter-test
  • Open the project in Eclipse, configure the formatting.xml file for the Eclipse formatter
  • Format the single Java file src/main/java/de/bennypi/formatter/test/SimpleClass.java, but Eclipse should not be changing anything
  • Run mvn validate, this will mark the Java file as not properly formatted.

Expected behavior
The plugin's formatter does not complain about the whitespace in the JavaDoc to be consistent with Eclipse's behaviour.

Eclipse doesn't handle javadocs in the formatter but rather in source cleanup. We have the formatter properly handling the trailing space. In fact, if your run the formatter (not validate), it will trim the trailing spaces. This is an enhancement added in recent versions to fix what we see as a defect in how Eclipse handled javadocs whitespace in relation to everything else.

The specific code is in the formater mojo

  // Process the source one more time and remove any trailing whitespace found
                if (this.removeTrailingWhitespace) {
                    formattedCode = FormatterMojo.REMOVE_TRAILING_PATTERN.matcher(formattedCode).replaceAll("");
                }

So yes it would complain if trailing whitespace is present.

In my opinion this is not a nice user experience. I understand that you want to improve a defect in Eclipse, but this means that using the formatter in Eclipse or IntelliJ does not mean that the files will pass the maven-formatter-plugin. I guess there is no way to exclude whitespace (In Javadoc) for the validate-goal?

See the javadocs

    /**
     * When set to true, remove trailing whitespace on all lines after the formatter has finished.
     * <p>
     * Default to 'true' since 2.18.0
     *
     * @since 2.17.0
     */
    @Parameter(defaultValue = "true", property = "formatter.removeTrailingWhitespace")
    private boolean removeTrailingWhitespace;

If you don't want it on, just set that property to false

Closing this as answered, since it is already currently possible to tune this feature.