Skip to content

Commit

Permalink
fixup! Introduce semantics-aware Java import ordering. (fixes #522)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frettman committed May 22, 2023
1 parent 7deb02e commit 10610f8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> TREAT_AS_PACKAGE_DEFAULT = null;
private static final Set<String> TREAT_AS_CLASS_DEFAULT = null;

Expand Down Expand Up @@ -84,9 +85,8 @@ public FormatterStep createFrom(boolean wildcardsLast, boolean semanticSort, Set
private FormatterStep createFrom(boolean wildcardsLast, boolean semanticSort, Set<String> treatAsPackage,
Set<String> treatAsClass, Supplier<List<String>> 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);
}

Expand Down Expand Up @@ -118,17 +118,17 @@ private static final class State implements Serializable {
private final String lineFormat;
private final boolean wildcardsLast;
private final boolean semanticSort;
private final Set<String> treatAsPackage;
private final Set<String> treatAsClass;
private final TreeSet<String> treatAsPackage;
private final TreeSet<String> treatAsClass;

State(List<String> importOrder, String lineFormat, boolean wildcardsLast, boolean semanticSort,
Set<String> treatAsPackage, Set<String> treatAsClass) {
this.importOrder = importOrder;
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class ImportOrderConfig {
final File importOrderFile;

boolean wildcardsLast = false;
boolean semanticSort = true;
boolean semanticSort = false;
Set<String> treatAsPackage = Set.of();
Set<String> treatAsClass = Set.of();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 10610f8

Please sign in to comment.