Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PRESERVE line endings mode #1172

Closed
nedtwigg opened this issue Apr 19, 2022 · 1 comment
Closed

Add a PRESERVE line endings mode #1172

nedtwigg opened this issue Apr 19, 2022 · 1 comment

Comments

@nedtwigg
Copy link
Member

nedtwigg commented Apr 19, 2022

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)
  • public interface Policy extends Serializable, NoLambda {
    /** Returns the line ending appropriate for the given file. */
    String getEndingFor(File file);
    /** Returns true iff this file has unix line endings. */
    public default boolean isUnix(File file) {
  • 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
Copy link
Member Author

nedtwigg commented Oct 24, 2024

Shipped in plugin-gradle 7.0.0.BETA4 and plugin-maven 2.44.0.BETA4 thanks to @kwin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant