google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Don't reformat 'javadoc' comments that appear before package/imports, to avoid mangling license headers

Open pettermahlen opened this issue 8 years ago • 7 comments

The use case is license headers added by, for instance, maven license plugins. These headers are not by default formatted in the way that google-java-format requires, and since neither (for good reasons - neither code styles nor licenses should be changed lightly) are very configurable, conflicts are hard to avoid. (The mycila license plugin allows configuration, and with a bit of effort, you can make it create license headers that don't get reformatted - I haven't succeeded to do that with the Codehaus plugin).

Creating this issue as a different one than #139, which is about javadoc comments.

pettermahlen avatar Dec 08 '17 13:12 pettermahlen

Do you have an example of a license header with >100 column lines that get wrapped by the formatter?

cushon avatar Dec 08 '17 16:12 cushon

I have an example with <100 line columns that gets reformatted: https://github.com/pettermahlen/format-example.

I wasn't able to reproduce it easily this morning, but I also ran into a situation where lines < 100 columns got reformatted, at the same time introducing <p> tags where there were empty lines. This was also with the exact same Apache license data, and the reformatting involved meant the lines were made longer, closer to 100 columns, I would expect, though I didn't verify that.

pettermahlen avatar Dec 11 '17 08:12 pettermahlen

In that example it's removing trailing whitespace from the lines before and after the line containing http://www.apache.org/licenses/LICENSE-2.0.

The other behaviour you describe (adding <p> tags and reflowing lines) would only happen if the license header was a javadoc comment, not a block comment.

cushon avatar Dec 11 '17 08:12 cushon

Ah, yes, that makes sense. The mycila plugin uses javadoc formatting for license headers by default so it would have happened when I was experimenting with that.

pettermahlen avatar Dec 11 '17 08:12 pettermahlen

And the reason for trying the mycila plugin out was because it doesn't add trailing whitespace, btw.

pettermahlen avatar Dec 11 '17 08:12 pettermahlen

Would it be a good change if google-java-format refused to reformat any block comment that comes before the package/import statements?

kevinb9n avatar May 10 '18 17:05 kevinb9n

That sounds pragmatic to me.

pettermahlen avatar May 11 '18 06:05 pettermahlen