Skip to content

Commit a94dce9

Browse files
committed
Pipe the lints through FenceStep, preliminary.
1 parent b57bf24 commit a94dce9

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import javax.annotation.Nullable;
2828

2929
import com.diffplug.spotless.Formatter;
30-
import com.diffplug.spotless.FormatterFunc;
3130
import com.diffplug.spotless.FormatterStep;
3231
import com.diffplug.spotless.LineEnding;
32+
import com.diffplug.spotless.Lint;
3333

3434
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3535

@@ -101,7 +101,7 @@ static class ApplyWithin extends BaseStep {
101101
}
102102

103103
@Override
104-
public String apply(Formatter formatter, String unix, File file) throws Exception {
104+
protected String applySubclass(Formatter formatter, String unix, File file) {
105105
List<String> groups = groupsZeroed();
106106
Matcher matcher = regex.matcher(unix);
107107
while (matcher.find()) {
@@ -130,15 +130,15 @@ private void storeGroups(String unix) {
130130
}
131131

132132
@Override
133-
public String apply(Formatter formatter, String unix, File file) throws Exception {
133+
protected String applySubclass(Formatter formatter, String unix, File file) {
134134
storeGroups(unix);
135135
String formatted = formatter.compute(unix, file);
136136
return assembleGroups(formatted);
137137
}
138138
}
139139

140140
@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 {
142142
final String name;
143143
private static final long serialVersionUID = -2301848328356559915L;
144144
final Pattern regex;
@@ -198,8 +198,8 @@ protected String assembleGroups(String unix) {
198198
return builder.toString();
199199
} else {
200200
// 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();
203203

204204
// throw an error with either the full regex, or the nicer open/close pair
205205
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) {
210210
} else {
211211
pattern = regex.pattern();
212212
}
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));
214216
}
215217
}
216218

@@ -221,15 +223,21 @@ public String getName() {
221223

222224
private transient Formatter formatter;
223225

224-
@Nullable
225-
@Override
226-
public String format(String rawUnix, File file) throws Exception {
226+
private String apply(String rawUnix, File file) throws Exception {
227227
if (formatter == null) {
228228
formatter = buildFormatter();
229229
}
230-
return this.apply(formatter, rawUnix, file);
230+
return applySubclass(formatter, rawUnix, file);
231231
}
232232

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+
233241
@Override
234242
public boolean equals(Object o) {
235243
if (this == o)

testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import java.io.File;
1919
import java.util.Arrays;
2020

21+
import org.junit.jupiter.api.Disabled;
2122
import org.junit.jupiter.api.Test;
2223

2324
import com.diffplug.common.base.StringPrinter;
2425
import com.diffplug.spotless.FormatterStep;
2526
import com.diffplug.spotless.ResourceHarness;
2627
import com.diffplug.spotless.StepHarness;
28+
import com.diffplug.spotless.tag.ForLintRefactor;
2729

2830
class FenceStepTest extends ResourceHarness {
2931
@Test
@@ -80,6 +82,8 @@ void multiple() {
8082
"1 2 3"));
8183
}
8284

85+
@Disabled
86+
@ForLintRefactor
8387
@Test
8488
void broken() {
8589
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")

0 commit comments

Comments
 (0)