Skip to content

Commit bab36df

Browse files
authored
add support for MAC_CLASSIC (\r only) (#1243 fixes #1196)
2 parents 1ff43ad + 0aca3e2 commit bab36df

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
1315

1416
## [2.26.2] - 2022-06-11
1517
### Fixed

lib/src/main/java/com/diffplug/spotless/LineEnding.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,10 @@ public Policy createPolicy() {
4141
PLATFORM_NATIVE,
4242
/** {@code \r\n} */
4343
WINDOWS,
44-
/** {@code \n} */
45-
UNIX;
44+
/** {@code \n} */
45+
UNIX,
46+
/** {@code \r} */
47+
MAC_CLASSIC;
4648
// @formatter:on
4749

4850
/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
@@ -75,6 +77,7 @@ public Policy createPolicy() {
7577
case PLATFORM_NATIVE: return _platformNativePolicy;
7678
case WINDOWS: return WINDOWS_POLICY;
7779
case UNIX: return UNIX_POLICY;
80+
case MAC_CLASSIC: return MAC_CLASSIC_POLICY;
7881
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");
7982
}
8083
}
@@ -96,6 +99,7 @@ public String getEndingFor(File file) {
9699

97100
private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy(WINDOWS.str());
98101
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy(UNIX.str());
102+
private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy(MAC_CLASSIC.str());
99103
private static final String _platformNative = System.getProperty("line.separator");
100104
private static final Policy _platformNativePolicy = new ConstantLineEndingPolicy(_platformNative);
101105
private static final boolean nativeIsWin = _platformNative.equals(WINDOWS.str());
@@ -117,6 +121,7 @@ public String str() {
117121
case PLATFORM_NATIVE: return _platformNative;
118122
case WINDOWS: return "\r\n";
119123
case UNIX: return "\n";
124+
case MAC_CLASSIC: return "\r";
120125
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");
121126
}
122127
}
@@ -137,12 +142,17 @@ public default boolean isUnix(File file) {
137142

138143
/** Returns a string with exclusively unix line endings. */
139144
public static String toUnix(String input) {
140-
int firstNewline = input.lastIndexOf('\n');
141-
if (firstNewline == -1) {
142-
// fastest way to detect if a string is already unix-only
145+
int lastCarriageReturn = input.lastIndexOf('\r');
146+
if (lastCarriageReturn == -1) {
143147
return input;
144148
} else {
145-
return input.replace("\r", "");
149+
if (input.lastIndexOf("\r\n") == -1) {
150+
// it is MAC_CLASSIC \r
151+
return input.replace('\r', '\n');
152+
} else {
153+
// it is WINDOWS \r\n
154+
return input.replace("\r", "");
155+
}
146156
}
147157
}
148158
}

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
68

79
## [6.7.2] - 2022-06-11
810
### Fixed

plugin-gradle/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ spotless {
866866
encoding 'Cp1252' // except java, which will be Cp1252
867867
```
868868
869-
Line endings can also be set globally or per-format using the `lineEndings` property. Spotless supports four line ending modes: `UNIX`, `WINDOWS`, `PLATFORM_NATIVE`, and `GIT_ATTRIBUTES`. The default value is `GIT_ATTRIBUTES`, and *we highly recommend that you* ***do not change*** *this value*. Git has opinions about line endings, and if Spotless and git disagree, then you're going to have a bad time.
869+
Line endings can also be set globally or per-format using the `lineEndings` property. Spotless supports four line ending modes: `UNIX`, `WINDOWS`, `MAC_CLASSIC`, `PLATFORM_NATIVE`, and `GIT_ATTRIBUTES`. The default value is `GIT_ATTRIBUTES`, and *we highly recommend that you* ***do not change*** *this value*. Git has opinions about line endings, and if Spotless and git disagree, then you're going to have a bad time.
870870
871871
You can easily set the line endings of different files using [a `.gitattributes` file](https://help.github.com/articles/dealing-with-line-endings/). Here's an example `.gitattributes` which sets all files to unix newlines: `* text eol=lf`.
872872

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
68

79
## [2.22.8] - 2022-06-11
810
### Fixed

plugin-maven/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ Spotless uses UTF-8 by default, but you can use [any encoding which Java support
10341034
</configuration>
10351035
```
10361036

1037-
Line endings can also be set globally or per-format using the `lineEndings` property. Spotless supports four line ending modes: `UNIX`, `WINDOWS`, `PLATFORM_NATIVE`, and `GIT_ATTRIBUTES`. The default value is `GIT_ATTRIBUTES`, and *we highly recommend that you* ***do not change*** *this value*. Git has opinions about line endings, and if Spotless and git disagree, then you're going to have a bad time.
1037+
Line endings can also be set globally or per-format using the `lineEndings` property. Spotless supports four line ending modes: `UNIX`, `WINDOWS`, `MAC_CLASSIC`, `PLATFORM_NATIVE`, and `GIT_ATTRIBUTES`. The default value is `GIT_ATTRIBUTES`, and *we highly recommend that you* ***do not change*** *this value*. Git has opinions about line endings, and if Spotless and git disagree, then you're going to have a bad time.
10381038

10391039
You can easily set the line endings of different files using [a `.gitattributes` file](https://help.github.com/articles/dealing-with-line-endings/). Here's an example `.gitattributes` which sets all files to unix newlines: `* text eol=lf`.
10401040

testlib/src/test/java/com/diffplug/spotless/FormatterTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,12 +22,20 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424

25+
import org.junit.jupiter.api.Assertions;
2526
import org.junit.jupiter.api.Test;
2627

2728
import com.diffplug.common.base.StandardSystemProperty;
2829
import com.diffplug.spotless.generic.EndWithNewlineStep;
2930

3031
class FormatterTest {
32+
@Test
33+
void toUnix() {
34+
Assertions.assertEquals("1\n2\n3", LineEnding.toUnix("1\n2\n3"));
35+
Assertions.assertEquals("1\n2\n3", LineEnding.toUnix("1\r2\r3"));
36+
Assertions.assertEquals("1\n2\n3", LineEnding.toUnix("1\r\n2\r\n3"));
37+
}
38+
3139
// Formatter normally needs to be closed, but no resources will be leaked in this special case
3240
@Test
3341
void equality() {

0 commit comments

Comments
 (0)