26
26
import java .nio .charset .Charset ;
27
27
import java .util .ArrayList ;
28
28
import java .util .List ;
29
- import java .util .ListIterator ;
30
29
import java .util .Objects ;
31
30
32
31
/** Formatter which performs the full formatting. */
@@ -129,11 +128,13 @@ public String computeLineEndings(String unix, File file) {
129
128
*/
130
129
public String compute (String unix , File file ) {
131
130
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
+ }
137
138
}
138
139
return result ;
139
140
}
@@ -145,27 +146,38 @@ public String compute(String unix, File file) {
145
146
* The input must have unix line endings, and the output
146
147
* is guaranteed to also have unix line endings.
147
148
* <p>
149
+ * It doesn't matter what is inside `ValuePerStep`, the value at every index will be overwritten
150
+ * when the method returns.
148
151
*/
149
- String compute (String unix , File file , ValuePerStep <Throwable > exceptionPerStep ) {
152
+ String computeWithLint (String unix , File file , ValuePerStep <Throwable > exceptionPerStep ) {
150
153
Objects .requireNonNull (unix , "unix" );
151
154
Objects .requireNonNull (file , "file" );
152
155
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 ;
155
159
try {
156
- String formatted = iter . next () .format (unix , file );
160
+ String formatted = step .format (unix , file );
157
161
if (formatted == null ) {
158
162
// This probably means it was a step that only checks
159
163
// for errors and doesn't actually have any fixes.
160
164
// No exception was thrown so we can just continue.
165
+ storeForStep = LintState .formatStepCausedNoChange ();
161
166
} else {
162
167
// 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
+ }
164
175
}
165
176
} catch (Throwable e ) {
166
177
// store the exception which was thrown and keep going
167
- exceptionPerStep . set ( iter . previousIndex (), e ) ;
178
+ storeForStep = e ;
168
179
}
180
+ exceptionPerStep .set (i , storeForStep );
169
181
}
170
182
return unix ;
171
183
}
0 commit comments