Skip to content

Commit 0868063

Browse files
committed
Revert the change to console formatting in x.26.0 (#485).
1 parent 14e74bc commit 0868063

7 files changed

Lines changed: 200 additions & 501 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ You might be looking for:
77

88
### Version 1.27.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))
99

10+
* Revert the change in console display of errors from 1.26.0 ([#485](https://github.com/diffplug/spotless/pull/485)) because [of these problems](https://github.com/diffplug/spotless/pull/485#issuecomment-552925932).
11+
1012
### Version 1.26.0 - November 11th 2019 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.26.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.26.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
1113

1214
* Fix project URLs in poms. ([#478](https://github.com/diffplug/spotless/pull/478))

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ Once someone has filled in one square of the formatter/build system matrix, it's
9292
- Thanks to [Simon Gamma](https://github.com/simschla) for [adding support for npm-based formatters](https://github.com/diffplug/spotless/pull/283), including `prettier` and `tsfmt`.
9393
- Thanks to [Frank Vennemeyer](https://github.com/fvgh) for [Groovy support via greclipse](https://github.com/diffplug/spotless/issues/13), [C++ support via CDT](https://github.com/diffplug/spotless/issues/232), [XML support via WTP](https://github.com/diffplug/spotless/pull/241) and a huge body of work with other eclipse-based formatters.
9494
- Thanks to [Konstantin Lutovich](https://github.com/lutovich) for [implementing the maven plugin](https://github.com/diffplug/spotless/pull/188).
95-
- Thanks to [Vladimir Sitnikov](https://github.com/vlsi) for [improving our format error messages](https://github.com/diffplug/spotless/pull/485).
9695
- Thanks to [Kevin Brooks](https://github.com/k-brooks) for [updating all eclipse-based formatters to 4.13](https://github.com/diffplug/spotless/pull/482).
9796
- Thanks to [Joan Goyeau](https://github.com/joan38) for [fixing scalafmt integration](https://github.com/diffplug/spotless/pull/260).
9897
- Thanks to [Nick Sutcliffe](https://github.com/nsutcliffe) for [fixing scalafmt post-2.0](https://github.com/diffplug/spotless/pull/416).

lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
import java.io.ByteArrayOutputStream;
1919
import java.io.File;
2020
import java.io.IOException;
21-
import java.nio.charset.Charset;
2221
import java.nio.charset.StandardCharsets;
2322
import java.nio.file.Files;
2423
import java.util.List;
2524
import java.util.ListIterator;
2625
import java.util.Objects;
2726

27+
import org.eclipse.jgit.diff.DiffFormatter;
2828
import org.eclipse.jgit.diff.EditList;
29-
import org.eclipse.jgit.diff.HistogramDiff;
29+
import org.eclipse.jgit.diff.MyersDiff;
3030
import org.eclipse.jgit.diff.RawText;
3131
import org.eclipse.jgit.diff.RawTextComparator;
3232

33+
import com.diffplug.common.base.CharMatcher;
3334
import com.diffplug.common.base.Errors;
3435
import com.diffplug.common.base.Preconditions;
3536
import com.diffplug.common.base.Splitter;
@@ -138,10 +139,7 @@ private void addFile(String arg) {
138139

139140
// then we'll print the rest that can fit
140141
ListIterator<String> iter = lines.listIterator(Math.min(MIN_LINES_PER_FILE, lines.size()));
141-
// lines.size() - iter.nextIndex() == 1 means "just one line left", and we just print the line
142-
// instead of "1 more lines that didn't fit"
143-
while (iter.hasNext() &&
144-
(numLines < MAX_CHECK_MESSAGE_LINES || lines.size() - iter.nextIndex() == 1)) {
142+
while (iter.hasNext() && numLines < MAX_CHECK_MESSAGE_LINES) {
145143
addIntendedLine(DIFF_INDENT, iter.next());
146144
}
147145

@@ -170,30 +168,70 @@ private void addIntendedLine(String indent, String line) {
170168
* sequence (\n, \r, \r\n).
171169
*/
172170
private static String diff(Builder builder, File file) throws IOException {
173-
Charset encoding = builder.formatter.getEncoding();
174-
String raw = new String(Files.readAllBytes(file.toPath()), encoding);
171+
String raw = new String(Files.readAllBytes(file.toPath()), builder.formatter.getEncoding());
175172
String rawUnix = LineEnding.toUnix(raw);
176173
String formattedUnix;
177174
if (builder.isPaddedCell) {
178175
formattedUnix = PaddedCell.check(builder.formatter, file, rawUnix).canonical();
179176
} else {
180177
formattedUnix = builder.formatter.compute(rawUnix, file);
181178
}
182-
String formatted = builder.formatter.computeLineEndings(formattedUnix, file);
183-
return visualizeDiff(raw, formatted);
179+
180+
if (rawUnix.equals(formattedUnix)) {
181+
// the formatting is fine, so it's a line-ending issue
182+
String formatted = builder.formatter.computeLineEndings(formattedUnix, file);
183+
return diffWhitespaceLineEndings(raw, formatted, false, true);
184+
} else {
185+
return diffWhitespaceLineEndings(rawUnix, formattedUnix, true, false);
186+
}
184187
}
185188

186-
private static String visualizeDiff(String raw, String formattedBytes) throws IOException {
187-
RawText a = new RawText(raw.getBytes(StandardCharsets.UTF_8));
188-
RawText b = new RawText(formattedBytes.getBytes(StandardCharsets.UTF_8));
189-
EditList edits = new HistogramDiff().diff(RawTextComparator.DEFAULT, a, b);
189+
/**
190+
* Returns a git-style diff between the two unix strings.
191+
*
192+
* Output has no trailing newlines.
193+
*
194+
* Boolean args determine whether whitespace or line endings will be visible.
195+
*/
196+
private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
197+
dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
198+
clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);
199+
200+
RawText a = new RawText(dirty.getBytes(StandardCharsets.UTF_8));
201+
RawText b = new RawText(clean.getBytes(StandardCharsets.UTF_8));
202+
EditList edits = new EditList();
203+
edits.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b));
204+
190205
ByteArrayOutputStream out = new ByteArrayOutputStream();
191-
// defaultCharset is here so the formatter could select "fancy" or "simple"
192-
// characters for whitespace visualization based on the capabilities of the console
193-
// For instance, if the app is running with file.encoding=ISO-8859-1, then
194-
// the console can't encode fancy whitespace characters, and the formatter would
195-
// resort to simple "\\r", "\\n", and so on
196-
new WriteSpaceAwareDiffFormatter(out, Charset.defaultCharset()).format(edits, a, b);
197-
return new String(out.toByteArray(), StandardCharsets.UTF_8);
206+
try (DiffFormatter formatter = new DiffFormatter(out)) {
207+
formatter.format(edits, a, b);
208+
}
209+
String formatted = out.toString(StandardCharsets.UTF_8.name());
210+
211+
// we don't need the diff to show this, since we display newlines ourselves
212+
formatted = formatted.replace("\\ No newline at end of file\n", "");
213+
return NEWLINE_MATCHER.trimTrailingFrom(formatted);
198214
}
215+
216+
private static final CharMatcher NEWLINE_MATCHER = CharMatcher.is('\n');
217+
218+
/**
219+
* Makes the whitespace and/or the lineEndings visible.
220+
*
221+
* MyersDiff wants inputs with only unix line endings. So this ensures that that is the case.
222+
*/
223+
private static String visibleWhitespaceLineEndings(String input, boolean whitespace, boolean lineEndings) {
224+
if (whitespace) {
225+
input = input.replace(' ', MIDDLE_DOT).replace("\t", "\\t");
226+
}
227+
if (lineEndings) {
228+
input = input.replace("\n", "\\n\n").replace("\r", "\\r");
229+
} else {
230+
// we want only \n, so if we didn't replace them above, we'll replace them here.
231+
input = input.replace("\r", "");
232+
}
233+
return input;
234+
}
235+
236+
private static final char MIDDLE_DOT = '\u00b7';
199237
}

lib-extra/src/main/java/com/diffplug/spotless/extra/integration/WriteSpaceAwareDiffFormatter.java

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

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Version 3.27.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))
44

5+
* Revert the change in console display of errors from 3.26.0 ([#485](https://github.com/diffplug/spotless/pull/485)) because [of these problems](https://github.com/diffplug/spotless/pull/485#issuecomment-552925932).
6+
57
### Version 3.26.0 - November 11th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.26.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.26.0))
68

79
* Fix project URLs in poms. ([#478](https://github.com/diffplug/spotless/pull/478))

0 commit comments

Comments
 (0)