Skip to content

Commit

Permalink
Pipe the lints through FenceStep, preliminary.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed May 30, 2024
1 parent b57bf24 commit a94dce9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> groups = groupsZeroed();
Matcher matcher = regex.matcher(unix);
while (matcher.find()) {
Expand Down Expand Up @@ -130,15 +130,15 @@ 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);
}
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
public static abstract class BaseStep implements Serializable, FormatterStep, FormatterFunc.Closeable.ResourceFuncNeedsFile<Formatter> {
private static abstract class BaseStep implements Serializable, FormatterStep {
final String name;
private static final long serialVersionUID = -2301848328356559915L;
final Pattern regex;
Expand Down Expand Up @@ -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")
Expand All @@ -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));
}
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +82,8 @@ void multiple() {
"1 2 3"));
}

@Disabled
@ForLintRefactor
@Test
void broken() {
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
Expand Down

0 comments on commit a94dce9

Please sign in to comment.