From 790607b8e853c55cd509588d091a8f8fad986207 Mon Sep 17 00:00:00 2001 From: Aleksandra Zdrojowa Date: Mon, 20 Jan 2025 17:54:21 +0100 Subject: [PATCH 1/3] NPW: make DefaultModuleContentEntryFolders build tool agnostic - it was already being used for Scala CLI (not just sbt), but the documentation and default parameters were primarily geared towards sbt --- .../template/AbstractArchivedSbtProjectBuilder.scala | 2 +- .../template/DefaultModuleContentEntryFolders.scala | 11 ++++------- .../sbt/project/template/SbtModuleBuilder.scala | 1 + 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala index 80264ea7d5a..4ee1aef5ed2 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala @@ -43,7 +43,7 @@ abstract class AbstractArchivedSbtProjectBuilder extends SbtModuleBuilderBase { testSources = Seq("src/test/scala"), resources = Seq("resources"), testResources = Seq(), - excluded = DefaultModuleContentEntryFolders.RootTargets, + excluded = DefaultModuleContentEntryFolders.SbtRootTargets, )) } diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala index c1a87028593..6c03eb1b072 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala @@ -2,9 +2,9 @@ package org.jetbrains.sbt.project.template /** * Represents set of folders to be marked as "source", "resource", "excluded" folders just after project is created. - * These values will be rewritten after sbt project will be reimported (it will be read from sbt project dumped structure). + * These values will be rewritten after a project is reimported (it will be read from sbt project dumped structure). * So this class contains a "guess" set of folders. - * (Luckily it's quite well known for simple sbt projects: src/main|test/scala, target, project/target) + * (Luckily it's quite well known for simple e.g. sbt projects: src/main|test/scala, target, project/target) * Without this class we would need to wait for the project reimport even to create a simple scala file in sources folder. * * @note all paths are relative to model content root @@ -14,12 +14,9 @@ final case class DefaultModuleContentEntryFolders( testSources: Seq[String], resources: Seq[String], testResources: Seq[String], - excluded: Seq[String] = DefaultModuleContentEntryFolders.RootTargets, + excluded: Seq[String], ) object DefaultModuleContentEntryFolders { - val RootTargets = Seq("target", "project/target") - - val rootTargets: DefaultModuleContentEntryFolders = - DefaultModuleContentEntryFolders(Nil, Nil, Nil, Nil, RootTargets) + val SbtRootTargets: Seq[String] = Seq("target", "project/target") } \ No newline at end of file diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala index 0640864d12a..6efdb109837 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala @@ -225,6 +225,7 @@ object SbtModuleBuilder { Seq(testSourcesPath), Nil, Nil, + DefaultModuleContentEntryFolders.SbtRootTargets )) } else None From 8aef26290802805a863c518bd60d9063f4a7b884 Mon Sep 17 00:00:00 2001 From: Aleksandra Zdrojowa Date: Mon, 20 Jan 2025 18:04:56 +0100 Subject: [PATCH 2/3] NPW: move ModuleBuilderUtil and DefaultModuleContentEntryFolders to the scala module --- .../project/template/AbstractArchivedSbtProjectBuilder.scala | 1 + .../org/jetbrains/sbt/project/template/ModuleBuilderBase.scala | 2 +- .../org/jetbrains/sbt/project/template/SbtModuleBuilder.scala | 2 +- .../project/template/wizard/ScalaCliModuleBuilder.scala | 3 ++- .../project/template/DefaultModuleContentEntryFolders.scala | 2 +- .../plugins/scala}/project/template/ModuleBuilderUtil.scala | 2 +- 6 files changed, 7 insertions(+), 5 deletions(-) rename {sbt/sbt-impl/src/org/jetbrains/sbt => scala/scala-impl/src/org/jetbrains/plugins/scala}/project/template/DefaultModuleContentEntryFolders.scala (94%) rename {sbt/sbt-impl/src/org/jetbrains/sbt => scala/scala-impl/src/org/jetbrains/plugins/scala}/project/template/ModuleBuilderUtil.scala (99%) diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala index 4ee1aef5ed2..527ee94ce34 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/AbstractArchivedSbtProjectBuilder.scala @@ -6,6 +6,7 @@ import com.intellij.openapi.projectRoots.{JavaSdk, SdkTypeId} import com.intellij.platform.templates.github.ZipUtil import org.jetbrains.annotations.ApiStatus import org.jetbrains.plugins.scala.extensions.ObjectExt +import org.jetbrains.plugins.scala.project.template.DefaultModuleContentEntryFolders import org.jetbrains.sbt.project.template.AbstractArchivedSbtProjectBuilder.{replacePatterns, replacePatterns2} import java.io.File diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala index 4a70f0f6340..f99d480d280 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala @@ -13,7 +13,7 @@ import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vfs.{VirtualFile, VirtualFileManager} import com.intellij.psi.PsiManager import org.jetbrains.plugins.scala.extensions.invokeLater -import org.jetbrains.plugins.scala.project.template.FileExt +import org.jetbrains.plugins.scala.project.template.{DefaultModuleContentEntryFolders, FileExt, ModuleBuilderUtil} import org.jetbrains.plugins.scala.util.ScalaPluginUtils import org.jetbrains.sbt.Sbt diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala index 6efdb109837..e667abcc343 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/SbtModuleBuilder.scala @@ -8,7 +8,7 @@ import com.intellij.openapi.util.io.FileUtil import org.jetbrains.annotations.{ApiStatus, NonNls, TestOnly} import org.jetbrains.plugins.scala.ScalaVersion import org.jetbrains.plugins.scala.extensions._ -import org.jetbrains.plugins.scala.project.template.patchProjectLabels +import org.jetbrains.plugins.scala.project.template.{DefaultModuleContentEntryFolders, patchProjectLabels} import org.jetbrains.plugins.scala.project.{ScalaLanguageLevel, Version, Versions} import org.jetbrains.plugins.scala.util.ui.extensions.JComboBoxOps import org.jetbrains.sbt.project.template.wizard.SbtModuleStepLike diff --git a/scala-cli/src/org/jetbrains/scalaCli/project/template/wizard/ScalaCliModuleBuilder.scala b/scala-cli/src/org/jetbrains/scalaCli/project/template/wizard/ScalaCliModuleBuilder.scala index fadb5d2e6fb..bd19b6e5922 100644 --- a/scala-cli/src/org/jetbrains/scalaCli/project/template/wizard/ScalaCliModuleBuilder.scala +++ b/scala-cli/src/org/jetbrains/scalaCli/project/template/wizard/ScalaCliModuleBuilder.scala @@ -6,9 +6,10 @@ import org.jetbrains.plugins.scala.ScalaVersion import org.jetbrains.bsp.BSP import org.jetbrains.bsp.settings.BspProjectSettings import org.jetbrains.plugins.scala.extensions._ +import org.jetbrains.plugins.scala.project.template.DefaultModuleContentEntryFolders import java.io.File -import org.jetbrains.sbt.project.template.{DefaultModuleContentEntryFolders, ModuleBuilderBase, ScalaModuleBuilderSelections} +import org.jetbrains.sbt.project.template.{ModuleBuilderBase, ScalaModuleBuilderSelections} import org.jetbrains.scalaCli.project.ScalaCliProjectUtils class ScalaCliModuleBuilder ( diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/DefaultModuleContentEntryFolders.scala similarity index 94% rename from sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala rename to scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/DefaultModuleContentEntryFolders.scala index 6c03eb1b072..8012c27ff58 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/DefaultModuleContentEntryFolders.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/DefaultModuleContentEntryFolders.scala @@ -1,4 +1,4 @@ -package org.jetbrains.sbt.project.template +package org.jetbrains.plugins.scala.project.template /** * Represents set of folders to be marked as "source", "resource", "excluded" folders just after project is created. diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderUtil.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala similarity index 99% rename from sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderUtil.scala rename to scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala index c9b850210f9..29652705342 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderUtil.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala @@ -1,4 +1,4 @@ -package org.jetbrains.sbt.project.template +package org.jetbrains.plugins.scala.project.template import com.intellij.openapi.externalSystem.ExternalSystemModulePropertyManager import com.intellij.openapi.externalSystem.importing.ImportSpecBuilder From 344cb2d270c145705c21f5731a080d9976f091c4 Mon Sep 17 00:00:00 2001 From: Aleksandra Zdrojowa Date: Mon, 20 Jan 2025 19:29:36 +0100 Subject: [PATCH 3/3] NPW: run #openInEditor in the activity after opening the project #SCL-23454 fixed - in general, this improvement should have been implemented earlier, but some issues on the platform side prevented the problem from being detected. It's done in the similar way in AbstractMavenModuleBuilder --- .../project/template/ModuleBuilderBase.scala | 15 +-------------- .../project/template/ModuleBuilderUtil.scala | 19 +++++++++++++++++++ .../project/template/ScalaModuleBuilder.scala | 17 ++--------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala index f99d480d280..41c5ccf7fd0 100644 --- a/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala +++ b/sbt/sbt-impl/src/org/jetbrains/sbt/project/template/ModuleBuilderBase.scala @@ -1,6 +1,5 @@ package org.jetbrains.sbt.project.template -import com.intellij.ide.util.EditorHelper import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.externalSystem.model.ProjectSystemId @@ -11,8 +10,6 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.roots.ModifiableRootModel import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vfs.{VirtualFile, VirtualFileManager} -import com.intellij.psi.PsiManager -import org.jetbrains.plugins.scala.extensions.invokeLater import org.jetbrains.plugins.scala.project.template.{DefaultModuleContentEntryFolders, FileExt, ModuleBuilderUtil} import org.jetbrains.plugins.scala.util.ScalaPluginUtils import org.jetbrains.sbt.Sbt @@ -80,17 +77,7 @@ abstract class ModuleBuilderBase[T <: ExternalProjectSettings]( else Option(VirtualFileManager.getInstance().findFileByNioPath((contentDir / externalSystemConfigFile).toPath)).toSeq - if (filesToOpen.nonEmpty) { - val psiManager = PsiManager.getInstance(project) - filesToOpen.foreach { file => - Option(psiManager.findFile(file)) - .foreach { psiFile => - invokeLater { - EditorHelper.openInEditor(psiFile) - } - } - } - } + ModuleBuilderUtil.openFilesInEditor(filesToOpen, project) } protected def externalSystemConfigFile: String diff --git a/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala index 29652705342..6852adf892e 100644 --- a/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ModuleBuilderUtil.scala @@ -1,5 +1,6 @@ package org.jetbrains.plugins.scala.project.template +import com.intellij.ide.util.EditorHelper import com.intellij.openapi.externalSystem.ExternalSystemModulePropertyManager import com.intellij.openapi.externalSystem.importing.ImportSpecBuilder import com.intellij.openapi.externalSystem.model.ProjectSystemId @@ -10,9 +11,12 @@ import com.intellij.openapi.externalSystem.settings.{AbstractExternalSystemSetti import com.intellij.openapi.externalSystem.util.{ExternalSystemApiUtil, ExternalSystemUtil} import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.module.Module +import com.intellij.openapi.project.Project import com.intellij.openapi.roots.{ContentEntry, ModifiableRootModel} +import com.intellij.openapi.startup.StartupManager import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vfs.{LocalFileSystem, VirtualFile} +import com.intellij.psi.PsiManager import org.jetbrains.annotations.{ApiStatus, Nullable} import org.jetbrains.jps.model.java.{JavaResourceRootType, JavaSourceRootType} import org.jetbrains.plugins.scala.extensions._ @@ -94,6 +98,21 @@ object ModuleBuilderUtil { } } + def openFilesInEditor(files: Seq[VirtualFile], project: Project): Unit = { + if (files.isEmpty) return + val psiManager = PsiManager.getInstance(project) + StartupManager.getInstance(project).runAfterOpened(() => + files.foreach { file => + val psiFile = inReadAction { psiManager.findFile(file) } + if (psiFile != null) { + invokeLater { + EditorHelper.openInEditor(psiFile) + } + } + } + ) + } + private def doSetupRootModel( model: ModifiableRootModel, vContentRootDir: VirtualFile, diff --git a/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ScalaModuleBuilder.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ScalaModuleBuilder.scala index 3285efa9f05..c769b444ecc 100644 --- a/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ScalaModuleBuilder.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/template/ScalaModuleBuilder.scala @@ -1,7 +1,6 @@ package org.jetbrains.plugins.scala.project.template import com.intellij.facet.impl.ui.libraries.LibraryCompositionSettings -import com.intellij.ide.util.EditorHelper import com.intellij.ide.util.projectWizard.{JavaModuleBuilder, ModuleWizardStep, SettingsStep} import com.intellij.openapi.module.{JavaModuleType, Module} import com.intellij.openapi.project.Project @@ -10,8 +9,6 @@ import com.intellij.openapi.roots.libraries.Library import com.intellij.openapi.roots.ui.configuration.projectRoot.{LibrariesContainer, LibrariesContainerFactory} import com.intellij.openapi.util.Disposer import com.intellij.openapi.vfs.VirtualFile -import com.intellij.psi.PsiManager -import org.jetbrains.plugins.scala.extensions.invokeLater import java.{util => ju} import javax.swing.JComponent @@ -45,19 +42,9 @@ class ScalaModuleBuilder extends JavaModuleBuilder { new ScalaStep(settingsStep) } + //open code sample or buildSbt private def openEditorForCodeSample(project: Project): Unit = - //open code sample or buildSbt - if (openFileEditorAfterProjectOpened.nonEmpty) { - val psiManager = PsiManager.getInstance(project) - openFileEditorAfterProjectOpened.foreach { file => - Option(psiManager.findFile(file)) - .foreach { psiFile => - invokeLater { - EditorHelper.openInEditor(psiFile) - } - } - } - } + ModuleBuilderUtil.openFilesInEditor(openFileEditorAfterProjectOpened, project) private class ScalaStep(settingsStep: SettingsStep) extends ModuleWizardStep with ScalaSDKStepLike { private val javaStep = JavaModuleType.getModuleType.modifyProjectTypeStep(settingsStep, ScalaModuleBuilder.this)