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 67a44b9eca4c..0caf72264f41 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 @@ -186,6 +186,7 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im @Property(externalName = "imports_layout") public PackageEntryTable IMPORT_LAYOUT_TABLE = new PackageEntryTable(); + private boolean updatedModuleImportLayout = false; /** *
@@ -389,7 +390,7 @@ 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));
+    PACKAGES_TO_USE_IMPORT_ON_DEMAND.addEntry(new PackageEntry(false, "javax.swing", false));
     initImportLayout();
   }
 
@@ -499,6 +500,7 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
       }
       if (!ContainerUtil.exists(entries, entry -> entry == PackageEntry.ALL_MODULE_IMPORTS)) {
         IMPORT_LAYOUT_TABLE.insertEntryAt(PackageEntry.ALL_MODULE_IMPORTS, 0);
+        updatedModuleImportLayout = true;
       }
     }
   }
@@ -508,9 +510,34 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
     super.writeExternal(parentElement, parentSettings);
     writeExternalCollection(parentElement, myRepeatAnnotations, REPEAT_ANNOTATIONS, REPEAT_ANNOTATIONS_ITEM);
     writeExternalCollection(parentElement, myDoNotImportInner, DO_NOT_IMPORT_INNER, DO_NOT_IMPORT_INNER_ITEM);
+    //don't change setting file if it was generated and not changed
+    if (updatedModuleImportLayout && IMPORT_LAYOUT_TABLE.getEntries() != null &&
+        IMPORT_LAYOUT_TABLE.getEntries()[0] == PackageEntry.ALL_MODULE_IMPORTS) {
+      deleteFirstModuleFromImportLayoutTable(parentElement);
+    }
     writeVersion(parentElement);
   }
 
+  private void deleteFirstModuleFromImportLayoutTable(Element parentElement) {
+    Element child = parentElement.getChild(getTagName());
+    if (child == null) return;
+    Element table = null;
+    for (Element option : child.getChildren("option")) {
+      if (option.getAttributeValue("name").equals("IMPORT_LAYOUT_TABLE")) {
+        table = option;
+        break;
+      }
+    }
+    if (table == null) return;
+    Element value = table.getChild("value");
+    if (value == null) return;
+    List entries = value.getChildren();
+    if (entries == null || entries.isEmpty()) return;
+    Element firstEntry = entries.get(0);
+    if (!"true".equals(firstEntry.getAttributeValue("module"))) return;
+    firstEntry.detach();
+  }
+
 
   /**
    * Appends {@code version} attribute to the {@code JavaCodeStyleSettings} tag in {@link CodeStyleScheme}
diff --git a/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaPackageEntryTableAccessor.java b/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaPackageEntryTableAccessor.java
index 2f0395ca3bc6..20880c733eca 100644
--- a/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaPackageEntryTableAccessor.java
+++ b/java/java-frontback-impl/src/com/intellij/psi/codeStyle/JavaPackageEntryTableAccessor.java
@@ -18,7 +18,6 @@ public class JavaPackageEntryTableAccessor extends ValueListPropertyAccessor toExternal(@NotNull PackageEntryTable value) {
     List externalList = new ArrayList<>();
-    for (PackageEntry entry : value.getEntries()) {
+    PackageEntry[] entries = value.getEntries();
+    for (int i = 0; i < entries.length; i++) {
+      PackageEntry entry = entries[i];
       if (entry == PackageEntry.BLANK_LINE_ENTRY) {
         externalList.add(String.valueOf(BLANK_LINE_CHAR));
       }
@@ -90,6 +91,11 @@ public class JavaPackageEntryTableAccessor extends ValueListPropertyAccessor
+  
+
\ 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 28d688ed71ed..273d29db4785 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
@@ -1,18 +1,4 @@
-/*
- * Copyright 2000-2017 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
 package com.intellij.java.psi.codeStyle;
 
 import com.intellij.application.options.CodeStyle;
@@ -22,8 +8,11 @@ import com.intellij.ide.codeStyleSettings.CodeStyleTestCase;
 import com.intellij.lang.java.JavaLanguage;
 import com.intellij.openapi.application.ex.PathManagerEx;
 import com.intellij.openapi.options.SchemeImportException;
+import com.intellij.openapi.util.JDOMUtil;
 import com.intellij.psi.codeStyle.*;
 import com.intellij.psi.impl.source.codeStyle.json.CodeStyleSchemeJsonExporter;
+import org.jdom.Element;
+import org.jdom.JDOMException;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -92,6 +81,244 @@ public class JavaCodeStyleSettingsTest extends CodeStyleTestCase {
     compareWithExpected(outputStream.toString(), "json");
   }
 
+  public void testNotFirstImportModule() throws IOException {
+    CodeStyleScheme testScheme = new CodeStyleScheme() {
+
+      @NotNull
+      @Override
+      public String getName() {
+        return "Test";
+      }
+
+      @Override
+      public boolean isDefault() {
+        return false;
+      }
+
+      @NotNull
+      @Override
+      public CodeStyleSettings getCodeStyleSettings() {
+        try {
+          return importSettings();
+        }
+        catch (SchemeImportException e) {
+          throw new RuntimeException(e);
+        }
+      }
+    };
+    CodeStyleSchemeJsonExporter exporter = new CodeStyleSchemeJsonExporter();
+    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+    exporter.exportScheme(testScheme, outputStream, Collections.singletonList("java"));
+    compareWithExpected(outputStream.toString(), "json");
+  }
+
+  public void testFirstNotImportedImportModule() throws IOException, JDOMException {
+    CodeStyleSettings originalRoot = CodeStyle.getSettings(getProject());
+    JavaCodeStyleSettings settings = originalRoot.getCustomSettings(JavaCodeStyleSettings.class);
+    String text = """
+      
+        
+          
+        
+      
+      """;
+    settings.readExternal(JDOMUtil.load(text));
+
+    Element root = new Element("root");
+    PackageEntry moduleEntry = settings.IMPORT_LAYOUT_TABLE.getEntryAt(0);
+    assertSame(PackageEntry.ALL_MODULE_IMPORTS, moduleEntry);
+    settings.IMPORT_LAYOUT_TABLE.addEntry(new PackageEntry(true, "org.foo", true));
+    settings.writeExternal(root, new JavaCodeStyleSettings(originalRoot));
+    String actual = """
+      
+        
+          
+        
+      """;
+    assertEquals(actual, JDOMUtil.writeElement(root));
+  }
+
+  public void testMovedFirstNotImportedImportModule() throws IOException, JDOMException {
+    CodeStyleSettings originalRoot = CodeStyle.getSettings(getProject());
+    JavaCodeStyleSettings settings = originalRoot.getCustomSettings(JavaCodeStyleSettings.class);
+    String text = """
+      
+        
+          
+        
+      
+      """;
+    settings.readExternal(JDOMUtil.load(text));
+
+    Element root = new Element("root");
+    PackageEntry moduleEntry = settings.IMPORT_LAYOUT_TABLE.getEntryAt(0);
+    assertSame(PackageEntry.ALL_MODULE_IMPORTS, moduleEntry);
+    settings.IMPORT_LAYOUT_TABLE.removeEntryAt(0);
+    settings.IMPORT_LAYOUT_TABLE.insertEntryAt(moduleEntry, 1);
+
+    settings.writeExternal(root, new JavaCodeStyleSettings(originalRoot));
+    String actual = """
+      
+        
+          
+        
+      """;
+    assertEquals(actual, JDOMUtil.writeElement(root));
+  }
+
+  public void testMovedFirstImportedImportModule() throws IOException, JDOMException {
+    CodeStyleSettings originalRoot = CodeStyle.getSettings(getProject());
+    JavaCodeStyleSettings settings = originalRoot.getCustomSettings(JavaCodeStyleSettings.class);
+    String text = """
+      
+        
+          
+        
+      
+      """;
+    settings.readExternal(JDOMUtil.load(text));
+
+    Element root = new Element("root");
+    PackageEntry moduleEntry = settings.IMPORT_LAYOUT_TABLE.getEntryAt(0);
+    assertSame(PackageEntry.ALL_MODULE_IMPORTS, moduleEntry);
+    settings.IMPORT_LAYOUT_TABLE.removeEntryAt(0);
+    settings.IMPORT_LAYOUT_TABLE.insertEntryAt(moduleEntry, 1);
+
+    settings.writeExternal(root, new JavaCodeStyleSettings(originalRoot));
+    String actual = """
+      
+        
+          
+        
+      """;
+    assertEquals(actual, JDOMUtil.writeElement(root));
+  }
+
+  public void testMovedNotFirstImportedImportModule() throws IOException, JDOMException {
+    CodeStyleSettings originalRoot = CodeStyle.getSettings(getProject());
+    JavaCodeStyleSettings settings = originalRoot.getCustomSettings(JavaCodeStyleSettings.class);
+    String text = """
+      
+        
+          
+        
+      
+      """;
+    settings.readExternal(JDOMUtil.load(text));
+
+    Element root = new Element("root");
+    PackageEntry notModuleEntry = settings.IMPORT_LAYOUT_TABLE.getEntryAt(0);
+    assertNotSame(PackageEntry.ALL_MODULE_IMPORTS, notModuleEntry);
+    settings.IMPORT_LAYOUT_TABLE.removeEntryAt(0);
+    settings.IMPORT_LAYOUT_TABLE.insertEntryAt(notModuleEntry, 1);
+
+    settings.writeExternal(root, new JavaCodeStyleSettings(originalRoot));
+    String actual = """
+      
+        
+          
+        
+      """;
+    assertEquals(actual, JDOMUtil.writeElement(root));
+  }
+
+  public void testFirstImportedImportModule() throws IOException, JDOMException {
+    CodeStyleSettings originalRoot = CodeStyle.getSettings(getProject());
+    JavaCodeStyleSettings settings = originalRoot.getCustomSettings(JavaCodeStyleSettings.class);
+    String text = """
+      
+        
+          
+        
+      
+      """;
+    settings.readExternal(JDOMUtil.load(text));
+
+    Element root = new Element("root");
+    PackageEntryTable table = settings.IMPORT_LAYOUT_TABLE;
+    assertSize(4,table.getEntries());
+    PackageEntry moduleEntry = table.getEntryAt(0);
+    assertSame(PackageEntry.ALL_MODULE_IMPORTS, moduleEntry);
+    table.addEntry(new PackageEntry(true, "org.foo", true));
+    settings.writeExternal(root, new JavaCodeStyleSettings(originalRoot));
+    String actual = """
+      
+        
+          
+        
+      """;
+    assertEquals(actual, JDOMUtil.writeElement(root));
+  }
+
   public void testSetProperties() {
     final CodeStyleSettings settings = getCurrentCodeStyleSettings();
     AbstractCodeStylePropertyMapper mapper =