Skip to content

Commit b47e70e

Browse files
authored
Adjust existing steps to be lint-friendly (#2305)
2 parents b2cbba2 + 7986002 commit b47e70e

File tree

21 files changed

+137
-106
lines changed

21 files changed

+137
-106
lines changed

lib/src/compatDiktat1Dot2Dot5/java/com/diffplug/spotless/glue/diktat/compat/DiktatCompat1Dot2Dot5Adapter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,9 +77,7 @@ public String format(final File file, final String content, final boolean isScri
7777
false,
7878
new EditorConfigOverride(),
7979
false));
80-
81-
DiktatReporting.reportIfRequired(errors, LintError::getLine, LintError::getCol, LintError::getDetail);
82-
80+
DiktatReporting.reportIfRequired(errors, LintError::getLine, LintError::getRuleId, LintError::getDetail);
8381
return result;
8482
}
8583
}

lib/src/compatDiktat2Dot0Dot0/java/com/diffplug/spotless/glue/diktat/compat/DiktatCompat2Dot0Dot0Adapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public DiktatCompat2Dot0Dot0Adapter(@Nullable File configFile) {
4949
public String format(File file, String content, boolean isScript) {
5050
errors.clear();
5151
String result = processor.fix(content, file.toPath(), formatterCallback);
52-
DiktatReporting.reportIfRequired(errors, DiktatError::getLine, DiktatError::getCol, DiktatError::getDetail);
52+
DiktatReporting.reportIfRequired(errors, DiktatError::getLine, DiktatError::getRuleId, DiktatError::getDetail);
5353
return result;
5454
}
5555

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,28 +15,45 @@
1515
*/
1616
package com.diffplug.spotless.glue.diktat.compat;
1717

18+
import java.io.Serializable;
19+
import java.util.ArrayList;
1820
import java.util.List;
1921
import java.util.function.Function;
2022
import java.util.function.ToIntFunction;
2123

22-
interface DiktatReporting {
24+
public interface DiktatReporting {
25+
class Lint implements Serializable {
26+
private static final long serialVersionUID = 1L;
27+
public final int line;
28+
public final String ruleId;
29+
public final String detail;
30+
31+
Lint(int line, String ruleId, String detail) {
32+
this.line = line;
33+
this.ruleId = ruleId;
34+
this.detail = detail;
35+
}
36+
}
37+
38+
class LintException extends RuntimeException {
39+
public final List<Lint> lints;
40+
41+
LintException(List<Lint> lints) {
42+
this.lints = lints;
43+
}
44+
}
45+
2346
static <T> void reportIfRequired(
2447
List<T> errors,
2548
ToIntFunction<T> lineGetter,
26-
ToIntFunction<T> columnGetter,
49+
Function<T, String> codeGetter,
2750
Function<T, String> detailGetter) {
2851
if (!errors.isEmpty()) {
29-
StringBuilder error = new StringBuilder();
30-
error.append("There are ").append(errors.size()).append(" unfixed errors:");
52+
var lints = new ArrayList<Lint>();
3153
for (T er : errors) {
32-
error.append(System.lineSeparator())
33-
.append("Error on line: ").append(lineGetter.applyAsInt(er))
34-
.append(", column: ").append(columnGetter.applyAsInt(er))
35-
.append(" cannot be fixed automatically")
36-
.append(System.lineSeparator())
37-
.append(detailGetter.apply(er));
54+
lints.add(new Lint(lineGetter.applyAsInt(er), codeGetter.apply(er), detailGetter.apply(er)));
3855
}
39-
throw new AssertionError(error);
56+
throw new LintException(lints);
4057
}
4158
}
4259
}

lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatReporting.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,25 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18-
final class KtLintCompatReporting {
18+
public final class KtLintCompatReporting {
1919

2020
private KtLintCompatReporting() {}
2121

2222
static void report(int line, int column, String ruleId, String detail) {
23-
throw new AssertionError("Error on line: " + line + ", column: " + column + "\nrule: " + ruleId + "\n" + detail);
23+
throw new KtlintSpotlessException(line, ruleId, detail);
24+
}
25+
26+
public static class KtlintSpotlessException extends RuntimeException {
27+
private static final long serialVersionUID = 1L;
28+
29+
public final int line;
30+
public final String ruleId;
31+
public final String detail;
32+
33+
KtlintSpotlessException(int line, String ruleId, String detail) {
34+
this.line = line;
35+
this.ruleId = ruleId;
36+
this.detail = detail;
37+
}
2438
}
2539
}

lib/src/diktat/java/com/diffplug/spotless/glue/diktat/DiktatFormatterFunc.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,14 @@
1616
package com.diffplug.spotless.glue.diktat;
1717

1818
import java.io.File;
19+
import java.util.stream.Collectors;
1920

2021
import com.diffplug.spotless.FormatterFunc;
22+
import com.diffplug.spotless.Lint;
2123
import com.diffplug.spotless.glue.diktat.compat.DiktatCompat1Dot2Dot5Adapter;
2224
import com.diffplug.spotless.glue.diktat.compat.DiktatCompat2Dot0Dot0Adapter;
2325
import com.diffplug.spotless.glue.diktat.compat.DiktatCompatAdapter;
26+
import com.diffplug.spotless.glue.diktat.compat.DiktatReporting;
2427

2528
public class DiktatFormatterFunc implements FormatterFunc.NeedsFile {
2629
private final DiktatCompatAdapter adapter;
@@ -40,6 +43,10 @@ public DiktatFormatterFunc(
4043

4144
@Override
4245
public String applyWithFile(String unix, File file) {
43-
return adapter.format(file, unix, isScript);
46+
try {
47+
return adapter.format(file, unix, isScript);
48+
} catch (DiktatReporting.LintException e) {
49+
throw Lint.shortcut(e.lints.stream().map(lint -> Lint.atLine(lint.line, lint.ruleId, lint.detail)).collect(Collectors.toList()));
50+
}
4451
}
4552
}

lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232

3333
public class GsonFormatterFunc implements FormatterFunc {
3434

35-
private static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to format JSON";
35+
private static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to parse JSON";
3636

3737
private final Gson gson;
3838
private final GsonConfig gsonConfig;
@@ -56,7 +56,7 @@ public String apply(String inputString) {
5656
} else {
5757
JsonElement jsonElement = gson.fromJson(inputString, JsonElement.class);
5858
if (jsonElement == null) {
59-
throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE);
59+
throw new IllegalArgumentException(FAILED_TO_PARSE_ERROR_MESSAGE);
6060
}
6161
if (gsonConfig.isSortByKeys()) {
6262
jsonElement = sortByKeys(jsonElement);

lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.diffplug.spotless.FileSignature;
2323
import com.diffplug.spotless.FormatterFunc;
24+
import com.diffplug.spotless.Lint;
2425
import com.diffplug.spotless.glue.ktlint.compat.*;
2526

2627
public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
@@ -65,10 +66,14 @@ public String applyWithFile(String unix, File file) throws NoSuchFieldException,
6566
if (editorConfigPath != null) {
6667
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
6768
}
68-
return adapter.format(
69-
unix,
70-
file.toPath(),
71-
absoluteEditorConfigPath,
72-
editorConfigOverrideMap);
69+
try {
70+
return adapter.format(
71+
unix,
72+
file.toPath(),
73+
absoluteEditorConfigPath,
74+
editorConfigOverrideMap);
75+
} catch (KtLintCompatReporting.KtlintSpotlessException e) {
76+
throw Lint.atLine(e.line, e.ruleId, e.detail).shortcut();
77+
}
7378
}
7479
}

lib/src/main/java/com/diffplug/spotless/Jvm.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,8 @@ public static int version() {
6161
* @param <V> Version type of formatter
6262
*/
6363
public static class Support<V> {
64+
static final String LINT_CODE = "jvm-version";
65+
6466
private final String fmtName;
6567
private final Comparator<? super V> fmtVersionComparator;
6668
private final NavigableMap<Integer, V> jvm2fmtMaxVersion;
@@ -139,7 +141,7 @@ public void assertFormatterSupported(V formatterVersion) {
139141
Objects.requireNonNull(formatterVersion);
140142
String error = buildUnsupportedFormatterMessage(formatterVersion);
141143
if (!error.isEmpty()) {
142-
throw new IllegalArgumentException(error);
144+
throw Lint.atUndefinedLine(LINT_CODE, error).shortcut();
143145
}
144146
}
145147

lib/src/main/java/com/diffplug/spotless/LintState.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ public Map<FormatterStep, List<Lint>> getLints(Formatter formatter) {
6262
return result;
6363
}
6464

65-
public String asString(File file, Formatter formatter) {
65+
public String asStringDetailed(File file, Formatter formatter) {
66+
return asString(file, formatter, false);
67+
}
68+
69+
public String asStringOneLine(File file, Formatter formatter) {
70+
return asString(file, formatter, true);
71+
}
72+
73+
private String asString(File file, Formatter formatter, boolean oneLine) {
6674
if (!isHasLints()) {
6775
return "(none)";
6876
} else {
@@ -84,7 +92,16 @@ public String asString(File file, Formatter formatter) {
8492
}
8593
result.append(" ");
8694
result.append(step.getName()).append("(").append(lint.getRuleId()).append(") ");
87-
result.append(lint.getDetail());
95+
96+
int firstNewline = lint.getDetail().indexOf('\n');
97+
if (firstNewline == -1) {
98+
result.append(lint.getDetail());
99+
} else if (oneLine) {
100+
result.append(lint.getDetail(), 0, firstNewline);
101+
result.append(" (...)");
102+
} else {
103+
result.append(lint.getDetail());
104+
}
88105
result.append("\n");
89106
}
90107
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ protected String assembleGroups(String unix) {
210210
} else {
211211
pattern = regex.pattern();
212212
}
213-
throw new Lint.ShortcutException(Lint.create("fenceRemoved",
214-
"An intermediate step removed a match of " + pattern,
215-
startLine, endLine));
213+
throw Lint.atLineRange(startLine, endLine, "fenceRemoved",
214+
"An intermediate step removed a match of " + pattern).shortcut();
216215
}
217216
}
218217

0 commit comments

Comments
 (0)