Skip to content

Commit 856ab9f

Browse files
committed
Removed rootDir property Formatter and its builder because it was only used to annnotate errors, which will now done by the lint phase.
1 parent 5843830 commit 856ab9f

File tree

13 files changed

+19
-82
lines changed

13 files changed

+19
-82
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 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.
@@ -58,15 +58,17 @@ interface CleanProvider {
5858
}
5959

6060
private static class CleanProviderFormatter implements CleanProvider {
61+
private final Path rootDir;
6162
private final Formatter formatter;
6263

63-
CleanProviderFormatter(Formatter formatter) {
64+
CleanProviderFormatter(Path rootDir, Formatter formatter) {
65+
this.rootDir = Objects.requireNonNull(rootDir);
6466
this.formatter = Objects.requireNonNull(formatter);
6567
}
6668

6769
@Override
6870
public Path getRootDir() {
69-
return formatter.getRootDir();
71+
return rootDir;
7072
}
7173

7274
@Override
@@ -123,8 +125,8 @@ public Builder runToFix(String runToFix) {
123125
return this;
124126
}
125127

126-
public Builder formatter(Formatter formatter) {
127-
this.formatter = new CleanProviderFormatter(formatter);
128+
public Builder formatter(Path rootDir, Formatter formatter) {
129+
this.formatter = new CleanProviderFormatter(rootDir, formatter);
128130
return this;
129131
}
130132

@@ -244,8 +246,8 @@ private String diff(File file) throws IOException {
244246
* look like if formatted using the given formatter. Does not end with any newline
245247
* sequence (\n, \r, \r\n). The key of the map entry is the 0-based line where the first difference occurred.
246248
*/
247-
public static Map.Entry<Integer, String> diff(Formatter formatter, File file) throws IOException {
248-
return diff(new CleanProviderFormatter(formatter), file);
249+
public static Map.Entry<Integer, String> diff(Path rootDir, Formatter formatter, File file) throws IOException {
250+
return diff(new CleanProviderFormatter(rootDir, formatter), file);
249251
}
250252

251253
private static Map.Entry<Integer, String> diff(CleanProvider formatter, File file) throws IOException {

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.io.ObjectStreamException;
2525
import java.io.Serializable;
2626
import java.nio.charset.Charset;
27-
import java.nio.file.Path;
28-
import java.nio.file.Paths;
2927
import java.util.ArrayList;
3028
import java.util.List;
3129
import java.util.Objects;
@@ -38,15 +36,12 @@ public final class Formatter implements Serializable, AutoCloseable {
3836
private String name;
3937
private LineEnding.Policy lineEndingsPolicy;
4038
private Charset encoding;
41-
private Path rootDir;
4239
private List<FormatterStep> steps;
4340
private FormatExceptionPolicy exceptionPolicy;
4441

45-
private Formatter(String name, LineEnding.Policy lineEndingsPolicy, Charset encoding, Path rootDirectory, List<FormatterStep> steps, FormatExceptionPolicy exceptionPolicy) {
46-
this.name = name;
42+
private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, List<FormatterStep> steps, FormatExceptionPolicy exceptionPolicy) {
4743
this.lineEndingsPolicy = Objects.requireNonNull(lineEndingsPolicy, "lineEndingsPolicy");
4844
this.encoding = Objects.requireNonNull(encoding, "encoding");
49-
this.rootDir = Objects.requireNonNull(rootDirectory, "rootDir");
5045
this.steps = requireElementsNonNull(new ArrayList<>(steps));
5146
this.exceptionPolicy = Objects.requireNonNull(exceptionPolicy, "exceptionPolicy");
5247
}
@@ -56,7 +51,6 @@ private void writeObject(ObjectOutputStream out) throws IOException {
5651
out.writeObject(name);
5752
out.writeObject(lineEndingsPolicy);
5853
out.writeObject(encoding.name());
59-
out.writeObject(rootDir.toString());
6054
out.writeObject(steps);
6155
out.writeObject(exceptionPolicy);
6256
}
@@ -67,7 +61,6 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
6761
name = (String) in.readObject();
6862
lineEndingsPolicy = (LineEnding.Policy) in.readObject();
6963
encoding = Charset.forName((String) in.readObject());
70-
rootDir = Paths.get((String) in.readObject());
7164
steps = (List<FormatterStep>) in.readObject();
7265
exceptionPolicy = (FormatExceptionPolicy) in.readObject();
7366
}
@@ -90,10 +83,6 @@ public Charset getEncoding() {
9083
return encoding;
9184
}
9285

93-
public Path getRootDir() {
94-
return rootDir;
95-
}
96-
9786
public List<FormatterStep> getSteps() {
9887
return steps;
9988
}
@@ -112,7 +101,6 @@ public static class Builder {
112101
// required parameters
113102
private LineEnding.Policy lineEndingsPolicy;
114103
private Charset encoding;
115-
private Path rootDir;
116104
private List<FormatterStep> steps;
117105
private FormatExceptionPolicy exceptionPolicy;
118106

@@ -133,11 +121,6 @@ public Builder encoding(Charset encoding) {
133121
return this;
134122
}
135123

136-
public Builder rootDir(Path rootDir) {
137-
this.rootDir = rootDir;
138-
return this;
139-
}
140-
141124
public Builder steps(List<FormatterStep> steps) {
142125
this.steps = steps;
143126
return this;
@@ -149,7 +132,7 @@ public Builder exceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
149132
}
150133

151134
public Formatter build() {
152-
return new Formatter(name, lineEndingsPolicy, encoding, rootDir, steps,
135+
return new Formatter(lineEndingsPolicy, encoding, steps,
153136
exceptionPolicy == null ? FormatExceptionPolicy.failOnlyOnError() : exceptionPolicy);
154137
}
155138
}
@@ -188,13 +171,9 @@ public String compute(String unix, File file) {
188171
unix = LineEnding.toUnix(formatted);
189172
}
190173
} catch (Throwable e) {
191-
if (file == NO_FILE_SENTINEL) {
192-
exceptionPolicy.handleError(e, step, "");
193-
} else {
194-
// Path may be forged from a different FileSystem than Filesystem.default
195-
String relativePath = rootDir.relativize(rootDir.getFileSystem().getPath(file.getPath())).toString();
196-
exceptionPolicy.handleError(e, step, relativePath);
197-
}
174+
// TODO: this is not accurate, but it won't matter when add support for linting
175+
String relativePath = file.toString();
176+
exceptionPolicy.handleError(e, step, relativePath);
198177
}
199178
}
200179
return unix;
@@ -207,7 +186,6 @@ public int hashCode() {
207186
result = prime * result + name.hashCode();
208187
result = prime * result + encoding.hashCode();
209188
result = prime * result + lineEndingsPolicy.hashCode();
210-
result = prime * result + rootDir.hashCode();
211189
result = prime * result + steps.hashCode();
212190
result = prime * result + exceptionPolicy.hashCode();
213191
return result;
@@ -228,7 +206,6 @@ public boolean equals(Object obj) {
228206
return name.equals(other.name) &&
229207
encoding.equals(other.encoding) &&
230208
lineEndingsPolicy.equals(other.lineEndingsPolicy) &&
231-
rootDir.equals(other.rootDir) &&
232209
steps.equals(other.steps) &&
233210
exceptionPolicy.equals(other.exceptionPolicy);
234211
}

lib/src/main/java/com/diffplug/spotless/generic/FenceStep.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.File;
1919
import java.io.Serializable;
2020
import java.nio.charset.StandardCharsets;
21-
import java.nio.file.Path;
2221
import java.util.ArrayList;
2322
import java.util.List;
2423
import java.util.Objects;
@@ -177,7 +176,6 @@ protected Formatter buildFormatter() {
177176
.encoding(StandardCharsets.UTF_8) // can be any UTF, doesn't matter
178177
.lineEndingsPolicy(LineEnding.UNIX.createPolicy()) // just internal, won't conflict with user
179178
.steps(steps)
180-
.rootDir(Path.of("")) // TODO: error messages will be suboptimal for now, but it will get fixed when we ship linting
181179
.build();
182180
}
183181

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ Formatter buildFormatter() {
178178
.name(formatName())
179179
.lineEndingsPolicy(getLineEndingsPolicy().get())
180180
.encoding(Charset.forName(encoding))
181-
.rootDir(getProjectDir().get().getAsFile().toPath())
182181
.steps(steps)
183182
.exceptionPolicy(exceptionPolicy)
184183
.build();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
121121
private List<RemoteRepository> repositories;
122122

123123
@Parameter(defaultValue = "${project.basedir}", required = true, readonly = true)
124-
private File baseDir;
124+
protected File baseDir;
125125

126126
@Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true)
127127
private File buildDir;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public final Formatter newFormatter(Supplier<Iterable<File>> filesToFormat, Form
107107
.lineEndingsPolicy(formatterLineEndingPolicy)
108108
.exceptionPolicy(new FormatExceptionPolicyStrict())
109109
.steps(formatterSteps)
110-
.rootDir(config.getFileLocator().getBaseDir().toPath())
111110
.build();
112111
}
113112

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
8282
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
8383
problemFiles.add(file);
8484
if (buildContext.isIncremental()) {
85-
Map.Entry<Integer, String> diffEntry = DiffMessageFormatter.diff(formatter, file);
85+
Map.Entry<Integer, String> diffEntry = DiffMessageFormatter.diff(baseDir.toPath(), formatter, file);
8686
buildContext.addMessage(file, diffEntry.getKey() + 1, 0, INCREMENTAL_MESSAGE_PREFIX + diffEntry.getValue(), m2eIncrementalBuildMessageSeverity.getSeverity(), null);
8787
}
8888
counter.cleaned();
@@ -106,7 +106,7 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
106106
if (!problemFiles.isEmpty()) {
107107
throw new MojoExecutionException(DiffMessageFormatter.builder()
108108
.runToFix("Run 'mvn spotless:apply' to fix these violations.")
109-
.formatter(formatter)
109+
.formatter(baseDir.toPath(), formatter)
110110
.problemFiles(problemFiles)
111111
.getMessage());
112112
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 DiffPlug
2+
* Copyright 2021-2024 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.
@@ -28,7 +28,6 @@
2828
import java.io.IOException;
2929
import java.nio.file.Files;
3030
import java.nio.file.Path;
31-
import java.nio.file.Paths;
3231

3332
import org.apache.maven.model.Build;
3433
import org.apache.maven.model.Plugin;
@@ -37,7 +36,6 @@
3736
import org.junit.jupiter.api.BeforeEach;
3837
import org.junit.jupiter.api.Test;
3938

40-
import com.diffplug.spotless.FormatExceptionPolicyStrict;
4139
import com.diffplug.spotless.Formatter;
4240
import com.diffplug.spotless.FormatterStep;
4341
import com.diffplug.spotless.LineEnding;
@@ -116,11 +114,9 @@ private MavenProject buildMavenProject() throws IOException {
116114

117115
private static Formatter dummyFormatter() {
118116
return Formatter.builder()
119-
.rootDir(Paths.get(""))
120117
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
121118
.encoding(UTF_8)
122119
.steps(singletonList(mock(FormatterStep.class, withSettings().serializable())))
123-
.exceptionPolicy(new FormatExceptionPolicyStrict())
124120
.build();
125121
}
126122
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222

2323
import java.io.ByteArrayInputStream;
24-
import java.nio.file.Paths;
2524
import java.util.Arrays;
2625
import java.util.Collections;
2726
import java.util.List;
@@ -35,7 +34,6 @@
3534
import org.codehaus.plexus.util.xml.XmlStreamReader;
3635
import org.junit.jupiter.api.Test;
3736

38-
import com.diffplug.spotless.FormatExceptionPolicyStrict;
3937
import com.diffplug.spotless.Formatter;
4038
import com.diffplug.spotless.FormatterStep;
4139
import com.diffplug.spotless.LineEnding;
@@ -173,11 +171,9 @@ private static Formatter formatter(FormatterStep... steps) {
173171

174172
private static Formatter formatter(LineEnding lineEnding, FormatterStep... steps) {
175173
return Formatter.builder()
176-
.rootDir(Paths.get(""))
177174
.lineEndingsPolicy(lineEnding.createPolicy())
178175
.encoding(UTF_8)
179176
.steps(Arrays.asList(steps))
180-
.exceptionPolicy(new FormatExceptionPolicyStrict())
181177
.build();
182178
}
183179
}

testlib/src/main/java/com/diffplug/spotless/StepHarness.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.File;
2121
import java.nio.charset.StandardCharsets;
22-
import java.nio.file.Paths;
2322
import java.util.Arrays;
2423

2524
import org.assertj.core.api.AbstractStringAssert;
@@ -42,8 +41,6 @@ public static StepHarness forSteps(FormatterStep... steps) {
4241
.steps(Arrays.asList(steps))
4342
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
4443
.encoding(StandardCharsets.UTF_8)
45-
.rootDir(Paths.get(""))
46-
.exceptionPolicy(new FormatExceptionPolicyStrict())
4744
.build());
4845
}
4946

@@ -57,7 +54,6 @@ public static StepHarness forStepNoRoundtrip(FormatterStep step) {
5754
.steps(Arrays.asList(step))
5855
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
5956
.encoding(StandardCharsets.UTF_8)
60-
.rootDir(Paths.get(""))
6157
.exceptionPolicy(new FormatExceptionPolicyStrict())
6258
.build(), RoundTrip.DONT_ROUNDTRIP);
6359
}

0 commit comments

Comments
 (0)