From 8ce55bb2be3c32c7c7eeeff5a29238df89756e60 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Sat, 1 Mar 2025 01:05:41 +0100 Subject: [PATCH] [java-imports] IDEA-368382 incorrect place for module import (cherry picked from commit ce6ec1c65d5777e5e3059093fdd01baf24f95e50) IJ-CR-156496 GitOrigin-RevId: 4e3ae37de77005802a25b2f85de3dfa43ea4a2bf --- .../psi/codeStyle/JavaCodeStyleSettings.java | 31 ++++- .../testData/codeStyle/emptyConfigImport.xml | 10 ++ .../testData/codeStyle/withoutModules.xml | 19 +++ .../withoutModulesAndOtherImport.xml | 15 +++ ...utModulesAndOtherImportAndStaticImport.xml | 14 +++ .../withoutOtherImportWithModule.xml | 16 +++ .../withoutStaticImportWithoutModule.xml | 16 +++ .../codeStyle/JavaCodeStyleSettingsTest.java | 108 +++++++++++++++++- 8 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/codeStyle/emptyConfigImport.xml create mode 100644 java/java-tests/testData/codeStyle/withoutModules.xml create mode 100644 java/java-tests/testData/codeStyle/withoutModulesAndOtherImport.xml create mode 100644 java/java-tests/testData/codeStyle/withoutModulesAndOtherImportAndStaticImport.xml create mode 100644 java/java-tests/testData/codeStyle/withoutOtherImportWithModule.xml create mode 100644 java/java-tests/testData/codeStyle/withoutStaticImportWithoutModule.xml diff --git a/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettings.java b/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettings.java index 76719fce00c4..4b34d102d813 100644 --- a/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettings.java +++ b/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaCodeStyleSettings.java @@ -376,6 +376,10 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im private void initImportsByDefault() { PACKAGES_TO_USE_IMPORT_ON_DEMAND.addEntry(new PackageEntry(false, "java.awt", false)); PACKAGES_TO_USE_IMPORT_ON_DEMAND.addEntry(new PackageEntry(false,"javax.swing", false)); + initImportLayout(); + } + + private void initImportLayout() { IMPORT_LAYOUT_TABLE.addEntry(PackageEntry.ALL_MODULE_IMPORTS); IMPORT_LAYOUT_TABLE.addEntry(PackageEntry.ALL_OTHER_IMPORTS_ENTRY); IMPORT_LAYOUT_TABLE.addEntry(PackageEntry.BLANK_LINE_ENTRY); @@ -454,8 +458,31 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im myOldVersion = myVersion = CustomCodeStyleSettingsUtils.readVersion(parentElement.getChild(getTagName())); myIsInitialized = true; PackageEntry[] entries = IMPORT_LAYOUT_TABLE.getEntries(); - if (!ContainerUtil.exists(entries, entry -> entry == PackageEntry.ALL_MODULE_IMPORTS)) { - IMPORT_LAYOUT_TABLE.setEntryAt(PackageEntry.ALL_MODULE_IMPORTS, 0); + //if it is broken, try to restore + if (entries.length == 0) { + initImportLayout(); + } + else { + //if something is missed, restore it + if (!ContainerUtil.exists(entries, entry -> entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY)) { + if (entries[0] == PackageEntry.ALL_MODULE_IMPORTS) { + IMPORT_LAYOUT_TABLE.insertEntryAt(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, 1); + } + else { + IMPORT_LAYOUT_TABLE.insertEntryAt(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, 0); + } + } + if (!ContainerUtil.exists(entries, entry -> entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY)) { + if (entries[0] == PackageEntry.ALL_MODULE_IMPORTS) { + IMPORT_LAYOUT_TABLE.insertEntryAt(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, 1); + } + else { + IMPORT_LAYOUT_TABLE.insertEntryAt(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, 0); + } + } + if (!ContainerUtil.exists(entries, entry -> entry == PackageEntry.ALL_MODULE_IMPORTS)) { + IMPORT_LAYOUT_TABLE.insertEntryAt(PackageEntry.ALL_MODULE_IMPORTS, 0); + } } } diff --git a/java/java-tests/testData/codeStyle/emptyConfigImport.xml b/java/java-tests/testData/codeStyle/emptyConfigImport.xml new file mode 100644 index 000000000000..25f753574478 --- /dev/null +++ b/java/java-tests/testData/codeStyle/emptyConfigImport.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/java/java-tests/testData/codeStyle/withoutModules.xml b/java/java-tests/testData/codeStyle/withoutModules.xml new file mode 100644 index 000000000000..0221422984c0 --- /dev/null +++ b/java/java-tests/testData/codeStyle/withoutModules.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/java/java-tests/testData/codeStyle/withoutModulesAndOtherImport.xml b/java/java-tests/testData/codeStyle/withoutModulesAndOtherImport.xml new file mode 100644 index 000000000000..91d6dac79e67 --- /dev/null +++ b/java/java-tests/testData/codeStyle/withoutModulesAndOtherImport.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/java/java-tests/testData/codeStyle/withoutModulesAndOtherImportAndStaticImport.xml b/java/java-tests/testData/codeStyle/withoutModulesAndOtherImportAndStaticImport.xml new file mode 100644 index 000000000000..cde3be6fb0c7 --- /dev/null +++ b/java/java-tests/testData/codeStyle/withoutModulesAndOtherImportAndStaticImport.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file diff --git a/java/java-tests/testData/codeStyle/withoutOtherImportWithModule.xml b/java/java-tests/testData/codeStyle/withoutOtherImportWithModule.xml new file mode 100644 index 000000000000..8979f84d2435 --- /dev/null +++ b/java/java-tests/testData/codeStyle/withoutOtherImportWithModule.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/java/java-tests/testData/codeStyle/withoutStaticImportWithoutModule.xml b/java/java-tests/testData/codeStyle/withoutStaticImportWithoutModule.xml new file mode 100644 index 000000000000..cbfbed74f07b --- /dev/null +++ b/java/java-tests/testData/codeStyle/withoutStaticImportWithoutModule.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/psi/codeStyle/JavaCodeStyleSettingsTest.java b/java/java-tests/testSrc/com/intellij/java/psi/codeStyle/JavaCodeStyleSettingsTest.java index 66e9d7c40c02..43d2f2429fba 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/codeStyle/JavaCodeStyleSettingsTest.java +++ b/java/java-tests/testSrc/com/intellij/java/psi/codeStyle/JavaCodeStyleSettingsTest.java @@ -76,7 +76,7 @@ public class JavaCodeStyleSettingsTest extends CodeStyleTestCase { CodeStyleScheme testScheme = createTestScheme(); final CodeStyleSettings settings = testScheme.getCodeStyleSettings(); final CommonCodeStyleSettings commonJavaSettings = settings.getCommonSettings(JavaLanguage.INSTANCE); - settings.setSoftMargins(JavaLanguage.INSTANCE, Arrays.asList(11,22)); + settings.setSoftMargins(JavaLanguage.INSTANCE, Arrays.asList(11, 22)); commonJavaSettings.METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED; commonJavaSettings.CALL_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM; commonJavaSettings.WRAP_ON_TYPING = CommonCodeStyleSettings.WrapOnTyping.WRAP.intValue; @@ -122,7 +122,7 @@ public class JavaCodeStyleSettingsTest extends CodeStyleTestCase { assertEquals("com.jetbrains.First", repeatAnno.get(0)); assertEquals("com.jetbrains.Second", repeatAnno.get(1)); } - + private static void setSimple(@NotNull AbstractCodeStylePropertyMapper mapper, @NotNull String name, @NotNull String value) { CodeStylePropertyAccessor accessor = mapper.getAccessor(name); assertNotNull(name + " not found", accessor); @@ -154,6 +154,110 @@ public class JavaCodeStyleSettingsTest extends CodeStyleTestCase { assertEquals(7, customSettings.BLANK_LINES_AROUND_FIELD_WITH_ANNOTATIONS); } + public void testWithoutModulesAndOtherImport() throws SchemeImportException { + CodeStyleSettings settings = importSettings(); + + JavaCodeStyleSettings customSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); + + PackageEntryTable table = customSettings.IMPORT_LAYOUT_TABLE; + assertSize(8, table.getEntries()); + + assertEquals(PackageEntry.ALL_MODULE_IMPORTS, table.getEntryAt(0)); + assertEquals(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, table.getEntryAt(1)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(2)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(3)); + assertEquals("javax", table.getEntryAt(4).getPackageName()); + assertEquals("java", table.getEntryAt(5).getPackageName()); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(6)); + assertEquals(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, table.getEntryAt(7)); + } + + public void testWithoutModulesAndOtherImportAndStaticImport() throws SchemeImportException { + CodeStyleSettings settings = importSettings(); + + JavaCodeStyleSettings customSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); + + PackageEntryTable table = customSettings.IMPORT_LAYOUT_TABLE; + assertSize(8, table.getEntries()); + + assertEquals(PackageEntry.ALL_MODULE_IMPORTS, table.getEntryAt(0)); + assertEquals(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, table.getEntryAt(1)); + assertEquals(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, table.getEntryAt(2)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(3)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(4)); + assertEquals("javax", table.getEntryAt(5).getPackageName()); + assertEquals("java", table.getEntryAt(6).getPackageName()); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(7)); + } + + public void testWithoutModules() throws SchemeImportException { + CodeStyleSettings settings = importSettings(); + + JavaCodeStyleSettings customSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); + + PackageEntryTable table = customSettings.IMPORT_LAYOUT_TABLE; + assertSize(8, table.getEntries()); + assertEquals(PackageEntry.ALL_MODULE_IMPORTS, table.getEntryAt(0)); + assertEquals(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, table.getEntryAt(1)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(2)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(3)); + assertEquals("javax", table.getEntryAt(4).getPackageName()); + assertEquals("java", table.getEntryAt(5).getPackageName()); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(6)); + assertEquals(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, table.getEntryAt(7)); + } + + public void testWithoutOtherImportWithModule() throws SchemeImportException { + CodeStyleSettings settings = importSettings(); + + JavaCodeStyleSettings customSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); + + PackageEntryTable table = customSettings.IMPORT_LAYOUT_TABLE; + assertSize(8, table.getEntries()); + assertEquals(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, table.getEntryAt(0)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(1)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(2)); + assertEquals("javax", table.getEntryAt(3).getPackageName()); + assertEquals("java", table.getEntryAt(4).getPackageName()); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(5)); + assertEquals(PackageEntry.ALL_MODULE_IMPORTS, table.getEntryAt(6)); + assertEquals(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, table.getEntryAt(7)); + } + + public void testWithoutStaticImportWithoutModule() throws SchemeImportException { + CodeStyleSettings settings = importSettings(); + + JavaCodeStyleSettings customSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); + + PackageEntryTable table = customSettings.IMPORT_LAYOUT_TABLE; + assertSize(8, table.getEntries()); + + assertEquals(PackageEntry.ALL_MODULE_IMPORTS, table.getEntryAt(0)); + assertEquals(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, table.getEntryAt(1)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(2)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(3)); + assertEquals("javax", table.getEntryAt(4).getPackageName()); + assertEquals("java", table.getEntryAt(5).getPackageName()); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(6)); + assertEquals(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, table.getEntryAt(7)); + } + + public void testEmptyConfigImport() throws SchemeImportException { + CodeStyleSettings settings = importSettings(); + + JavaCodeStyleSettings customSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); + + PackageEntryTable table = customSettings.IMPORT_LAYOUT_TABLE; + assertSize(7, table.getEntries()); + assertEquals(PackageEntry.ALL_MODULE_IMPORTS, table.getEntryAt(0)); + assertEquals(PackageEntry.ALL_OTHER_IMPORTS_ENTRY, table.getEntryAt(1)); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(2)); + assertEquals("javax", table.getEntryAt(3).getPackageName()); + assertEquals("java", table.getEntryAt(4).getPackageName()); + assertEquals(PackageEntry.BLANK_LINE_ENTRY, table.getEntryAt(5)); + assertEquals(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, table.getEntryAt(6)); + } + private static boolean isPrimitiveOrString(Class type) { return type.isPrimitive() || type.equals(String.class); }