From a6717ef5c42a46eec4652b7af8365f4b53e322a1 Mon Sep 17 00:00:00 2001 From: Peter Nied Date: Fri, 4 Oct 2024 16:38:32 +0000 Subject: [PATCH 1/2] Java import order, ignore duplicate group entries While setting the java importOrder, if there are an duplicate item the array of strings, the output is doubled which can be very confusing. I've made an update to ignore duplicate importOrder groups, e.g. `importOrder('java|javax', '', '', 'java|bar')` would be interpreted as `importOrder('java|javax', '', 'bar')`. Signed-off-by: Peter Nied --- CHANGES.md | 2 ++ .../diffplug/spotless/java/ImportSorterImpl.java | 13 ++++++++----- plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ .../diffplug/spotless/java/ImportOrderStepTest.java | 8 +++++++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d0cf643323..9e91518bfb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Support configuring the Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238)) * Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`. ([#2259](https://github.com/diffplug/spotless/pull/2259)) +### Fixed +* Java import order, ignore duplicate group entries ## [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 8c1c357c32..0e20fbc7c5 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -9,6 +9,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general 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)) +### Fixed +* Java import order, ignore duplicate group entries ## [7.0.0.BETA2] - 2024-08-25 ### Changed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index b4151a5e9f..72dc1a6991 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -10,6 +10,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general 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)) +### Fixed +* Java import order, ignore duplicate group entries ## [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("\\#|"); From 8fb2cb89dfdd671ff0bf5b23d9a2d3c93e9b2c27 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 10 Oct 2024 15:44:54 -0700 Subject: [PATCH 2/2] Add PR links in the changelog. --- CHANGES.md | 2 +- plugin-gradle/CHANGES.md | 2 +- plugin-maven/CHANGES.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8ae3ef933c..02e14cc79a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -18,7 +18,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ([#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 +* 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/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 2d51b0e236..e3550693f7 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -11,7 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ([#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 +* 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 f3c77858d1..452e1e79a8 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -12,7 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ([#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 +* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293)) ## [2.44.0.BETA2] - 2024-08-25 ### Changed