Skip to content

Commit 8eaadf2

Browse files
committed
Use eclipse util method to detect line separator
1 parent e406e36 commit 8eaadf2

File tree

3 files changed

+5
-22
lines changed
  • spring-javaformat
    • spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter
    • spring-javaformat-formatter-eclipse-jdk11
    • spring-javaformat-formatter-eclipse-jdk8

3 files changed

+5
-22
lines changed

Diff for: spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<option>-keep class org.eclipse.jdt.core.formatter.** {*;}</option>
174174
<option>-keep class org.eclipse.jdt.internal.formatter.** {*;}</option>
175175
<option>-keep class org.eclipse.jface.text.Document {*;}</option>
176+
<option>-keep class org.eclipse.jface.text.TextUtilities {*;}</option>
176177
<option>-keep class
177178
org.eclipse.jdt.internal.compiler.parser.TerminalTokens {*;}</option>
178179
<option>-keep class org.eclipse.jdt.core.dom.TagElement {*;}</option>

Diff for: spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<option>-keep class org.eclipse.jdt.core.formatter.** {*;}</option>
174174
<option>-keep class org.eclipse.jdt.internal.formatter.** {*;}</option>
175175
<option>-keep class org.eclipse.jface.text.Document {*;}</option>
176+
<option>-keep class org.eclipse.jface.text.TextUtilities {*;}</option>
176177
<option>-keep class
177178
org.eclipse.jdt.internal.compiler.parser.TerminalTokens {*;}</option>
178179
<option>-keep class org.eclipse.jdt.core.dom.TagElement {*;}</option>

Diff for: spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java

+3-22
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package io.spring.javaformat.formatter;
1818

1919
import java.util.Map;
20-
import java.util.regex.Matcher;
21-
import java.util.regex.Pattern;
2220

2321
import org.eclipse.jface.text.IRegion;
22+
import org.eclipse.jface.text.TextUtilities;
2423
import org.eclipse.text.edits.TextEdit;
2524

2625
import io.spring.javaformat.config.JavaBaseline;
@@ -58,11 +57,6 @@ public class Formatter {
5857
*/
5958
private static final int DEFAULT_INDENTATION_LEVEL = 0;
6059

61-
/**
62-
* Pattern that matches all line separators into named-capturing group "sep".
63-
*/
64-
private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile("(?<sep>(\r\n|\r|\n))");
65-
6660
/**
6761
* The default line separator.
6862
*/
@@ -131,7 +125,7 @@ public TextEdit format(String source, int offset, int length, String lineSeparat
131125
public TextEdit format(int kind, String source, int offset, int length, int indentationLevel,
132126
String lineSeparator) {
133127
if (lineSeparator == null) {
134-
lineSeparator = detectLineSeparator(source);
128+
lineSeparator = TextUtilities.determineLineDelimiter(source, DEFAULT_LINE_SEPARATOR);
135129
}
136130
return this.delegate.format(kind, source, offset, length, indentationLevel, lineSeparator);
137131
}
@@ -159,7 +153,7 @@ public TextEdit format(String source, IRegion[] regions, String lineSeparator) {
159153

160154
public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) {
161155
if (lineSeparator == null) {
162-
lineSeparator = detectLineSeparator(source);
156+
lineSeparator = TextUtilities.determineLineDelimiter(source, DEFAULT_LINE_SEPARATOR);
163157
}
164158
return this.delegate.format(kind, source, regions, indentationLevel, lineSeparator);
165159
}
@@ -172,17 +166,4 @@ public void setOptions(Map<String, String> options) {
172166
this.delegate.setOptions(options);
173167
}
174168

175-
private String detectLineSeparator(String contents) {
176-
Matcher matcher = LINE_SEPARATOR_PATTERN.matcher(contents);
177-
if (!matcher.find()) {
178-
return DEFAULT_LINE_SEPARATOR;
179-
}
180-
String firstMatch = matcher.group("sep");
181-
while (matcher.find()) {
182-
if (!matcher.group("sep").equals(firstMatch)) {
183-
return DEFAULT_LINE_SEPARATOR;
184-
}
185-
}
186-
return firstMatch;
187-
}
188169
}

0 commit comments

Comments
 (0)