Skip to content

Commit 220b374

Browse files
authored
Fix #219 - Replacing a step no longer triggers early evaluation (#351)
1 parent 0ab6802 commit 220b374

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Provided eclipse-wtp formatters in generic formatter extension. ([#325](https://github.com/diffplug/spotless/pull/325)). This change obsoletes the CSS and XML extensions.
66
* Improved configuration times for large projects (thanks to @oehme for finding [#348](https://github.com/diffplug/spotless/pull/348)).
77
* Updated default google-java-format from 1.5 to 1.7 ([#335](https://github.com/diffplug/spotless/issues/335)).
8+
* Replacing a step no longer triggers early evaluation ([#219](https://github.com/diffplug/spotless/issues/219)).
89

910
### Version 3.17.0 - December 13th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.17.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.17.0))
1011

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,39 @@ private static void relativizeIfSubdir(List<String> relativePaths, File root, Fi
209209
/** Adds a new step. */
210210
public void addStep(FormatterStep newStep) {
211211
Objects.requireNonNull(newStep);
212-
FormatterStep existing = getExistingStep(newStep.getName());
213-
if (existing != null) {
212+
int existingIdx = getExistingStepIdx(newStep.getName());
213+
if (existingIdx != -1) {
214214
throw new GradleException("Multiple steps with name '" + newStep.getName() + "' for spotless format '" + formatName() + "'");
215215
}
216216
steps.add(newStep);
217217
}
218218

219219
/** Returns the existing step with the given name, if any. */
220+
@Deprecated
220221
protected @Nullable FormatterStep getExistingStep(String stepName) {
221222
return steps.stream() //
222223
.filter(step -> stepName.equals(step.getName())) //
223224
.findFirst() //
224225
.orElse(null);
225226
}
226227

228+
/** Returns the index of the existing step with the given name, or -1 if no such step exists. */
229+
protected int getExistingStepIdx(String stepName) {
230+
for (int i = 0; i < steps.size(); ++i) {
231+
if (steps.get(i).getName().equals(stepName)) {
232+
return i;
233+
}
234+
}
235+
return -1;
236+
}
237+
227238
/** Replaces the given step. */
228239
protected void replaceStep(FormatterStep replacementStep) {
229-
FormatterStep existing = getExistingStep(replacementStep.getName());
230-
if (existing == null) {
240+
int existingIdx = getExistingStepIdx(replacementStep.getName());
241+
if (existingIdx == -1) {
231242
throw new GradleException("Cannot replace step '" + replacementStep.getName() + "' for spotless format '" + formatName() + "' because it hasn't been added yet.");
232243
}
233-
int index = steps.indexOf(existing);
234-
steps.set(index, replacementStep);
244+
steps.set(existingIdx, replacementStep);
235245
}
236246

237247
/** Clears all of the existing steps. */

0 commit comments

Comments
 (0)