Skip to content

Commit

Permalink
Revert the change to console formatting in x.26.0 (#485).
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Nov 12, 2019
1 parent 14e74bc commit 0868063
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 501 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -7,6 +7,8 @@ You might be looking for:

### 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/))

* 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).

### 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)))

* Fix project URLs in poms. ([#478](https://github.com/diffplug/spotless/pull/478))
Expand Down
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -92,7 +92,6 @@ Once someone has filled in one square of the formatter/build system matrix, it's
- 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`.
- 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.
- Thanks to [Konstantin Lutovich](https://github.com/lutovich) for [implementing the maven plugin](https://github.com/diffplug/spotless/pull/188).
- Thanks to [Vladimir Sitnikov](https://github.com/vlsi) for [improving our format error messages](https://github.com/diffplug/spotless/pull/485).
- 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).
- Thanks to [Joan Goyeau](https://github.com/joan38) for [fixing scalafmt integration](https://github.com/diffplug/spotless/pull/260).
- Thanks to [Nick Sutcliffe](https://github.com/nsutcliffe) for [fixing scalafmt post-2.0](https://github.com/diffplug/spotless/pull/416).
Expand Down
Expand Up @@ -18,18 +18,19 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;

import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.EditList;
import org.eclipse.jgit.diff.HistogramDiff;
import org.eclipse.jgit.diff.MyersDiff;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RawTextComparator;

import com.diffplug.common.base.CharMatcher;
import com.diffplug.common.base.Errors;
import com.diffplug.common.base.Preconditions;
import com.diffplug.common.base.Splitter;
Expand Down Expand Up @@ -138,10 +139,7 @@ private void addFile(String arg) {

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

Expand Down Expand Up @@ -170,30 +168,70 @@ private void addIntendedLine(String indent, String line) {
* sequence (\n, \r, \r\n).
*/
private static String diff(Builder builder, File file) throws IOException {
Charset encoding = builder.formatter.getEncoding();
String raw = new String(Files.readAllBytes(file.toPath()), encoding);
String raw = new String(Files.readAllBytes(file.toPath()), builder.formatter.getEncoding());
String rawUnix = LineEnding.toUnix(raw);
String formattedUnix;
if (builder.isPaddedCell) {
formattedUnix = PaddedCell.check(builder.formatter, file, rawUnix).canonical();
} else {
formattedUnix = builder.formatter.compute(rawUnix, file);
}
String formatted = builder.formatter.computeLineEndings(formattedUnix, file);
return visualizeDiff(raw, formatted);

if (rawUnix.equals(formattedUnix)) {
// the formatting is fine, so it's a line-ending issue
String formatted = builder.formatter.computeLineEndings(formattedUnix, file);
return diffWhitespaceLineEndings(raw, formatted, false, true);
} else {
return diffWhitespaceLineEndings(rawUnix, formattedUnix, true, false);
}
}

private static String visualizeDiff(String raw, String formattedBytes) throws IOException {
RawText a = new RawText(raw.getBytes(StandardCharsets.UTF_8));
RawText b = new RawText(formattedBytes.getBytes(StandardCharsets.UTF_8));
EditList edits = new HistogramDiff().diff(RawTextComparator.DEFAULT, a, b);
/**
* Returns a git-style diff between the two unix strings.
*
* Output has no trailing newlines.
*
* Boolean args determine whether whitespace or line endings will be visible.
*/
private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);

RawText a = new RawText(dirty.getBytes(StandardCharsets.UTF_8));
RawText b = new RawText(clean.getBytes(StandardCharsets.UTF_8));
EditList edits = new EditList();
edits.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b));

ByteArrayOutputStream out = new ByteArrayOutputStream();
// defaultCharset is here so the formatter could select "fancy" or "simple"
// characters for whitespace visualization based on the capabilities of the console
// For instance, if the app is running with file.encoding=ISO-8859-1, then
// the console can't encode fancy whitespace characters, and the formatter would
// resort to simple "\\r", "\\n", and so on
new WriteSpaceAwareDiffFormatter(out, Charset.defaultCharset()).format(edits, a, b);
return new String(out.toByteArray(), StandardCharsets.UTF_8);
try (DiffFormatter formatter = new DiffFormatter(out)) {
formatter.format(edits, a, b);
}
String formatted = out.toString(StandardCharsets.UTF_8.name());

// we don't need the diff to show this, since we display newlines ourselves
formatted = formatted.replace("\\ No newline at end of file\n", "");
return NEWLINE_MATCHER.trimTrailingFrom(formatted);
}

private static final CharMatcher NEWLINE_MATCHER = CharMatcher.is('\n');

/**
* Makes the whitespace and/or the lineEndings visible.
*
* MyersDiff wants inputs with only unix line endings. So this ensures that that is the case.
*/
private static String visibleWhitespaceLineEndings(String input, boolean whitespace, boolean lineEndings) {
if (whitespace) {
input = input.replace(' ', MIDDLE_DOT).replace("\t", "\\t");
}
if (lineEndings) {
input = input.replace("\n", "\\n\n").replace("\r", "\\r");
} else {
// we want only \n, so if we didn't replace them above, we'll replace them here.
input = input.replace("\r", "");
}
return input;
}

private static final char MIDDLE_DOT = '\u00b7';
}

This file was deleted.

2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Expand Up @@ -2,6 +2,8 @@

### 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/))

* 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).

### 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))

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

0 comments on commit 0868063

Please sign in to comment.