17
17
package io .spring .javaformat .formatter ;
18
18
19
19
import java .util .Map ;
20
- import java .util .regex .Matcher ;
21
- import java .util .regex .Pattern ;
22
20
23
21
import org .eclipse .jface .text .IRegion ;
22
+ import org .eclipse .jface .text .TextUtilities ;
24
23
import org .eclipse .text .edits .TextEdit ;
25
24
26
25
import io .spring .javaformat .config .JavaBaseline ;
@@ -58,11 +57,6 @@ public class Formatter {
58
57
*/
59
58
private static final int DEFAULT_INDENTATION_LEVEL = 0 ;
60
59
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
-
66
60
/**
67
61
* The default line separator.
68
62
*/
@@ -131,7 +125,7 @@ public TextEdit format(String source, int offset, int length, String lineSeparat
131
125
public TextEdit format (int kind , String source , int offset , int length , int indentationLevel ,
132
126
String lineSeparator ) {
133
127
if (lineSeparator == null ) {
134
- lineSeparator = detectLineSeparator (source );
128
+ lineSeparator = TextUtilities . determineLineDelimiter (source , DEFAULT_LINE_SEPARATOR );
135
129
}
136
130
return this .delegate .format (kind , source , offset , length , indentationLevel , lineSeparator );
137
131
}
@@ -159,7 +153,7 @@ public TextEdit format(String source, IRegion[] regions, String lineSeparator) {
159
153
160
154
public TextEdit format (int kind , String source , IRegion [] regions , int indentationLevel , String lineSeparator ) {
161
155
if (lineSeparator == null ) {
162
- lineSeparator = detectLineSeparator (source );
156
+ lineSeparator = TextUtilities . determineLineDelimiter (source , DEFAULT_LINE_SEPARATOR );
163
157
}
164
158
return this .delegate .format (kind , source , regions , indentationLevel , lineSeparator );
165
159
}
@@ -172,17 +166,4 @@ public void setOptions(Map<String, String> options) {
172
166
this .delegate .setOptions (options );
173
167
}
174
168
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
- }
188
169
}
0 commit comments