From a94dce9f9b0e62d832f3d033603844a8884fa906 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 30 May 2024 16:01:47 -0700 Subject: [PATCH] Pipe the lints through `FenceStep`, preliminary. --- .../diffplug/spotless/generic/FenceStep.java | 30 ++++++++++++------- .../spotless/generic/FenceStepTest.java | 4 +++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java b/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java index 9ce34675f8..3cb0e3f047 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java @@ -27,9 +27,9 @@ import javax.annotation.Nullable; import com.diffplug.spotless.Formatter; -import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.LineEnding; +import com.diffplug.spotless.Lint; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -101,7 +101,7 @@ static class ApplyWithin extends BaseStep { } @Override - public String apply(Formatter formatter, String unix, File file) throws Exception { + protected String applySubclass(Formatter formatter, String unix, File file) { List groups = groupsZeroed(); Matcher matcher = regex.matcher(unix); while (matcher.find()) { @@ -130,7 +130,7 @@ private void storeGroups(String unix) { } @Override - public String apply(Formatter formatter, String unix, File file) throws Exception { + protected String applySubclass(Formatter formatter, String unix, File file) { storeGroups(unix); String formatted = formatter.compute(unix, file); return assembleGroups(formatted); @@ -138,7 +138,7 @@ public String apply(Formatter formatter, String unix, File file) throws Exceptio } @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") - public static abstract class BaseStep implements Serializable, FormatterStep, FormatterFunc.Closeable.ResourceFuncNeedsFile { + private static abstract class BaseStep implements Serializable, FormatterStep { final String name; private static final long serialVersionUID = -2301848328356559915L; final Pattern regex; @@ -198,8 +198,8 @@ protected String assembleGroups(String unix) { return builder.toString(); } else { // these will be needed to generate Lints later on - // int startLine = 1 + (int) builder.toString().codePoints().filter(c -> c == '\n').count(); - // int endLine = 1 + (int) unix.codePoints().filter(c -> c == '\n').count(); + int startLine = 1 + (int) builder.toString().codePoints().filter(c -> c == '\n').count(); + int endLine = 1 + (int) unix.codePoints().filter(c -> c == '\n').count(); // throw an error with either the full regex, or the nicer open/close pair 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) { } else { pattern = regex.pattern(); } - throw new Error("An intermediate step removed a match of " + pattern); + throw new Lint.ShortcutException(Lint.create("fenceRemoved", + "An intermediate step removed a match of " + pattern, + startLine, endLine)); } } @@ -221,15 +223,21 @@ public String getName() { private transient Formatter formatter; - @Nullable - @Override - public String format(String rawUnix, File file) throws Exception { + private String apply(String rawUnix, File file) throws Exception { if (formatter == null) { formatter = buildFormatter(); } - return this.apply(formatter, rawUnix, file); + return applySubclass(formatter, rawUnix, file); } + @Nullable + @Override + public String format(String rawUnix, File file) throws Exception { + return apply(rawUnix, file); + } + + protected abstract String applySubclass(Formatter formatter, String unix, File file) throws Exception; + @Override public boolean equals(Object o) { if (this == o) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java index 697a199fd2..ea9755a804 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java @@ -18,12 +18,14 @@ import java.io.File; import java.util.Arrays; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import com.diffplug.common.base.StringPrinter; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.StepHarness; +import com.diffplug.spotless.tag.ForLintRefactor; class FenceStepTest extends ResourceHarness { @Test @@ -80,6 +82,8 @@ void multiple() { "1 2 3")); } + @Disabled + @ForLintRefactor @Test void broken() { FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")