Skip to content

Commit 3ee9093

Browse files
committed
Remove crufty and internally-unused methods from Formatter.
1 parent 48723eb commit 3ee9093

File tree

2 files changed

+4
-66
lines changed

2 files changed

+4
-66
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This document is intended for Spotless developers.
99

1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

12+
## [TBD lint release]
13+
* **BREAKING** Removed `isClean`, `applyTo`, and `applyToAndReturnResultIfDirty` from `Formatter` because users should instead use `PaddedCell.check()`.
14+
1215
## [Unreleased]
1316
### Changed
1417
* Bump CI from Java 15 to 17 ([#1094](https://github.com/diffplug/spotless/pull/1094)).

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

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,17 +24,12 @@
2424
import java.io.ObjectStreamException;
2525
import java.io.Serializable;
2626
import java.nio.charset.Charset;
27-
import java.nio.file.Files;
2827
import java.nio.file.Path;
2928
import java.nio.file.Paths;
30-
import java.nio.file.StandardOpenOption;
3129
import java.util.ArrayList;
32-
import java.util.Arrays;
3330
import java.util.List;
3431
import java.util.Objects;
3532

36-
import javax.annotation.Nullable;
37-
3833
/** Formatter which performs the full formatting. */
3934
public final class Formatter implements Serializable, AutoCloseable {
4035
private static final long serialVersionUID = 1L;
@@ -143,66 +138,6 @@ public Formatter build() {
143138
}
144139
}
145140

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-
206141
/** Applies the appropriate line endings to the given unix content. */
207142
public String computeLineEndings(String unix, File file) {
208143
Objects.requireNonNull(unix, "unix");

0 commit comments

Comments
 (0)