Skip to content

Commit 6579264

Browse files
authored
Emit contextual error message during incremental m2e builds (#1962)
2 parents 6440e64 + a4641ca commit 6579264

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* New static method to `DiffMessageFormatter` which allows to retrieve diffs with their line numbers ([#1960](https://github.com/diffplug/spotless/issues/1960))
1315
### Changes
1416
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))
1517

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.nio.file.Path;
2525
import java.util.List;
2626
import java.util.ListIterator;
27+
import java.util.Map;
2728
import java.util.Objects;
2829

2930
import org.eclipse.jgit.diff.DiffFormatter;
31+
import org.eclipse.jgit.diff.Edit;
3032
import org.eclipse.jgit.diff.EditList;
3133
import org.eclipse.jgit.diff.MyersDiff;
3234
import org.eclipse.jgit.diff.RawText;
@@ -234,6 +236,19 @@ private void addIntendedLine(String indent, String line) {
234236
* sequence (\n, \r, \r\n).
235237
*/
236238
private String diff(File file) throws IOException {
239+
return diff(formatter, file).getValue();
240+
}
241+
242+
/**
243+
* Returns a map entry with value being a git-style diff between the contents of the given file and what those contents would
244+
* look like if formatted using the given formatter. Does not end with any newline
245+
* sequence (\n, \r, \r\n). The key of the map entry is the 0-based line where the first difference occurred.
246+
*/
247+
public static Map.Entry<Integer, String> diff(Formatter formatter, File file) throws IOException {
248+
return diff(new CleanProviderFormatter(formatter), file);
249+
}
250+
251+
private static Map.Entry<Integer, String> diff(CleanProvider formatter, File file) throws IOException {
237252
String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding());
238253
String rawUnix = LineEnding.toUnix(raw);
239254
String formatted = formatter.getFormatted(file, rawUnix);
@@ -248,13 +263,13 @@ private String diff(File file) throws IOException {
248263
}
249264

250265
/**
251-
* Returns a git-style diff between the two unix strings.
266+
* Returns a map entry with value being a git-style diff between the two unix strings and key being the 0-based line of the first difference (in the dirty string)
252267
* <p>
253268
* Output has no trailing newlines.
254269
* <p>
255270
* Boolean args determine whether whitespace or line endings will be visible.
256271
*/
257-
private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
272+
private static Map.Entry<Integer, String> diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
258273
dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
259274
clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);
260275

@@ -271,7 +286,11 @@ private static String diffWhitespaceLineEndings(String dirty, String clean, bool
271286

272287
// we don't need the diff to show this, since we display newlines ourselves
273288
formatted = formatted.replace("\\ No newline at end of file\n", "");
274-
return NEWLINE_MATCHER.trimTrailingFrom(formatted);
289+
return Map.entry(getLineOfFirstDifference(edits), NEWLINE_MATCHER.trimTrailingFrom(formatted));
290+
}
291+
292+
private static int getLineOfFirstDifference(EditList edits) {
293+
return edits.stream().mapToInt(Edit::getBeginA).min().getAsInt();
275294
}
276295

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

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* M2E support: Emit file specific errors during incremental build. ([#1960](https://github.com/diffplug/spotless/issues/1960))
68
### Changes
79
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))
810

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
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;
2526
import org.apache.maven.plugins.annotations.Mojo;
27+
import org.sonatype.plexus.build.incremental.BuildContext;
2628

2729
import com.diffplug.spotless.Formatter;
2830
import com.diffplug.spotless.PaddedCell;
@@ -54,13 +56,17 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
5456
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
5557
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
5658
problemFiles.add(file);
59+
if (buildContext.isIncremental()) {
60+
Map.Entry<Integer, String> diffEntry = DiffMessageFormatter.diff(formatter, file);
61+
buildContext.addMessage(file, diffEntry.getKey() + 1, 0, diffEntry.getValue(), BuildContext.SEVERITY_ERROR, null);
62+
}
5763
counter.cleaned();
5864
} else {
5965
counter.checkedButAlreadyClean();
6066
upToDateChecker.setUpToDate(file.toPath());
6167
}
6268
} catch (IOException | RuntimeException e) {
63-
throw new MojoExecutionException("Unable to format file " + file, e);
69+
throw new MojoExecutionException("Unable to check file " + file, e);
6470
}
6571
}
6672

0 commit comments

Comments
 (0)