Skip to content

Commit 34c5fe7

Browse files
author
范俊翔
committed
add support for MAC_CLASSIC (\r only)
1 parent 24a32c2 commit 34c5fe7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,14 @@ 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) {
145+
if (input.lastIndexOf(WINDOWS.str()) > -1) {
146+
return input.replace("\r", "");
147+
} else if (input.lastIndexOf(MAC_CLASSIC.str()) > -1) {
148+
// replace mac classic '\r' with unix line endings '\n'
149+
return input.replace(MAC_CLASSIC.str(), UNIX.str());
150+
} else {
142151
// fastest way to detect if a string is already unix-only
143152
return input;
144-
} else {
145-
return input.replace("\r", "");
146153
}
147154
}
148155
}

0 commit comments

Comments
 (0)