diff --git a/build.gradle.kts b/build.gradle.kts index 96b9088fb..2689dc3ce 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -95,7 +95,9 @@ dependencies { // Kotlin implementation(kotlin("stdlib-jdk8")) implementation(kotlin("reflect")) - implementation(libs.bundles.coroutines) + implementation(libs.bundles.coroutines) { + exclude(module = "kotlinx-coroutines-core-jvm") + } implementation(files(gradleToolingExtensionJar)) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3c20fba51..14af97377 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -40,6 +40,6 @@ junit-entine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junit-platform" } [bundles] -coroutines = ["coroutines-core", "coroutines-jdk8", "coroutines-swing"] +coroutines = ["coroutines-swing"] asm = ["asm", "asm-tree", "asm-analysis"] fuel = ["fuel", "fuel-coroutines"] diff --git a/src/main/kotlin/facet/MinecraftFacet.kt b/src/main/kotlin/facet/MinecraftFacet.kt index 6c38fd206..e42f20b23 100644 --- a/src/main/kotlin/facet/MinecraftFacet.kt +++ b/src/main/kotlin/facet/MinecraftFacet.kt @@ -35,6 +35,7 @@ import com.intellij.facet.FacetTypeId import com.intellij.facet.FacetTypeRegistry import com.intellij.ide.projectView.ProjectView import com.intellij.openapi.application.runReadAction +import com.intellij.openapi.application.runWriteActionAndWait import com.intellij.openapi.module.Module import com.intellij.openapi.module.ModuleGrouper import com.intellij.openapi.module.ModuleManager @@ -122,17 +123,19 @@ class MinecraftFacet( roots.clear() val rootManager = ModuleRootManager.getInstance(module) - rootManager.contentEntries.asSequence() - .flatMap { entry -> entry.sourceFolders.asSequence() } - .filterNotNull { it.file } - .forEach { - when (it.rootType) { - JavaSourceRootType.SOURCE -> roots.put(SourceType.SOURCE, it.file) - JavaSourceRootType.TEST_SOURCE -> roots.put(SourceType.TEST_SOURCE, it.file) - JavaResourceRootType.RESOURCE -> roots.put(SourceType.RESOURCE, it.file) - JavaResourceRootType.TEST_RESOURCE -> roots.put(SourceType.TEST_RESOURCE, it.file) + runWriteActionAndWait { + rootManager.contentEntries.asSequence() + .flatMap { entry -> entry.sourceFolders.asSequence() } + .filterNotNull { it.file } + .forEach { + when (it.rootType) { + JavaSourceRootType.SOURCE -> roots.put(SourceType.SOURCE, it.file) + JavaSourceRootType.TEST_SOURCE -> roots.put(SourceType.TEST_SOURCE, it.file) + JavaResourceRootType.RESOURCE -> roots.put(SourceType.RESOURCE, it.file) + JavaResourceRootType.TEST_RESOURCE -> roots.put(SourceType.TEST_RESOURCE, it.file) + } } - } + } } private fun register(type: AbstractModuleType<*>): AbstractModule { diff --git a/src/main/kotlin/facet/MinecraftFacetDetector.kt b/src/main/kotlin/facet/MinecraftFacetDetector.kt index de618e44b..b1c08f3d7 100644 --- a/src/main/kotlin/facet/MinecraftFacetDetector.kt +++ b/src/main/kotlin/facet/MinecraftFacetDetector.kt @@ -31,6 +31,10 @@ import com.demonwav.mcdev.util.runWriteTaskLater import com.intellij.facet.FacetManager import com.intellij.facet.impl.ui.libraries.LibrariesValidatorContextImpl import com.intellij.framework.library.LibraryVersionProperties +import com.intellij.openapi.application.EDT +import com.intellij.openapi.application.runWriteActionAndWait +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.service import com.intellij.openapi.module.Module import com.intellij.openapi.module.ModuleManager import com.intellij.openapi.project.Project @@ -41,11 +45,17 @@ import com.intellij.openapi.roots.libraries.LibraryDetectionManager import com.intellij.openapi.roots.libraries.LibraryKind import com.intellij.openapi.roots.libraries.LibraryProperties import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager -import com.intellij.openapi.startup.StartupActivity +import com.intellij.openapi.startup.ProjectActivity import com.intellij.openapi.util.Key +import com.intellij.platform.ide.progress.withBackgroundProgress +import com.intellij.platform.util.progress.forEachWithProgress +import com.intellij.util.application +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import org.jetbrains.plugins.gradle.util.GradleUtil -class MinecraftFacetDetector : StartupActivity { +class MinecraftFacetDetector : ProjectActivity { companion object { private val libraryVersionsKey = Key>("mcdev.libraryVersions") @@ -54,10 +64,15 @@ class MinecraftFacetDetector : StartupActivity { } } - override fun runActivity(project: Project) { - MinecraftModuleRootListener.doCheck(project) + override suspend fun execute(project: Project) { + withBackgroundProgress(project, "Detecting Minecraft Frameworks", cancellable = false) { + MinecraftModuleRootListener.doCheck(project) + } } + @Service(Service.Level.PROJECT) + private class FacetDetectorScopeProvider(val scope: CoroutineScope) + private object MinecraftModuleRootListener : ModuleRootListener { override fun rootsChanged(event: ModuleRootEvent) { if (event.isCausedByFileTypesChange) { @@ -65,26 +80,38 @@ class MinecraftFacetDetector : StartupActivity { } val project = event.source as? Project ?: return - doCheck(project) + project.service().scope.launch(Dispatchers.EDT) { + withBackgroundProgress(project, "Detecting Minecraft Frameworks", cancellable = false) { + doCheck(project) + } + } } - fun doCheck(project: Project) { + suspend fun doCheck(project: Project) { val moduleManager = ModuleManager.getInstance(project) var needsReimport = false - for (module in moduleManager.modules) { + moduleManager.modules.asList().forEachWithProgress(false) { module -> val facetManager = FacetManager.getInstance(module) val minecraftFacet = facetManager.getFacetByType(MinecraftFacet.ID) - if (minecraftFacet == null) { - checkNoFacet(module) - } else { - checkExistingFacet(module, minecraftFacet) - if (ProjectReimporter.needsReimport(minecraftFacet)) { - needsReimport = true + val action = { + if (minecraftFacet == null) { + checkNoFacet(module) + } else { + checkExistingFacet(module, minecraftFacet) + if (ProjectReimporter.needsReimport(minecraftFacet)) { + needsReimport = true + } } } + + if (application.isUnitTestMode) { + action() + } else { + runWriteActionAndWait(action) + } } if (needsReimport) {