From 10610f8a8d12bfdc8303645daafc4d68c17c9165 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Mon, 22 May 2023 21:03:16 +0200 Subject: [PATCH] fixup! Introduce semantics-aware Java import ordering. (fixes #522) --- .../diffplug/spotless/java/ImportOrderStep.java | 16 ++++++++-------- .../diffplug/spotless/java/ImportSorterImpl.java | 4 ++-- .../diffplug/gradle/spotless/JavaExtension.java | 2 +- .../spotless/maven/java/ImportOrder.java | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java b/lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java index 7f100e0adc..e986875a48 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.TreeSet; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -40,7 +41,7 @@ public final class ImportOrderStep { private static final boolean WILDCARDS_LAST_DEFAULT = false; - private static final boolean SEMANTIC_SORT_DEFAULT = true; + private static final boolean SEMANTIC_SORT_DEFAULT = false; private static final Set TREAT_AS_PACKAGE_DEFAULT = null; private static final Set TREAT_AS_CLASS_DEFAULT = null; @@ -84,9 +85,8 @@ public FormatterStep createFrom(boolean wildcardsLast, boolean semanticSort, Set private FormatterStep createFrom(boolean wildcardsLast, boolean semanticSort, Set treatAsPackage, Set treatAsClass, Supplier> importOrder) { return FormatterStep.createLazy("importOrder", - () -> new State(importOrder.get(), lineFormat, wildcardsLast, semanticSort, - treatAsPackage == null ? Set.of() : treatAsPackage, - treatAsClass == null ? Set.of() : treatAsClass), + () -> new State(importOrder.get(), lineFormat, wildcardsLast, semanticSort, treatAsPackage, + treatAsClass), State::toFormatter); } @@ -118,8 +118,8 @@ private static final class State implements Serializable { private final String lineFormat; private final boolean wildcardsLast; private final boolean semanticSort; - private final Set treatAsPackage; - private final Set treatAsClass; + private final TreeSet treatAsPackage; + private final TreeSet treatAsClass; State(List importOrder, String lineFormat, boolean wildcardsLast, boolean semanticSort, Set treatAsPackage, Set treatAsClass) { @@ -127,8 +127,8 @@ private static final class State implements Serializable { this.lineFormat = lineFormat; this.wildcardsLast = wildcardsLast; this.semanticSort = semanticSort; - this.treatAsPackage = treatAsPackage; - this.treatAsClass = treatAsClass; + this.treatAsPackage = treatAsPackage == null ? null : new TreeSet<>(treatAsPackage); + this.treatAsClass = treatAsClass == null ? null : new TreeSet<>(treatAsClass); } FormatterFunc toFormatter() { diff --git a/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java b/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java index 828b5ef8f0..a6d5af675c 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java @@ -415,7 +415,7 @@ private String[] splitPackageAndClasses(String fqcn) { */ private boolean treatAsPackage(String prefix) { // This would be the place to introduce wild cards or even regex matching. - return treatAsPackage.contains(prefix); + return treatAsPackage != null && treatAsPackage.contains(prefix); } /** @@ -424,7 +424,7 @@ private boolean treatAsPackage(String prefix) { */ private boolean treatAsClass(String prefix) { // This would be the place to introduce wild cards or even regex matching. - return treatAsClass.contains(prefix); + return treatAsClass != null && treatAsClass.contains(prefix); } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 3016a94413..ecb519bd6e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -77,7 +77,7 @@ public class ImportOrderConfig { final File importOrderFile; boolean wildcardsLast = false; - boolean semanticSort = true; + boolean semanticSort = false; Set treatAsPackage = Set.of(); Set treatAsClass = Set.of(); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java index 94a89478bb..fe6dc10fc8 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java @@ -44,7 +44,7 @@ public class ImportOrder implements FormatterStepFactory { * lexicographically. */ @Parameter - private boolean semanticSort = true; + private boolean semanticSort = false; /** * The prefixes that should be treated as packages for