Skip to content

Commit a83807f

Browse files
committed
Make ReplaceRegexStep a bit more idiomatically a linter.
1 parent a6b7d52 commit a83807f

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.generic;
1717

18-
import static com.diffplug.spotless.Lint.atLine;
19-
2018
import java.io.File;
2119
import java.io.Serializable;
2220
import java.util.ArrayList;
@@ -41,13 +39,13 @@ public static FormatterStep create(String name, String regex, String replacement
4139
State::toFormatter);
4240
}
4341

44-
public static FormatterStep lint(String name, String regex, String error) {
42+
public static FormatterStep lint(String name, String regex, String lintDetail) {
4543
Objects.requireNonNull(name, "name");
4644
Objects.requireNonNull(regex, "regex");
47-
Objects.requireNonNull(error, "error");
45+
Objects.requireNonNull(lintDetail, "lintDetail");
4846
return FormatterStep.createLazy(name,
49-
() -> new State(Pattern.compile(regex, Pattern.UNIX_LINES | Pattern.MULTILINE), error),
50-
State::toLinter);
47+
() -> new LintState(Pattern.compile(regex, Pattern.UNIX_LINES | Pattern.MULTILINE), name, lintDetail),
48+
LintState::toLinter);
5149
}
5250

5351
private static final class State implements Serializable {
@@ -64,6 +62,20 @@ private static final class State implements Serializable {
6462
FormatterFunc toFormatter() {
6563
return raw -> regex.matcher(raw).replaceAll(replacement);
6664
}
65+
}
66+
67+
private static final class LintState implements Serializable {
68+
private static final long serialVersionUID = 1L;
69+
70+
private final Pattern regex;
71+
private final String ruleId;
72+
private final String lintDetail;
73+
74+
LintState(Pattern regex, String ruleId, String lintDetail) {
75+
this.regex = regex;
76+
this.ruleId = ruleId;
77+
this.lintDetail = lintDetail;
78+
}
6779

6880
FormatterFunc toLinter() {
6981
return new FormatterFunc() {
@@ -78,7 +90,7 @@ public List<Lint> lint(String raw, File file) {
7890
var matcher = regex.matcher(raw);
7991
while (matcher.find()) {
8092
int line = 1 + (int) raw.codePoints().limit(matcher.start()).filter(c -> c == '\n').count();
81-
lints.add(atLine(line, matcher.group(0), replacement));
93+
lints.add(Lint.atLine(line, ruleId, lintDetail));
8294
}
8395
return lints;
8496
}

0 commit comments

Comments
 (0)