|
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;
|
@@ -159,66 +154,6 @@ public Formatter build() {
|
159 | 154 | }
|
160 | 155 | }
|
161 | 156 |
|
162 |
| - /** Returns true iff the given file's formatting is up-to-date. */ |
163 |
| - public boolean isClean(File file) throws IOException { |
164 |
| - Objects.requireNonNull(file); |
165 |
| - |
166 |
| - String raw = new String(Files.readAllBytes(file.toPath()), encoding); |
167 |
| - String unix = LineEnding.toUnix(raw); |
168 |
| - |
169 |
| - // check the newlines (we can find these problems without even running the steps) |
170 |
| - int totalNewLines = (int) unix.codePoints().filter(val -> val == '\n').count(); |
171 |
| - int windowsNewLines = raw.length() - unix.length(); |
172 |
| - if (lineEndingsPolicy.isUnix(file)) { |
173 |
| - if (windowsNewLines != 0) { |
174 |
| - return false; |
175 |
| - } |
176 |
| - } else { |
177 |
| - if (windowsNewLines != totalNewLines) { |
178 |
| - return false; |
179 |
| - } |
180 |
| - } |
181 |
| - |
182 |
| - // check the other formats |
183 |
| - String formatted = compute(unix, file); |
184 |
| - |
185 |
| - // return true iff the formatted string equals the unix one |
186 |
| - return formatted.equals(unix); |
187 |
| - } |
188 |
| - |
189 |
| - /** Applies formatting to the given file. */ |
190 |
| - public void applyTo(File file) throws IOException { |
191 |
| - applyToAndReturnResultIfDirty(file); |
192 |
| - } |
193 |
| - |
194 |
| - /** |
195 |
| - * Applies formatting to the given file. |
196 |
| - * <p> |
197 |
| - * Returns null if the file was already clean, or the |
198 |
| - * formatted result with unix newlines if it was not. |
199 |
| - */ |
200 |
| - public @Nullable String applyToAndReturnResultIfDirty(File file) throws IOException { |
201 |
| - Objects.requireNonNull(file); |
202 |
| - |
203 |
| - byte[] rawBytes = Files.readAllBytes(file.toPath()); |
204 |
| - String raw = new String(rawBytes, encoding); |
205 |
| - String rawUnix = LineEnding.toUnix(raw); |
206 |
| - |
207 |
| - // enforce the format |
208 |
| - String formattedUnix = compute(rawUnix, file); |
209 |
| - // enforce the line endings |
210 |
| - String formatted = computeLineEndings(formattedUnix, file); |
211 |
| - |
212 |
| - // write out the file iff it has changed |
213 |
| - byte[] formattedBytes = formatted.getBytes(encoding); |
214 |
| - if (!Arrays.equals(rawBytes, formattedBytes)) { |
215 |
| - Files.write(file.toPath(), formattedBytes, StandardOpenOption.TRUNCATE_EXISTING); |
216 |
| - return formattedUnix; |
217 |
| - } else { |
218 |
| - return null; |
219 |
| - } |
220 |
| - } |
221 |
| - |
222 | 157 | /** Applies the appropriate line endings to the given unix content. */
|
223 | 158 | public String computeLineEndings(String unix, File file) {
|
224 | 159 | Objects.requireNonNull(unix, "unix");
|
|
0 commit comments