diff --git a/CHANGES.md b/CHANGES.md index 95ce8f24c1..02e14cc79a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`. ([#2259](https://github.com/diffplug/spotless/pull/2259)) * Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279)) +### Fixed +* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293)) ## [3.0.0.BETA2] - 2024-08-25 ### Changed 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 4d343d41d8..4393de95a3 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ final class ImportSorterImpl { private static final String SUBGROUP_SEPARATOR = "|"; private final List importsGroups; + private final Set knownGroupings = new HashSet<>(); private final Map> matchingImports = new HashMap<>(); private final List notMatching = new ArrayList<>(); private final Set allImportOrderItems = new HashSet<>(); @@ -44,10 +45,12 @@ private static class ImportsGroup { private final List subGroups; - public ImportsGroup(String importOrder) { + public ImportsGroup(String importOrder, Set knownGroupings) { this.subGroups = Stream.of(importOrder.split("\\" + SUBGROUP_SEPARATOR, -1)) .map(this::normalizeStatic) + .filter(group -> !knownGroupings.contains(group)) .collect(Collectors.toList()); + knownGroupings.addAll(this.subGroups); } private String normalizeStatic(String subgroup) { @@ -80,7 +83,7 @@ private List sort(List imports, String lineFormat) { private ImportSorterImpl(List importOrder, boolean wildcardsLast, boolean semanticSort, Set treatAsPackage, Set treatAsClass) { - importsGroups = importOrder.stream().filter(Objects::nonNull).map(ImportsGroup::new).collect(Collectors.toList()); + importsGroups = importOrder.stream().filter(Objects::nonNull).map(order -> new ImportsGroup(order, knownGroupings)).collect(Collectors.toList()); putStaticItemIfNotExists(importsGroups); putCatchAllGroupIfNotExists(importsGroups); @@ -107,13 +110,13 @@ private void putStaticItemIfNotExists(List importsGroups) { indexOfFirstStatic = i; } } - importsGroups.add(indexOfFirstStatic, new ImportsGroup(STATIC_KEYWORD)); + importsGroups.add(indexOfFirstStatic, new ImportsGroup(STATIC_KEYWORD, this.knownGroupings)); } private void putCatchAllGroupIfNotExists(List importsGroups) { boolean catchAllSubGroupExist = importsGroups.stream().anyMatch(group -> group.getSubGroups().contains(CATCH_ALL_SUBGROUP)); if (!catchAllSubGroupExist) { - importsGroups.add(new ImportsGroup(CATCH_ALL_SUBGROUP)); + importsGroups.add(new ImportsGroup(CATCH_ALL_SUBGROUP, this.knownGroupings)); } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index ec6f0aea9f..e3550693f7 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -10,6 +10,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable). ([#2259](https://github.com/diffplug/spotless/pull/2259)) * Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279)) +### Fixed +* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293)) ## [7.0.0.BETA2] - 2024-08-25 ### Changed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 4328fc14e1..452e1e79a8 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -11,6 +11,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable). ([#2259](https://github.com/diffplug/spotless/pull/2259)) * Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279)) +### Fixed +* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293)) ## [2.44.0.BETA2] - 2024-08-25 ### Changed diff --git a/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java index 75cd984f08..47bdff89dd 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,12 @@ void sortImportsFromArrayWithSubgroups() { StepHarness.forStep(step).testResource("java/importsorter/JavaCodeUnsortedImportsSubgroups.test", "java/importsorter/JavaCodeSortedImportsSubgroups.test"); } + @Test + void sortImportsFromArrayWithDuplicateEntries() { + FormatterStep step = ImportOrderStep.forJava().createFrom("java|javax", "java", "org|\\#com", "\\#"); + StepHarness.forStep(step).testResource("java/importsorter/JavaCodeUnsortedImportsSubgroups.test", "java/importsorter/JavaCodeSortedImportsSubgroups.test"); + } + @Test void sortImportsFromArrayWithSubgroupsLeadingCatchAll() { FormatterStep step = ImportOrderStep.forJava().createFrom("\\#|");