Skip to content

Commit 9e1255e

Browse files
authored
Remove FormatterStep.createNeverUpToDate (#2145)
2 parents 6fe56b6 + e37a567 commit 9e1255e

File tree

13 files changed

+135
-62
lines changed

13 files changed

+135
-62
lines changed

Diff for: CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1919
* Made ShadowCopy (`npmInstallCache`) more robust by re-creating the cache dir if it goes missing ([#1984](https://github.com/diffplug/spotless/issues/1984),[2096](https://github.com/diffplug/spotless/pull/2096))
2020
* scalafmt.conf fileOverride section now works correctly ([#1854](https://github.com/diffplug/spotless/pull/1854))
2121
### Changes
22+
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
2223
* Bump default `gherkin-utils` version to latest `8.0.2` -> `9.0.0`. ([#1703](https://github.com/diffplug/spotless/pull/1703))
2324
* Bump default `google-java-format` version to latest `1.19.2` -> `1.22.0`. ([#2129](https://github.com/diffplug/spotless/pull/2129))
2425
* Bump default `jackson` version to latest `2.14.2` -> `2.17.1`. ([#1685](https://github.com/diffplug/spotless/pull/1685))
@@ -28,8 +29,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2829
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
2930
* Bump default `sortpom` version to latest `3.2.1` -> `4.0.0`. ([#2049](https://github.com/diffplug/spotless/pull/2049), [#2078](https://github.com/diffplug/spotless/pull/2078), [#2115](https://github.com/diffplug/spotless/pull/2115))
3031
* Bump default `zjsonpatch` version to latest `0.4.14` -> `0.4.16`. ([#1969](https://github.com/diffplug/spotless/pull/1969))
31-
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
3232
### Removed
33+
* **BREAKING** Remove `FormatterStep.createNeverUpToDate` methods, they are available only in `testlib`. ([#2145](https://github.com/diffplug/spotless/pull/2145))
3334
* **BREAKING** Remove `JarState.getMavenCoordinate(String prefix)`. ([#1945](https://github.com/diffplug/spotless/pull/1945))
3435
* **BREAKING** Replace `PipeStepPair` with `FenceStep`. ([#1954](https://github.com/diffplug/spotless/pull/1954))
3536
* **BREAKING** Fully removed `Rome`, use `Biome` instead. ([#2119](https://github.com/diffplug/spotless/pull/2119))

Diff for: lib/src/main/java/com/diffplug/spotless/FormatterStep.java

-30
Original file line numberDiff line numberDiff line change
@@ -172,34 +172,4 @@ static <State extends Serializable> FormatterStep create(
172172
Objects.requireNonNull(state, "state");
173173
return createLazy(name, () -> state, stateToFormatter);
174174
}
175-
176-
/**
177-
* @param name
178-
* The name of the formatter step
179-
* @param functionSupplier
180-
* A supplier which will lazily generate the function
181-
* used by the formatter step
182-
* @return A FormatterStep which will never report that it is up-to-date, because
183-
* it is not equal to the serialized representation of itself.
184-
*/
185-
static FormatterStep createNeverUpToDateLazy(
186-
String name,
187-
ThrowingEx.Supplier<FormatterFunc> functionSupplier) {
188-
return new FormatterStepImpl.NeverUpToDate(name, functionSupplier);
189-
}
190-
191-
/**
192-
* @param name
193-
* The name of the formatter step
194-
* @param function
195-
* The function used by the formatter step
196-
* @return A FormatterStep which will never report that it is up-to-date, because
197-
* it is not equal to the serialized representation of itself.
198-
*/
199-
static FormatterStep createNeverUpToDate(
200-
String name,
201-
FormatterFunc function) {
202-
Objects.requireNonNull(function, "function");
203-
return createNeverUpToDateLazy(name, () -> function);
204-
}
205175
}

Diff for: lib/src/main/java/com/diffplug/spotless/SerializedFunction.java

+18
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,22 @@ public interface SerializedFunction<T, R> extends Serializable, ThrowingEx.Funct
2222
static <T> SerializedFunction<T, T> identity() {
2323
return t -> t;
2424
}
25+
26+
static <T, R> SerializedFunction<T, R> alwaysReturns(R value) {
27+
return new AlwaysReturns(value);
28+
}
29+
30+
class AlwaysReturns<T, R> implements SerializedFunction<T, R> {
31+
private static final long serialVersionUID = 1L;
32+
private final R value;
33+
34+
AlwaysReturns(R value) {
35+
this.value = value;
36+
}
37+
38+
@Override
39+
public R apply(T t) {
40+
return value;
41+
}
42+
}
2543
}

Diff for: lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

+15-9
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.
@@ -40,6 +40,7 @@
4040
import com.diffplug.spotless.LineEnding;
4141
import com.diffplug.spotless.OnMatch;
4242
import com.diffplug.spotless.SerializableFileFilter;
43+
import com.diffplug.spotless.SerializedFunction;
4344
import com.diffplug.spotless.ThrowingEx;
4445

4546
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -119,15 +120,22 @@ public LicenseHeaderStep withSkipLinesMatching(@Nullable String skipLinesMatchin
119120
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode, skipLinesMatching);
120121
}
121122

122-
public FormatterStep build() {
123-
FormatterStep formatterStep = null;
123+
private static class SetLicenseHeaderYearsFromGitHistory implements SerializedFunction<Runtime, FormatterFunc> {
124+
private static final long serialVersionUID = 1L;
124125

126+
@Override
127+
public FormatterFunc apply(Runtime input) throws Exception {
128+
return FormatterFunc.needsFile(input::setLicenseHeaderYearsFromGitHistory);
129+
}
130+
}
131+
132+
public FormatterStep build() {
133+
FormatterStep formatterStep;
125134
if (yearMode.get() == YearMode.SET_FROM_GIT) {
126-
formatterStep = FormatterStep.createNeverUpToDateLazy(name, () -> {
135+
formatterStep = FormatterStep.createLazy(name, () -> {
127136
boolean updateYear = false; // doesn't matter
128-
Runtime runtime = new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching);
129-
return FormatterFunc.needsFile(runtime::setLicenseHeaderYearsFromGitHistory);
130-
});
137+
return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching);
138+
}, new SetLicenseHeaderYearsFromGitHistory());
131139
} else {
132140
formatterStep = FormatterStep.createLazy(name, () -> {
133141
// by default, we should update the year if the user is using ratchetFrom
@@ -146,11 +154,9 @@ public FormatterStep build() {
146154
return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching);
147155
}, step -> FormatterFunc.needsFile(step::format));
148156
}
149-
150157
if (contentPattern == null) {
151158
return formatterStep;
152159
}
153-
154160
return formatterStep.filterByContent(OnMatch.INCLUDE, contentPattern);
155161
}
156162

Diff for: plugin-gradle/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* Made ShadowCopy (`npmInstallCache`) more robust by re-creating the cache dir if it goes missing ([#1984](https://github.com/diffplug/spotless/issues/1984),[2096](https://github.com/diffplug/spotless/pull/2096))
1515
* scalafmt.conf fileOverride section now works correctly ([#1854](https://github.com/diffplug/spotless/pull/1854))
1616
### Changes
17+
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
1718
* Bump default `gherkin-utils` version to latest `8.0.2` -> `9.0.0`. ([#1703](https://github.com/diffplug/spotless/pull/1703))
1819
* Bump default `google-java-format` version to latest `1.19.2` -> `1.22.0`. ([#2129](https://github.com/diffplug/spotless/pull/2129))
1920
* Bump default `jackson` version to latest `2.14.2` -> `2.17.1`. ([#1685](https://github.com/diffplug/spotless/pull/1685))
@@ -23,7 +24,6 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2324
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
2425
* Bump default `sortpom` version to latest `3.2.1` -> `4.0.0`. ([#2049](https://github.com/diffplug/spotless/pull/2049), [#2078](https://github.com/diffplug/spotless/pull/2078), [#2115](https://github.com/diffplug/spotless/pull/2115))
2526
* Bump default `zjsonpatch` version to latest `0.4.14` -> `0.4.16`. ([#1969](https://github.com/diffplug/spotless/pull/1969))
26-
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
2727
### Removed
2828
* **BREAKING** Fully removed `Rome`, use `Biome` instead. ([#2119](https://github.com/diffplug/spotless/pull/2119))
2929

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.diffplug.spotless.LineEnding;
5454
import com.diffplug.spotless.OnMatch;
5555
import com.diffplug.spotless.Provisioner;
56+
import com.diffplug.spotless.SerializedFunction;
5657
import com.diffplug.spotless.biome.BiomeFlavor;
5758
import com.diffplug.spotless.cpp.ClangFormatStep;
5859
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
@@ -427,7 +428,21 @@ protected Integer calculateState() throws Exception {
427428
*/
428429
public void custom(String name, Closure<String> formatter) {
429430
requireNonNull(formatter, "formatter");
430-
custom(name, formatter::call);
431+
Closure<String> dehydrated = formatter.dehydrate();
432+
custom(name, new ClosureFormatterFunc(dehydrated));
433+
}
434+
435+
static class ClosureFormatterFunc implements FormatterFunc, Serializable {
436+
private final Closure<String> closure;
437+
438+
ClosureFormatterFunc(Closure<String> closure) {
439+
this.closure = closure;
440+
}
441+
442+
@Override
443+
public String apply(String unixNewlines) {
444+
return closure.call(unixNewlines);
445+
}
431446
}
432447

433448
/**
@@ -436,7 +451,7 @@ public void custom(String name, Closure<String> formatter) {
436451
*/
437452
public void custom(String name, FormatterFunc formatter) {
438453
requireNonNull(formatter, "formatter");
439-
addStep(FormatterStep.createLazy(name, () -> globalState, unusedState -> formatter));
454+
addStep(FormatterStep.createLazy(name, () -> globalState, SerializedFunction.alwaysReturns(formatter)));
440455
}
441456

442457
/** Highly efficient find-replace char sequence. */

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Arrays;
2222
import java.util.Collections;
2323
import java.util.List;
24-
import java.util.regex.Pattern;
2524

2625
import org.assertj.core.api.Assertions;
2726
import org.gradle.api.Project;
@@ -31,11 +30,11 @@
3130

3231
import com.diffplug.common.base.StringPrinter;
3332
import com.diffplug.spotless.FileSignature;
34-
import com.diffplug.spotless.FormatterStep;
3533
import com.diffplug.spotless.LineEnding;
3634
import com.diffplug.spotless.ResourceHarness;
3735
import com.diffplug.spotless.TestProvisioner;
3836
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
37+
import com.diffplug.spotless.generic.TrimTrailingWhitespaceStep;
3938

4039
class DiffMessageFormatterTest extends ResourceHarness {
4140

@@ -151,10 +150,7 @@ void customRunToFixMessage() throws Exception {
151150
@Test
152151
void whitespaceProblem() throws Exception {
153152
Bundle spotless = create(setFile("testFile").toContent("A \nB\t\nC \n"));
154-
spotless.task.setSteps(List.of(FormatterStep.createNeverUpToDate("trimTrailing", input -> {
155-
Pattern pattern = Pattern.compile("[ \t]+$", Pattern.UNIX_LINES | Pattern.MULTILINE);
156-
return pattern.matcher(input).replaceAll("");
157-
})));
153+
spotless.task.setSteps(List.of(TrimTrailingWhitespaceStep.create()));
158154
assertCheckFailure(spotless,
159155
" testFile",
160156
" @@ -1,3 +1,3 @@",

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
2626

27-
import com.diffplug.spotless.FormatterStep;
2827
import com.diffplug.spotless.LineEnding;
2928
import com.diffplug.spotless.ResourceHarness;
3029
import com.diffplug.spotless.TestProvisioner;
30+
import com.diffplug.spotless.generic.ReplaceStep;
3131

3232
class FormatTaskTest extends ResourceHarness {
3333
private SpotlessTaskImpl spotlessTask;
@@ -62,7 +62,7 @@ void testStep() throws Exception {
6262
File outputFile = new File(spotlessTask.getOutputDirectory(), "testFile");
6363
spotlessTask.setTarget(Collections.singleton(testFile));
6464

65-
spotlessTask.setSteps(List.of(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p"))));
65+
spotlessTask.setSteps(List.of(ReplaceStep.create("double-p", "pp", "p")));
6666
Tasks.execute(spotlessTask);
6767

6868
assertFile(outputFile).hasContent("aple");

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131

3232
import com.diffplug.common.base.StringPrinter;
3333
import com.diffplug.spotless.FileSignature;
34-
import com.diffplug.spotless.FormatterFunc;
3534
import com.diffplug.spotless.FormatterStep;
3635
import com.diffplug.spotless.LineEnding;
36+
import com.diffplug.spotless.NeverUpToDateStep;
3737
import com.diffplug.spotless.ResourceHarness;
38+
import com.diffplug.spotless.SerializedFunction;
3839
import com.diffplug.spotless.TestProvisioner;
3940

4041
class PaddedCellTaskTest extends ResourceHarness {
@@ -53,10 +54,10 @@ public BuildServiceParameters.None getParameters() {
5354
SpotlessCheck check;
5455
SpotlessApply apply;
5556

56-
Bundle(String name, FormatterFunc function) throws IOException {
57+
Bundle(String name, SerializedFunction<String, String> function) throws IOException {
5758
this.name = name;
5859
file = setFile("src/test." + name).toContent("CCC");
59-
FormatterStep step = FormatterStep.createNeverUpToDate(name, function);
60+
FormatterStep step = NeverUpToDateStep.create(name, function);
6061
source = createFormatTask(name, step);
6162
check = createCheckTask(name, source);
6263
apply = createApplyTask(name, source);

Diff for: plugin-maven/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
* Made ShadowCopy (`npmInstallCache`) more robust by re-creating the cache dir if it goes missing ([#1984](https://github.com/diffplug/spotless/issues/1984),[2096](https://github.com/diffplug/spotless/pull/2096))
1313
* scalafmt.conf fileOverride section now works correctly ([#1854](https://github.com/diffplug/spotless/pull/1854))
1414
### Changes
15+
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
1516
* Bump default `gherkin-utils` version to latest `8.0.2` -> `9.0.0`. ([#1703](https://github.com/diffplug/spotless/pull/1703))
1617
* Bump default `google-java-format` version to latest `1.19.2` -> `1.22.0`. ([#2129](https://github.com/diffplug/spotless/pull/2129))
1718
* Bump default `jackson` version to latest `2.14.2` -> `2.17.1`. ([#1685](https://github.com/diffplug/spotless/pull/1685))
@@ -21,7 +22,6 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2122
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
2223
* Bump default `sortpom` version to latest `3.2.1` -> `4.0.0`. ([#2049](https://github.com/diffplug/spotless/pull/2049), [#2078](https://github.com/diffplug/spotless/pull/2078), [#2115](https://github.com/diffplug/spotless/pull/2115))
2324
* Bump default `zjsonpatch` version to latest `0.4.14` -> `0.4.16`. ([#1969](https://github.com/diffplug/spotless/pull/1969))
24-
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
2525
### Removed
2626
* **BREAKING** Fully removed `Rome`, use `Biome` instead. ([#2119](https://github.com/diffplug/spotless/pull/2119))
2727

Diff for: plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java

+4-2
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.
@@ -39,6 +39,8 @@
3939
import com.diffplug.spotless.Formatter;
4040
import com.diffplug.spotless.FormatterStep;
4141
import com.diffplug.spotless.LineEnding;
42+
import com.diffplug.spotless.NeverUpToDateStep;
43+
import com.diffplug.spotless.SerializedFunction;
4244
import com.diffplug.spotless.maven.MavenIntegrationHarness;
4345

4446
class PluginFingerprintTest extends MavenIntegrationHarness {
@@ -162,7 +164,7 @@ private static Model readPom(String xml) throws Exception {
162164
}
163165

164166
private static FormatterStep formatterStep(String name) {
165-
return FormatterStep.createNeverUpToDate(name, input -> input);
167+
return NeverUpToDateStep.create(name, SerializedFunction.identity());
166168
}
167169

168170
private static Formatter formatter(FormatterStep... steps) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless;
17+
18+
import java.io.File;
19+
20+
/**
21+
* Formatter which is equal to itself, but not to any other Formatter.
22+
*/
23+
public class NeverUpToDateStep implements FormatterStep {
24+
/**
25+
* @param name
26+
* The name of the formatter step
27+
* @param function
28+
* The function used by the formatter step
29+
* @return A FormatterStep which will never report that it is up-to-date, because
30+
* it is not equal to the serialized representation of itself.
31+
*/
32+
public static FormatterStep create(
33+
String name,
34+
SerializedFunction<String, String> function) {
35+
return new NeverUpToDateStep(name, function);
36+
}
37+
38+
private static final long serialVersionUID = 1L;
39+
40+
private final String name;
41+
private final SerializedFunction<String, String> formatter; // initialized lazily
42+
43+
NeverUpToDateStep(String name, SerializedFunction<String, String> formatter) {
44+
this.name = name;
45+
this.formatter = formatter;
46+
}
47+
48+
@Override
49+
public String getName() {
50+
return name;
51+
}
52+
53+
@Override
54+
public String format(String rawUnix, File file) throws Exception {
55+
return formatter.apply(rawUnix);
56+
}
57+
58+
@Override
59+
public void close() throws Exception {
60+
if (formatter instanceof FormatterFunc.Closeable) {
61+
((FormatterFunc.Closeable) formatter).close();
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)