|
1 | 1 | /*
|
2 |
| - * Copyright 2016 DiffPlug |
| 2 | + * Copyright 2016-2022 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
24 | 24 | import java.io.ObjectStreamException;
|
25 | 25 | import java.io.Serializable;
|
26 | 26 | import java.nio.charset.Charset;
|
27 |
| -import java.nio.file.Files; |
28 | 27 | import java.nio.file.Path;
|
29 | 28 | import java.nio.file.Paths;
|
30 |
| -import java.nio.file.StandardOpenOption; |
31 | 29 | import java.util.ArrayList;
|
32 |
| -import java.util.Arrays; |
33 | 30 | import java.util.List;
|
34 | 31 | import java.util.Objects;
|
35 | 32 |
|
36 |
| -import javax.annotation.Nullable; |
37 |
| - |
38 | 33 | /** Formatter which performs the full formatting. */
|
39 | 34 | public final class Formatter implements Serializable, AutoCloseable {
|
40 | 35 | private static final long serialVersionUID = 1L;
|
@@ -143,66 +138,6 @@ public Formatter build() {
|
143 | 138 | }
|
144 | 139 | }
|
145 | 140 |
|
146 |
| - /** Returns true iff the given file's formatting is up-to-date. */ |
147 |
| - public boolean isClean(File file) throws IOException { |
148 |
| - Objects.requireNonNull(file); |
149 |
| - |
150 |
| - String raw = new String(Files.readAllBytes(file.toPath()), encoding); |
151 |
| - String unix = LineEnding.toUnix(raw); |
152 |
| - |
153 |
| - // check the newlines (we can find these problems without even running the steps) |
154 |
| - int totalNewLines = (int) unix.codePoints().filter(val -> val == '\n').count(); |
155 |
| - int windowsNewLines = raw.length() - unix.length(); |
156 |
| - if (lineEndingsPolicy.isUnix(file)) { |
157 |
| - if (windowsNewLines != 0) { |
158 |
| - return false; |
159 |
| - } |
160 |
| - } else { |
161 |
| - if (windowsNewLines != totalNewLines) { |
162 |
| - return false; |
163 |
| - } |
164 |
| - } |
165 |
| - |
166 |
| - // check the other formats |
167 |
| - String formatted = compute(unix, file); |
168 |
| - |
169 |
| - // return true iff the formatted string equals the unix one |
170 |
| - return formatted.equals(unix); |
171 |
| - } |
172 |
| - |
173 |
| - /** Applies formatting to the given file. */ |
174 |
| - public void applyTo(File file) throws IOException { |
175 |
| - applyToAndReturnResultIfDirty(file); |
176 |
| - } |
177 |
| - |
178 |
| - /** |
179 |
| - * Applies formatting to the given file. |
180 |
| - * |
181 |
| - * Returns null if the file was already clean, or the |
182 |
| - * formatted result with unix newlines if it was not. |
183 |
| - */ |
184 |
| - public @Nullable String applyToAndReturnResultIfDirty(File file) throws IOException { |
185 |
| - Objects.requireNonNull(file); |
186 |
| - |
187 |
| - byte[] rawBytes = Files.readAllBytes(file.toPath()); |
188 |
| - String raw = new String(rawBytes, encoding); |
189 |
| - String rawUnix = LineEnding.toUnix(raw); |
190 |
| - |
191 |
| - // enforce the format |
192 |
| - String formattedUnix = compute(rawUnix, file); |
193 |
| - // enforce the line endings |
194 |
| - String formatted = computeLineEndings(formattedUnix, file); |
195 |
| - |
196 |
| - // write out the file iff it has changed |
197 |
| - byte[] formattedBytes = formatted.getBytes(encoding); |
198 |
| - if (!Arrays.equals(rawBytes, formattedBytes)) { |
199 |
| - Files.write(file.toPath(), formattedBytes, StandardOpenOption.TRUNCATE_EXISTING); |
200 |
| - return formattedUnix; |
201 |
| - } else { |
202 |
| - return null; |
203 |
| - } |
204 |
| - } |
205 |
| - |
206 | 141 | /** Applies the appropriate line endings to the given unix content. */
|
207 | 142 | public String computeLineEndings(String unix, File file) {
|
208 | 143 | Objects.requireNonNull(unix, "unix");
|
|
0 commit comments