Skip to content

Commit fe92d05

Browse files
committed
Update Formatter and ValuePerStep so that something (perhaps null) gets stored into every slot of ValuePerStep per invocation.
1 parent b01e9e9 commit fe92d05

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.nio.charset.Charset;
2727
import java.util.ArrayList;
2828
import java.util.List;
29-
import java.util.ListIterator;
3029
import java.util.Objects;
3130

3231
/** Formatter which performs the full formatting. */
@@ -129,11 +128,13 @@ public String computeLineEndings(String unix, File file) {
129128
*/
130129
public String compute(String unix, File file) {
131130
ValuePerStep<Throwable> exceptionPerStep = new ValuePerStep<>(this);
132-
String result = compute(unix, file, exceptionPerStep);
133-
int firstExceptionIndex = exceptionPerStep.indexOfFirstValue();
134-
if (firstExceptionIndex != -1) {
135-
LintPolicy.error(exceptionPerStep.get(firstExceptionIndex), steps.get(firstExceptionIndex), file.getAbsolutePath());
136-
throw ThrowingEx.asRuntimeRethrowError(exceptionPerStep.get(firstExceptionIndex));
131+
String result = computeWithLint(unix, file, exceptionPerStep);
132+
for (int i = 0; i < steps.size(); ++i) {
133+
Throwable exception = exceptionPerStep.get(i);
134+
if (exception != null && exception != LintState.formatStepCausedNoChange()) {
135+
LintPolicy.error(exception, steps.get(i), file.getAbsolutePath());
136+
throw ThrowingEx.asRuntimeRethrowError(exception);
137+
}
137138
}
138139
return result;
139140
}
@@ -145,27 +146,38 @@ public String compute(String unix, File file) {
145146
* The input must have unix line endings, and the output
146147
* is guaranteed to also have unix line endings.
147148
* <p>
149+
* It doesn't matter what is inside `ValuePerStep`, the value at every index will be overwritten
150+
* when the method returns.
148151
*/
149-
String compute(String unix, File file, ValuePerStep<Throwable> exceptionPerStep) {
152+
String computeWithLint(String unix, File file, ValuePerStep<Throwable> exceptionPerStep) {
150153
Objects.requireNonNull(unix, "unix");
151154
Objects.requireNonNull(file, "file");
152155

153-
ListIterator<FormatterStep> iter = steps.listIterator();
154-
while (iter.hasNext()) {
156+
for (int i = 0; i < steps.size(); ++i) {
157+
FormatterStep step = steps.get(i);
158+
Throwable storeForStep;
155159
try {
156-
String formatted = iter.next().format(unix, file);
160+
String formatted = step.format(unix, file);
157161
if (formatted == null) {
158162
// This probably means it was a step that only checks
159163
// for errors and doesn't actually have any fixes.
160164
// No exception was thrown so we can just continue.
165+
storeForStep = LintState.formatStepCausedNoChange();
161166
} else {
162167
// Should already be unix-only, but some steps might misbehave.
163-
unix = LineEnding.toUnix(formatted);
168+
String clean = LineEnding.toUnix(formatted);
169+
if (clean.equals(unix)) {
170+
storeForStep = LintState.formatStepCausedNoChange();
171+
} else {
172+
storeForStep = null;
173+
unix = LineEnding.toUnix(formatted);
174+
}
164175
}
165176
} catch (Throwable e) {
166177
// store the exception which was thrown and keep going
167-
exceptionPerStep.set(iter.previousIndex(), e);
178+
storeForStep = e;
168179
}
180+
exceptionPerStep.set(i, storeForStep);
169181
}
170182
return unix;
171183
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class ValuePerStep<T> extends AbstractList<T> {
5151
T previousValue = this.value;
5252
this.value = exception;
5353
return previousValue;
54+
} else if (value == null) {
55+
// everything is assumed null anyway so we don't need to do anything
56+
return null;
5457
} else {
5558
multipleValues = new Object[size];
5659
multipleValues[valueIdx] = this.value;

0 commit comments

Comments
 (0)