[java-imports] IDEA-368382 incorrect place for module import

(cherry picked from commit ce6ec1c65d5777e5e3059093fdd01baf24f95e50)

IJ-CR-156496

GitOrigin-RevId: 4e3ae37de77005802a25b2f85de3dfa43ea4a2bf
This commit is contained in:
Mikhail Pyltsin
2025-03-01 01:05:41 +01:00
committed by intellij-monorepo-bot
parent 9b1f9cdb7b
commit 8ce55bb2be
8 changed files with 225 additions and 4 deletions

View File

@@ -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);
}
}
}