27
27
import javax .annotation .Nullable ;
28
28
29
29
import com .diffplug .spotless .Formatter ;
30
- import com .diffplug .spotless .FormatterFunc ;
31
30
import com .diffplug .spotless .FormatterStep ;
32
31
import com .diffplug .spotless .LineEnding ;
32
+ import com .diffplug .spotless .Lint ;
33
33
34
34
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
35
35
@@ -101,7 +101,7 @@ static class ApplyWithin extends BaseStep {
101
101
}
102
102
103
103
@ Override
104
- public String apply (Formatter formatter , String unix , File file ) throws Exception {
104
+ protected String applySubclass (Formatter formatter , String unix , File file ) {
105
105
List <String > groups = groupsZeroed ();
106
106
Matcher matcher = regex .matcher (unix );
107
107
while (matcher .find ()) {
@@ -130,15 +130,15 @@ private void storeGroups(String unix) {
130
130
}
131
131
132
132
@ Override
133
- public String apply (Formatter formatter , String unix , File file ) throws Exception {
133
+ protected String applySubclass (Formatter formatter , String unix , File file ) {
134
134
storeGroups (unix );
135
135
String formatted = formatter .compute (unix , file );
136
136
return assembleGroups (formatted );
137
137
}
138
138
}
139
139
140
140
@ SuppressFBWarnings ("SE_TRANSIENT_FIELD_NOT_RESTORED" )
141
- public static abstract class BaseStep implements Serializable , FormatterStep , FormatterFunc . Closeable . ResourceFuncNeedsFile < Formatter > {
141
+ private static abstract class BaseStep implements Serializable , FormatterStep {
142
142
final String name ;
143
143
private static final long serialVersionUID = -2301848328356559915L ;
144
144
final Pattern regex ;
@@ -198,8 +198,8 @@ protected String assembleGroups(String unix) {
198
198
return builder .toString ();
199
199
} else {
200
200
// these will be needed to generate Lints later on
201
- // int startLine = 1 + (int) builder.toString().codePoints().filter(c -> c == '\n').count();
202
- // int endLine = 1 + (int) unix.codePoints().filter(c -> c == '\n').count();
201
+ int startLine = 1 + (int ) builder .toString ().codePoints ().filter (c -> c == '\n' ).count ();
202
+ int endLine = 1 + (int ) unix .codePoints ().filter (c -> c == '\n' ).count ();
203
203
204
204
// throw an error with either the full regex, or the nicer open/close pair
205
205
Matcher openClose = Pattern .compile ("\\ \\ Q([\\ s\\ S]*?)\\ \\ E" + "\\ Q([\\ s\\ S]*?)\\ E" + "\\ \\ Q([\\ s\\ S]*?)\\ \\ E" )
@@ -210,7 +210,9 @@ protected String assembleGroups(String unix) {
210
210
} else {
211
211
pattern = regex .pattern ();
212
212
}
213
- throw new Error ("An intermediate step removed a match of " + pattern );
213
+ throw new Lint .ShortcutException (Lint .create ("fenceRemoved" ,
214
+ "An intermediate step removed a match of " + pattern ,
215
+ startLine , endLine ));
214
216
}
215
217
}
216
218
@@ -221,15 +223,21 @@ public String getName() {
221
223
222
224
private transient Formatter formatter ;
223
225
224
- @ Nullable
225
- @ Override
226
- public String format (String rawUnix , File file ) throws Exception {
226
+ private String apply (String rawUnix , File file ) throws Exception {
227
227
if (formatter == null ) {
228
228
formatter = buildFormatter ();
229
229
}
230
- return this . apply (formatter , rawUnix , file );
230
+ return applySubclass (formatter , rawUnix , file );
231
231
}
232
232
233
+ @ Nullable
234
+ @ Override
235
+ public String format (String rawUnix , File file ) throws Exception {
236
+ return apply (rawUnix , file );
237
+ }
238
+
239
+ protected abstract String applySubclass (Formatter formatter , String unix , File file ) throws Exception ;
240
+
233
241
@ Override
234
242
public boolean equals (Object o ) {
235
243
if (this == o )
0 commit comments