User: rednesto Date: 07 Dec 23 14:37 Revision: 5855f63090b59770c633d0e2c3b7f1233c039086 Summary: Fix #mcdev-error-report/1567 reimport EDT error TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=8877&personal=false Index: src/main/kotlin/facet/MinecraftFacetDetector.kt =================================================================== --- src/main/kotlin/facet/MinecraftFacetDetector.kt (revision 6a8e547b3def80deb5e2e745928431dd9806a96d) +++ src/main/kotlin/facet/MinecraftFacetDetector.kt (revision 5855f63090b59770c633d0e2c3b7f1233c039086) @@ -115,9 +115,11 @@ } if (needsReimport) { + project.service().scope.launch(Dispatchers.EDT) { - ProjectReimporter.reimport(project) - } - } + ProjectReimporter.reimport(project) + } + } + } private fun checkNoFacet(module: Module) { val platforms = autoDetectTypes(module).ifEmpty { return } Index: src/main/kotlin/facet/ProjectReimporter.kt =================================================================== --- src/main/kotlin/facet/ProjectReimporter.kt (revision 6a8e547b3def80deb5e2e745928431dd9806a96d) +++ src/main/kotlin/facet/ProjectReimporter.kt (revision 5855f63090b59770c633d0e2c3b7f1233c039086) @@ -20,9 +20,13 @@ package com.demonwav.mcdev.facet -import com.intellij.openapi.actionSystem.ActionManager -import com.intellij.openapi.actionSystem.ActionPlaces import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.externalSystem.ExternalSystemManager +import com.intellij.openapi.externalSystem.importing.ImportSpecBuilder +import com.intellij.openapi.externalSystem.model.ProjectSystemId +import com.intellij.openapi.externalSystem.service.project.trusted.ExternalSystemTrustedProjectDialog +import com.intellij.openapi.externalSystem.util.ExternalSystemUtil +import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.module.ModuleManager import com.intellij.openapi.project.Project @@ -38,26 +42,33 @@ fun needsReimport(facet: MinecraftFacet) = facet.configuration.state.projectReimportVersion < CURRENT_REIMPORT_VERSION - fun reimport(project: Project) { + suspend fun reimport(project: Project) { for (module in ModuleManager.getInstance(project).modules) { val minecraftFacet = MinecraftFacet.getInstance(module) ?: continue minecraftFacet.configuration.state.projectReimportVersion = CURRENT_REIMPORT_VERSION } - val refreshAllProjectsAction = ActionManager.getInstance().getAction("ExternalSystem.RefreshAllProjects") - if (refreshAllProjectsAction == null) { - log.error("Could not find refresh all projects action") + val systemIds = getSystemIds() + if (systemIds.isEmpty()) { + log.error("No external system found") return } - val callback = ActionManager.getInstance().tryToExecute( - refreshAllProjectsAction, - null, - null, - ActionPlaces.UNKNOWN, - true - ) - callback.doWhenRejected { error -> - log.error("Rejected refresh all projects: $error") + + // We save all documents because there is a possible case that there is an external system config file changed inside the ide. + FileDocumentManager.getInstance().saveAllDocuments() + + for (externalSystemId in systemIds) { + if (ExternalSystemTrustedProjectDialog.confirmLoadingUntrustedProjectAsync(project, externalSystemId)) { + ExternalSystemUtil.refreshProjects(ImportSpecBuilder(project, externalSystemId)) - } - } -} + } + } + } + + private fun getSystemIds(): List { + val systemIds: MutableList = ArrayList() + ExternalSystemManager.EP_NAME.forEachExtensionSafe { manager: ExternalSystemManager<*, *, *, *, *> -> + systemIds.add(manager.systemId) + } + return systemIds + } +}