Skip to content

Commit d5cc199

Browse files
author
Vincent Potucek
committed
feat: Add RemoveUnusedDeclarations
1 parent a8b5172 commit d5cc199

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2016-2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.java;
17+
18+
import com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc;
20+
21+
/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
22+
public interface RemoveUnusedDeclarationsStep {
23+
String NAME = "removeUnusedImports";
24+
25+
static FormatterStep create() {
26+
return new PalantirJavaFormatFormatterFunc("PALANTIR", true);
27+
}
28+
}

lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 DiffPlug
2+
* Copyright 2022-2025 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.
@@ -15,17 +15,23 @@
1515
*/
1616
package com.diffplug.spotless.glue.pjf;
1717

18+
import java.io.File;
1819
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
21+
import java.util.List;
22+
23+
import javax.annotation.Nullable;
2024

2125
import com.palantir.javaformat.java.Formatter;
2226
import com.palantir.javaformat.java.ImportOrderer;
2327
import com.palantir.javaformat.java.JavaFormatterOptions;
2428
import com.palantir.javaformat.java.RemoveUnusedImports;
2529

2630
import com.diffplug.spotless.FormatterFunc;
31+
import com.diffplug.spotless.FormatterStep;
32+
import com.diffplug.spotless.Lint;
2733

28-
public class PalantirJavaFormatFormatterFunc implements FormatterFunc {
34+
public class PalantirJavaFormatFormatterFunc implements FormatterFunc, FormatterStep {
2935

3036
private final Formatter formatter;
3137

@@ -52,7 +58,17 @@ public String apply(String input) throws Exception {
5258
String source = input;
5359
source = ImportOrderer.reorderImports(source, formatterStyle);
5460
source = RemoveUnusedImports.removeUnusedImports(source);
55-
return formatter.formatSource(source);
61+
return formatter.formatSourceAndFixImportsAndDeclarations(source);
62+
}
63+
64+
@Override
65+
public String apply(String unix, File file) throws Exception {
66+
return FormatterFunc.super.apply(unix, file);
67+
}
68+
69+
@Override
70+
public List<Lint> lint(String content, File file) throws Exception {
71+
return FormatterFunc.super.lint(content, file);
5672
}
5773

5874
@Override
@@ -70,4 +86,19 @@ private static void applyFormatJavadoc(JavaFormatterOptions.Builder builder) {
7086
throw new IllegalStateException("Cannot enable formatJavadoc option, make sure you are using Palantir with version 2.36.0 or later", e);
7187
}
7288
}
89+
90+
@Override
91+
public String getName() {
92+
return toString();
93+
}
94+
95+
@Nullable @Override
96+
public String format(String rawUnix, File file) throws Exception {
97+
return apply(rawUnix);
98+
}
99+
100+
@Override
101+
public void close() throws Exception {
102+
103+
}
73104
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.diffplug.spotless.java.GoogleJavaFormatStep;
4141
import com.diffplug.spotless.java.ImportOrderStep;
4242
import com.diffplug.spotless.java.PalantirJavaFormatStep;
43+
import com.diffplug.spotless.java.RemoveUnusedDeclarationsStep;
4344
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
4445
import com.diffplug.spotless.java.RemoveWildcardImportsStep;
4546

@@ -156,6 +157,10 @@ public void removeWildcardImports() {
156157
addStep(RemoveWildcardImportsStep.create());
157158
}
158159

160+
public void removeUnusedDeclarations() {
161+
addStep(RemoveUnusedDeclarationsStep.create());
162+
}
163+
159164
/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
160165
public GoogleJavaFormatConfig googleJavaFormat() {
161166
return googleJavaFormat(GoogleJavaFormatStep.defaultVersion());

0 commit comments

Comments
 (0)