1717package io .spring .javaformat .formatter ;
1818
1919import java .util .Map ;
20- import java .util .regex .Matcher ;
21- import java .util .regex .Pattern ;
2220
2321import org .eclipse .jface .text .IRegion ;
22+ import org .eclipse .jface .text .TextUtilities ;
2423import org .eclipse .text .edits .TextEdit ;
2524
2625import 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