Skip to content

Commit b01e9e9

Browse files
committed
Rename ExceptionPerStep to ValuePerStep, and bring Formatter closer to the old behavior.
1 parent c224929 commit b01e9e9

File tree

3 files changed

+101
-106
lines changed

3 files changed

+101
-106
lines changed

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

Lines changed: 0 additions & 101 deletions
This file was deleted.

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ public String computeLineEndings(String unix, File file) {
128128
* is guaranteed to also have unix line endings.
129129
*/
130130
public String compute(String unix, File file) {
131-
ExceptionPerStep exceptionPerStep = new ExceptionPerStep(this);
131+
ValuePerStep<Throwable> exceptionPerStep = new ValuePerStep<>(this);
132132
String result = compute(unix, file, exceptionPerStep);
133-
exceptionPerStep.rethrowFirstIfPresent();
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));
137+
}
134138
return result;
135139
}
136140

@@ -142,7 +146,7 @@ public String compute(String unix, File file) {
142146
* is guaranteed to also have unix line endings.
143147
* <p>
144148
*/
145-
String compute(String unix, File file, ExceptionPerStep exceptionPerStep) {
149+
String compute(String unix, File file, ValuePerStep<Throwable> exceptionPerStep) {
146150
Objects.requireNonNull(unix, "unix");
147151
Objects.requireNonNull(file, "file");
148152

@@ -159,9 +163,8 @@ String compute(String unix, File file, ExceptionPerStep exceptionPerStep) {
159163
unix = LineEnding.toUnix(formatted);
160164
}
161165
} catch (Throwable e) {
162-
// store the exception which was thrown, and stop execution so we don't alter line numbers
166+
// store the exception which was thrown and keep going
163167
exceptionPerStep.set(iter.previousIndex(), e);
164-
return unix;
165168
}
166169
}
167170
return unix;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.util.AbstractList;
19+
20+
import javax.annotation.Nullable;
21+
22+
/**
23+
* Fixed-size list which maintains a list of exceptions, one per step of the formatter.
24+
* Usually this list will be empty or have only a single value, so it is optimized for stack allocation in those cases.
25+
*/
26+
class ValuePerStep<T> extends AbstractList<T> {
27+
private final int size;
28+
private @Nullable T value;
29+
private int valueIdx;
30+
private @Nullable Object[] multipleValues = null;
31+
32+
ValuePerStep(Formatter formatter) {
33+
this.size = formatter.getSteps().size();
34+
}
35+
36+
@Override
37+
public @Nullable T set(int index, T exception) {
38+
if (index < 0 || index >= size) {
39+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
40+
}
41+
if (this.value == null) {
42+
this.valueIdx = index;
43+
this.value = exception;
44+
return null;
45+
} else if (this.multipleValues != null) {
46+
T previousValue = (T) multipleValues[index];
47+
multipleValues[index] = exception;
48+
return previousValue;
49+
} else {
50+
if (index == valueIdx) {
51+
T previousValue = this.value;
52+
this.value = exception;
53+
return previousValue;
54+
} else {
55+
multipleValues = new Object[size];
56+
multipleValues[valueIdx] = this.value;
57+
multipleValues[index] = exception;
58+
return null;
59+
}
60+
}
61+
}
62+
63+
@Override
64+
public T get(int index) {
65+
if (multipleValues != null) {
66+
return (T) multipleValues[index];
67+
} else if (valueIdx == index) {
68+
return value;
69+
} else {
70+
return null;
71+
}
72+
}
73+
74+
public int indexOfFirstValue() {
75+
if (multipleValues != null) {
76+
for (int i = 0; i < multipleValues.length; i++) {
77+
if (multipleValues[i] != null) {
78+
return i;
79+
}
80+
}
81+
return -1;
82+
} else if (value != null) {
83+
return valueIdx;
84+
} else {
85+
return -1;
86+
}
87+
}
88+
89+
@Override
90+
public int size() {
91+
return size;
92+
}
93+
}

0 commit comments

Comments
 (0)