Skip to content

Remove FormatterStep.createNeverUpToDate #2145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 29, 2024
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* 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))
* scalafmt.conf fileOverride section now works correctly ([#1854](https://github.com/diffplug/spotless/pull/1854))
### Changes
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
* Bump default `gherkin-utils` version to latest `8.0.2` -> `9.0.0`. ([#1703](https://github.com/diffplug/spotless/pull/1703))
* Bump default `google-java-format` version to latest `1.19.2` -> `1.22.0`. ([#2129](https://github.com/diffplug/spotless/pull/2129))
* Bump default `jackson` version to latest `2.14.2` -> `2.17.1`. ([#1685](https://github.com/diffplug/spotless/pull/1685))
Expand All @@ -28,8 +29,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
* 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))
* Bump default `zjsonpatch` version to latest `0.4.14` -> `0.4.16`. ([#1969](https://github.com/diffplug/spotless/pull/1969))
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
### Removed
* **BREAKING** Remove `FormatterStep.createNeverUpToDate` methods, they are available only in `testlib`. ([#2145](https://github.com/diffplug/spotless/pull/2145))
* **BREAKING** Remove `JarState.getMavenCoordinate(String prefix)`. ([#1945](https://github.com/diffplug/spotless/pull/1945))
* **BREAKING** Replace `PipeStepPair` with `FenceStep`. ([#1954](https://github.com/diffplug/spotless/pull/1954))
* **BREAKING** Fully removed `Rome`, use `Biome` instead. ([#2119](https://github.com/diffplug/spotless/pull/2119))
Expand Down
30 changes: 0 additions & 30 deletions lib/src/main/java/com/diffplug/spotless/FormatterStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,4 @@ static <State extends Serializable> FormatterStep create(
Objects.requireNonNull(state, "state");
return createLazy(name, () -> state, stateToFormatter);
}

/**
* @param name
* The name of the formatter step
* @param functionSupplier
* A supplier which will lazily generate the function
* used by the formatter step
* @return A FormatterStep which will never report that it is up-to-date, because
* it is not equal to the serialized representation of itself.
*/
static FormatterStep createNeverUpToDateLazy(
String name,
ThrowingEx.Supplier<FormatterFunc> functionSupplier) {
return new FormatterStepImpl.NeverUpToDate(name, functionSupplier);
}

/**
* @param name
* The name of the formatter step
* @param function
* The function used by the formatter step
* @return A FormatterStep which will never report that it is up-to-date, because
* it is not equal to the serialized representation of itself.
*/
static FormatterStep createNeverUpToDate(
String name,
FormatterFunc function) {
Objects.requireNonNull(function, "function");
return createNeverUpToDateLazy(name, () -> function);
}
}
18 changes: 18 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/SerializedFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ public interface SerializedFunction<T, R> extends Serializable, ThrowingEx.Funct
static <T> SerializedFunction<T, T> identity() {
return t -> t;
}

static <T, R> SerializedFunction<T, R> alwaysReturns(R value) {
return new AlwaysReturns(value);
}

class AlwaysReturns<T, R> implements SerializedFunction<T, R> {
private static final long serialVersionUID = 1L;
private final R value;

AlwaysReturns(R value) {
this.value = value;
}

@Override
public R apply(T t) {
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,7 @@
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.OnMatch;
import com.diffplug.spotless.SerializableFileFilter;
import com.diffplug.spotless.SerializedFunction;
import com.diffplug.spotless.ThrowingEx;

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

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

@Override
public FormatterFunc apply(Runtime input) throws Exception {
return FormatterFunc.needsFile(input::setLicenseHeaderYearsFromGitHistory);
}
}

public FormatterStep build() {
FormatterStep formatterStep;
if (yearMode.get() == YearMode.SET_FROM_GIT) {
formatterStep = FormatterStep.createNeverUpToDateLazy(name, () -> {
formatterStep = FormatterStep.createLazy(name, () -> {
boolean updateYear = false; // doesn't matter
Runtime runtime = new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching);
return FormatterFunc.needsFile(runtime::setLicenseHeaderYearsFromGitHistory);
});
return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching);
}, new SetLicenseHeaderYearsFromGitHistory());
} else {
formatterStep = FormatterStep.createLazy(name, () -> {
// by default, we should update the year if the user is using ratchetFrom
Expand All @@ -146,11 +154,9 @@ public FormatterStep build() {
return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching);
}, step -> FormatterFunc.needsFile(step::format));
}

if (contentPattern == null) {
return formatterStep;
}

return formatterStep.filterByContent(OnMatch.INCLUDE, contentPattern);
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* 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))
* scalafmt.conf fileOverride section now works correctly ([#1854](https://github.com/diffplug/spotless/pull/1854))
### Changes
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
* Bump default `gherkin-utils` version to latest `8.0.2` -> `9.0.0`. ([#1703](https://github.com/diffplug/spotless/pull/1703))
* Bump default `google-java-format` version to latest `1.19.2` -> `1.22.0`. ([#2129](https://github.com/diffplug/spotless/pull/2129))
* Bump default `jackson` version to latest `2.14.2` -> `2.17.1`. ([#1685](https://github.com/diffplug/spotless/pull/1685))
Expand All @@ -23,7 +24,6 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
* 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))
* Bump default `zjsonpatch` version to latest `0.4.14` -> `0.4.16`. ([#1969](https://github.com/diffplug/spotless/pull/1969))
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
### Removed
* **BREAKING** Fully removed `Rome`, use `Biome` instead. ([#2119](https://github.com/diffplug/spotless/pull/2119))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.OnMatch;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.SerializedFunction;
import com.diffplug.spotless.biome.BiomeFlavor;
import com.diffplug.spotless.cpp.ClangFormatStep;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
Expand Down Expand Up @@ -427,7 +428,21 @@ protected Integer calculateState() throws Exception {
*/
public void custom(String name, Closure<String> formatter) {
requireNonNull(formatter, "formatter");
custom(name, formatter::call);
Closure<String> dehydrated = formatter.dehydrate();
custom(name, new ClosureFormatterFunc(dehydrated));
}

static class ClosureFormatterFunc implements FormatterFunc, Serializable {
private final Closure<String> closure;

ClosureFormatterFunc(Closure<String> closure) {
this.closure = closure;
}

@Override
public String apply(String unixNewlines) {
return closure.call(unixNewlines);
}
}

/**
Expand All @@ -436,7 +451,7 @@ public void custom(String name, Closure<String> formatter) {
*/
public void custom(String name, FormatterFunc formatter) {
requireNonNull(formatter, "formatter");
addStep(FormatterStep.createLazy(name, () -> globalState, unusedState -> formatter));
addStep(FormatterStep.createLazy(name, () -> globalState, SerializedFunction.alwaysReturns(formatter)));
}

/** Highly efficient find-replace char sequence. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.assertj.core.api.Assertions;
import org.gradle.api.Project;
Expand All @@ -31,11 +30,11 @@

import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
import com.diffplug.spotless.generic.TrimTrailingWhitespaceStep;

class DiffMessageFormatterTest extends ResourceHarness {

Expand Down Expand Up @@ -151,10 +150,7 @@ void customRunToFixMessage() throws Exception {
@Test
void whitespaceProblem() throws Exception {
Bundle spotless = create(setFile("testFile").toContent("A \nB\t\nC \n"));
spotless.task.setSteps(List.of(FormatterStep.createNeverUpToDate("trimTrailing", input -> {
Pattern pattern = Pattern.compile("[ \t]+$", Pattern.UNIX_LINES | Pattern.MULTILINE);
return pattern.matcher(input).replaceAll("");
})));
spotless.task.setSteps(List.of(TrimTrailingWhitespaceStep.create()));
assertCheckFailure(spotless,
" testFile",
" @@ -1,3 +1,3 @@",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.generic.ReplaceStep;

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

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

assertFile(outputFile).hasContent("aple");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@

import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.NeverUpToDateStep;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializedFunction;
import com.diffplug.spotless.TestProvisioner;

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

Bundle(String name, FormatterFunc function) throws IOException {
Bundle(String name, SerializedFunction<String, String> function) throws IOException {
this.name = name;
file = setFile("src/test." + name).toContent("CCC");
FormatterStep step = FormatterStep.createNeverUpToDate(name, function);
FormatterStep step = NeverUpToDateStep.create(name, function);
source = createFormatTask(name, step);
check = createCheckTask(name, source);
apply = createApplyTask(name, source);
Expand Down
2 changes: 1 addition & 1 deletion plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* 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))
* scalafmt.conf fileOverride section now works correctly ([#1854](https://github.com/diffplug/spotless/pull/1854))
### Changes
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
* Bump default `gherkin-utils` version to latest `8.0.2` -> `9.0.0`. ([#1703](https://github.com/diffplug/spotless/pull/1703))
* Bump default `google-java-format` version to latest `1.19.2` -> `1.22.0`. ([#2129](https://github.com/diffplug/spotless/pull/2129))
* Bump default `jackson` version to latest `2.14.2` -> `2.17.1`. ([#1685](https://github.com/diffplug/spotless/pull/1685))
Expand All @@ -21,7 +22,6 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
* 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))
* Bump default `zjsonpatch` version to latest `0.4.14` -> `0.4.16`. ([#1969](https://github.com/diffplug/spotless/pull/1969))
* Bump default `cleanthat` version to latest `2.16` -> `2.20`. ([#1725](https://github.com/diffplug/spotless/pull/1725))
### Removed
* **BREAKING** Fully removed `Rome`, use `Biome` instead. ([#2119](https://github.com/diffplug/spotless/pull/2119))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 DiffPlug
* Copyright 2021-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,6 +39,8 @@
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.NeverUpToDateStep;
import com.diffplug.spotless.SerializedFunction;
import com.diffplug.spotless.maven.MavenIntegrationHarness;

class PluginFingerprintTest extends MavenIntegrationHarness {
Expand Down Expand Up @@ -162,7 +164,7 @@ private static Model readPom(String xml) throws Exception {
}

private static FormatterStep formatterStep(String name) {
return FormatterStep.createNeverUpToDate(name, input -> input);
return NeverUpToDateStep.create(name, SerializedFunction.identity());
}

private static Formatter formatter(FormatterStep... steps) {
Expand Down
64 changes: 64 additions & 0 deletions testlib/src/main/java/com/diffplug/spotless/NeverUpToDateStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless;

import java.io.File;

/**
* Formatter which is equal to itself, but not to any other Formatter.
*/
public class NeverUpToDateStep implements FormatterStep {
/**
* @param name
* The name of the formatter step
* @param function
* The function used by the formatter step
* @return A FormatterStep which will never report that it is up-to-date, because
* it is not equal to the serialized representation of itself.
*/
public static FormatterStep create(
String name,
SerializedFunction<String, String> function) {
return new NeverUpToDateStep(name, function);
}

private static final long serialVersionUID = 1L;

private final String name;
private final SerializedFunction<String, String> formatter; // initialized lazily

NeverUpToDateStep(String name, SerializedFunction<String, String> formatter) {
this.name = name;
this.formatter = formatter;
}

@Override
public String getName() {
return name;
}

@Override
public String format(String rawUnix, File file) throws Exception {
return formatter.apply(rawUnix);
}

@Override
public void close() throws Exception {
if (formatter instanceof FormatterFunc.Closeable) {
((FormatterFunc.Closeable) formatter).close();
}
}
}
Loading
Loading