Skip to content

Commit bdecbad

Browse files
committed
Emit line number of first difference
Keep CleanProviderFormatter private
1 parent 5903bb6 commit bdecbad

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
25+
import java.util.AbstractMap;
2526
import java.util.List;
2627
import java.util.ListIterator;
28+
import java.util.Map;
2729
import java.util.Objects;
2830

2931
import org.eclipse.jgit.diff.DiffFormatter;
32+
import org.eclipse.jgit.diff.Edit;
3033
import org.eclipse.jgit.diff.EditList;
3134
import org.eclipse.jgit.diff.MyersDiff;
3235
import org.eclipse.jgit.diff.RawText;
@@ -55,10 +58,10 @@ interface CleanProvider {
5558
String getFormatted(File file, String rawUnix);
5659
}
5760

58-
public static class CleanProviderFormatter implements CleanProvider {
61+
private static class CleanProviderFormatter implements CleanProvider {
5962
private final Formatter formatter;
6063

61-
public CleanProviderFormatter(Formatter formatter) {
64+
CleanProviderFormatter(Formatter formatter) {
6265
this.formatter = Objects.requireNonNull(formatter);
6366
}
6467

@@ -234,10 +237,19 @@ private void addIntendedLine(String indent, String line) {
234237
* sequence (\n, \r, \r\n).
235238
*/
236239
private String diff(File file) throws IOException {
237-
return diff(formatter, file);
240+
return diff(formatter, file).getKey();
238241
}
239242

240-
public static String diff(CleanProvider formatter, File file) throws IOException {
243+
/**
244+
* Returns a map entry with key being a git-style diff between the contents of the given file and what those contents would
245+
* look like if formatted using the given formatter. Does not end with any newline
246+
* sequence (\n, \r, \r\n). The value of the map entry is the line where the first difference occurred.
247+
*/
248+
public static Map.Entry<String, Integer> diff(Formatter formatter, File file) throws IOException {
249+
return diff(new CleanProviderFormatter(formatter), file);
250+
}
251+
252+
private static Map.Entry<String, Integer> diff(CleanProvider formatter, File file) throws IOException {
241253
String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding());
242254
String rawUnix = LineEnding.toUnix(raw);
243255
String formatted = formatter.getFormatted(file, rawUnix);
@@ -252,13 +264,13 @@ public static String diff(CleanProvider formatter, File file) throws IOException
252264
}
253265

254266
/**
255-
* Returns a git-style diff between the two unix strings.
267+
* Returns a map entry with key being a git-style diff between the two unix strings and value being the line of the first difference (in the dirty string)
256268
* <p>
257269
* Output has no trailing newlines.
258270
* <p>
259271
* Boolean args determine whether whitespace or line endings will be visible.
260272
*/
261-
private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
273+
private static Map.Entry<String, Integer> diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
262274
dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
263275
clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);
264276

@@ -275,7 +287,11 @@ private static String diffWhitespaceLineEndings(String dirty, String clean, bool
275287

276288
// we don't need the diff to show this, since we display newlines ourselves
277289
formatted = formatted.replace("\\ No newline at end of file\n", "");
278-
return NEWLINE_MATCHER.trimTrailingFrom(formatted);
290+
return new AbstractMap.SimpleEntry<>(NEWLINE_MATCHER.trimTrailingFrom(formatted), getLineOfFirstDifference(edits));
291+
}
292+
293+
private static int getLineOfFirstDifference(EditList edits) {
294+
return edits.stream().mapToInt(Edit::getBeginA).min().getAsInt();
279295
}
280296

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

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Map;
2223

2324
import org.apache.maven.plugin.MojoExecutionException;
2425
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -28,7 +29,6 @@
2829
import com.diffplug.spotless.Formatter;
2930
import com.diffplug.spotless.PaddedCell;
3031
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
31-
import com.diffplug.spotless.extra.integration.DiffMessageFormatter.CleanProviderFormatter;
3232
import com.diffplug.spotless.maven.incremental.UpToDateChecker;
3333

3434
/**
@@ -57,15 +57,16 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
5757
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
5858
problemFiles.add(file);
5959
if (buildContext.isIncremental()) {
60-
buildContext.addMessage(file, 0, 0, DiffMessageFormatter.diff(new CleanProviderFormatter(formatter), file), BuildContext.SEVERITY_ERROR, null);
60+
Map.Entry<String, Integer> diffEntry = DiffMessageFormatter.diff(formatter, file);
61+
buildContext.addMessage(file, diffEntry.getValue(), 0, diffEntry.getKey(), BuildContext.SEVERITY_ERROR, null);
6162
}
6263
counter.cleaned();
6364
} else {
6465
counter.checkedButAlreadyClean();
6566
upToDateChecker.setUpToDate(file.toPath());
6667
}
6768
} catch (IOException | RuntimeException e) {
68-
throw new MojoExecutionException("Unable to format file " + file, e);
69+
throw new MojoExecutionException("Unable to check file " + file, e);
6970
}
7071
}
7172

0 commit comments

Comments
 (0)