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 support for MAC_CLASSIC (\r only) #1196

Closed
DemonJun opened this issue May 5, 2022 · 4 comments
Closed

add support for MAC_CLASSIC (\r only) #1196

DemonJun opened this issue May 5, 2022 · 4 comments

Comments

@DemonJun
Copy link

DemonJun commented May 5, 2022

If you are submitting a bug, please include the following:

  • summary of problem
    • spotless check can not checkout the file with cr(\r) lineSeparator
  • gradle or maven version
    • maven with 3.8
  • spotless version
    • 2.22.4
  • operating system and version
    • macos
  • copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.22.4</version>
                <configuration>
                    <formats>
                        <format>
                            <includes>
                                <include>*.java</include>
                                <include>*.xml</include>
                            </includes>
                            <trimTrailingWhitespace/>
                            <endWithNewline/>
                            <encoding>UTF-8</encoding>
                            <indent>
                                <spaces>true</spaces>
                                <spacesPerTab>4</spacesPerTab>
                            </indent>
                        </format>
                    </formats>
                    <java>
                        <encoding>UTF-8</encoding>
                        <googleJavaFormat>
                            <version>1.15.0</version>
                            <style>AOSP</style>
                        </googleJavaFormat>
                        <removeUnusedImports/>
                    </java>
                    <encoding>UTF-8</encoding>
                    <lineEndings>UNIX</lineEndings>
                </configuration>
</plugin>
@nedtwigg
Copy link
Member

nedtwigg commented May 5, 2022

There are two standard line endings in use today:

  • unix \n
  • windows \r\n

On Mac OS Classic (last actively developed 20 years ago around Y2K) there was a \r only line ending convention. To my knowledge, that mode is completely unsupported by git.

If you want to add a MAC_CLASSIC line ending, I'd be happy to merge it.

The tricky part is that this method currently assumes it is getting \r\n or \n, and then it promises to return \n only. You'll have to update it to detect files with \r but no \n, and then replace \r with \n.

/** Returns a string with exclusively unix line endings. */
public static String toUnix(String input) {
int firstNewline = input.lastIndexOf('\n');
if (firstNewline == -1) {
// fastest way to detect if a string is already unix-only
return input;
} else {
return input.replace("\r", "");
}
}

Then these methods would need to get updated too (perhaps other places too, but I think this is most of it).

/** {@code \n} on unix systems, {@code \r\n} on windows systems. */
PLATFORM_NATIVE,
/** {@code \r\n} */
WINDOWS,
/** {@code \n} */
UNIX;

/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
public Policy createPolicy() {
switch (this) {
case PLATFORM_NATIVE: return _platformNativePolicy;
case WINDOWS: return WINDOWS_POLICY;
case UNIX: return UNIX_POLICY;

private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy(WINDOWS.str());
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy(UNIX.str());

/** Returns the standard line ending for this policy. */
public String str() {
switch (this) {
case PLATFORM_NATIVE: return _platformNative;
case WINDOWS: return "\r\n";
case UNIX: return "\n";
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");

@nedtwigg nedtwigg changed the title spotless check can not checkout the file with cr(\r) lineSeparator ? add support for MAC_CLASSIC (\r only) May 5, 2022
@DemonJun
Copy link
Author

DemonJun commented Jun 20, 2022

 /** Returns a string with exclusively unix line endings. */ 
 public static String toUnix(String input) { 
 	int firstNewline = input.lastIndexOf('\n'); 
 	if (firstNewline == -1) { 
 		// fastest way to detect if a string is already unix-only 
 		return input; 
 	} else { 
 		return input.replace("\r", ""); 
 	} 
 } 

Here I don't understand, if input is endWith '\n', it must go to the else block? Is this necessary?

@DemonJun
Copy link
Author

DemonJun commented Jun 20, 2022

/** Returns a string with exclusively unix line endings. */
public static String toUnix(String input) {
      if (input.lastIndexOf(WINDOWS.str()) > -1) {
          return input.replace('\r', '');
      } else if (input.lastIndexOf(MAC_CLASSIC.str()) > -1) {
          // replace mac classic '\r' with unix line endings '\n'
          return input.replace(MAC_CLASSIC.str(), UNIX.str());
      } else {
	// fastest way to detect if a string is already unix-only
	return input;
      }
}

After adding the MAC_CLASSIC conversion, I think it should look like this.

@nedtwigg
Copy link
Member

Released in plugin-gradle 6.8.0 and plugin-maven 2.23.0, thanks to @DemonJun.

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

2 participants