spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Add a `PRESERVE` line endings mode

Open nedtwigg opened this issue 3 years ago • 0 comments

Right now we have UNIX, WINDOWS, PLATFORM_NATIVE, and our default GIT_ATTRIBUTES. These are all "content-blind", in that they can declare the line endings are without looking at the content of the file being formatted.

I'm open to merging a new mode PRESERVE which should work as such:

  • Add a parameter String raw to each of these two methods (this will be **BREAKING** change in /CHANGES.md)
  • https://github.com/diffplug/spotless/blob/4600bc36f7f23522037b3febf6a98ba6131cfbed/lib/src/main/java/com/diffplug/spotless/LineEnding.java#L126-L131
  • Use that string to count the number of \n and \r characters to determine what the correct line ending should be
    • Some files in the wild will have a mixture of line endings, but preserving that is out-of-scope, we'll pick whichever line ending is more common in the file
    • Assuming that the \r are always paired up as \r\n, then
    • return count_n >= 2 * count_r ? "\n" : "\r\n"
  • for plugin-gradle/CHANGES.md and plugin-maven/CHANGES.md this is just ### Added, not breaking

I think that GIT_ATTRIBUTES is what almost everyone should be using, so I'm not going to build PRESERVE myself, but I'd be happy to merge a PR for it if you want it.

nedtwigg avatar Apr 19 '22 06:04 nedtwigg