diff --git a/platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageAction.java b/platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageAction.java index f59b45680e38..26e266877e97 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageAction.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageAction.java @@ -84,7 +84,7 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar final CreateGroupHandler validator; final String message, title; - if (PsiDirectoryFactory.getInstance(project).isPackage(directory)) { + if (isPackage(project, Collections.singletonList(directory))) { validator = new CreatePackageHandler(project, directory); message = IdeBundle.message("prompt.enter.new.package.name"); title = IdeBundle.message("title.new.package"); @@ -146,14 +146,7 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar presentation.setEnabledAndVisible(true); - boolean isPackage = false; - final PsiDirectoryFactory factory = PsiDirectoryFactory.getInstance(project); - for (PsiDirectory directory : directories) { - if (factory.isPackage(directory)) { - isPackage = true; - break; - } - } + boolean isPackage = isPackage(project, directories); if (isPackage) { presentation.setText(IdeBundle.messagePointer("action.package")); @@ -165,6 +158,16 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar } } + protected boolean isPackage(@NotNull Project project, @NotNull List<@NotNull PsiDirectory> directories) { + final PsiDirectoryFactory factory = PsiDirectoryFactory.getInstance(project); + for (PsiDirectory directory : directories) { + if (factory.isPackage(directory)) { + return true; + } + } + return false; + } + private JBPopup createLightWeightPopup(@Nullable Project project, @NlsContexts.PopupTitle String title, String initialText, diff --git a/plugins/kotlin/kotlin.ide/src/org/jetbrains/kotlin/idea/actions/CreateKotlinAwareDirectoryOrPackageAction.kt b/plugins/kotlin/kotlin.ide/src/org/jetbrains/kotlin/idea/actions/CreateKotlinAwareDirectoryOrPackageAction.kt new file mode 100644 index 000000000000..906aa17ea966 --- /dev/null +++ b/plugins/kotlin/kotlin.ide/src/org/jetbrains/kotlin/idea/actions/CreateKotlinAwareDirectoryOrPackageAction.kt @@ -0,0 +1,18 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.actions + +import com.intellij.ide.actions.CreateDirectoryOrPackageAction +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.psi.PsiDirectory +import org.jetbrains.kotlin.idea.base.util.KOTLIN_AWARE_SOURCE_ROOT_TYPES + +class CreateKotlinAwareDirectoryOrPackageAction: CreateDirectoryOrPackageAction() { + override fun isPackage(project: Project, directories: List): Boolean { + val fileIndex = ProjectRootManager.getInstance(project).getFileIndex() + return directories.any { + val virtualFile = it.getVirtualFile() + fileIndex.isUnderSourceRootOfType(virtualFile, KOTLIN_AWARE_SOURCE_ROOT_TYPES) + } + } +} \ No newline at end of file diff --git a/plugins/kotlin/kotlin.ide/src/org/jetbrains/kotlin/idea/actions/KotlinActionsManager.kt b/plugins/kotlin/kotlin.ide/src/org/jetbrains/kotlin/idea/actions/KotlinActionsManager.kt new file mode 100644 index 000000000000..53987da9c519 --- /dev/null +++ b/plugins/kotlin/kotlin.ide/src/org/jetbrains/kotlin/idea/actions/KotlinActionsManager.kt @@ -0,0 +1,13 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.actions + +import com.intellij.openapi.actionSystem.ex.ActionRuntimeRegistrar +import com.intellij.openapi.actionSystem.impl.ActionConfigurationCustomizer + +private const val NEW_DIR: String = "NewDir" + +class KotlinActionsManager: ActionConfigurationCustomizer, ActionConfigurationCustomizer.LightCustomizeStrategy { + override suspend fun customize(actionRegistrar: ActionRuntimeRegistrar) { + actionRegistrar.replaceAction(NEW_DIR, CreateKotlinAwareDirectoryOrPackageAction()) + } +} \ No newline at end of file diff --git a/plugins/kotlin/plugin/common/resources/META-INF/kotlin-core.xml b/plugins/kotlin/plugin/common/resources/META-INF/kotlin-core.xml index 6abe1b25cedc..a4b59b5b4d71 100644 --- a/plugins/kotlin/plugin/common/resources/META-INF/kotlin-core.xml +++ b/plugins/kotlin/plugin/common/resources/META-INF/kotlin-core.xml @@ -509,6 +509,7 @@ +