spring-io / spring-javaformat

Apache License 2.0
797 stars 110 forks source link

Detect and preserve line separators instead of using system default #340

Closed ParkerM closed 1 year ago

ParkerM commented 2 years ago

Fixes #280

The current behavior of Formatter#format falls back to the system default line separator when one isn't explicitly specified, causing a bunch of false positives when building on Windows machines with LF configured via .gitconfig or .gitattributes. This modifies the behavior to instead detect a common line separator per source file, and only fall back to system default when one cannot be determined for some reason.

(See linked issue for additional context)

philwebb commented 1 year ago

Looking at the Eclipse codebase I see similar detection logic in https://github.com/eclipse-platform/eclipse.platform.text/blob/150da4b2675d18f53a2087b74f6e6bed65ce0472/org.eclipse.text/src/org/eclipse/jface/text/DefaultLineTracker.java#L47

philwebb commented 1 year ago

@ParkerM I'm in the process of merging this one, I've got a local branch that replaces the regex. I'm not sure it's worth actually reusing the eclipse code directly.

ParkerM commented 1 year ago

10-4, here's a backup in case anything was clobbered by my recent push: https://github.com/ParkerM/spring-javaformat/tree/backup/detect-eol

Thanks for looking into it!

philwebb commented 1 year ago

Thanks very much for the PR @ParkerM. I've now merged this with a few changes to the tests which hopefully mean we don't need .gitattributes. I was a little worried that someone would edit the file and the line-endings would change.

ParkerM commented 1 year ago

Right on, good call on the test changes (and double good call on swapping out the clumsy regex impl). Just ran a local build on my Windows 10 box and everything's green! 🥳