Skip to content

Commit

Permalink
Fix #mcdev-error-report/1567 reimport EDT error
Browse files Browse the repository at this point in the history
  • Loading branch information
RedNesto committed Dec 7, 2023
1 parent 6a8e547 commit 5855f63
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/main/kotlin/facet/MinecraftFacetDetector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class MinecraftFacetDetector : ProjectActivity {
}

if (needsReimport) {
ProjectReimporter.reimport(project)
project.service<FacetDetectorScopeProvider>().scope.launch(Dispatchers.EDT) {
ProjectReimporter.reimport(project)
}
}
}

Expand Down
41 changes: 26 additions & 15 deletions src/main/kotlin/facet/ProjectReimporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -38,26 +42,33 @@ object ProjectReimporter {
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<ProjectSystemId> {
val systemIds: MutableList<ProjectSystemId> = ArrayList()
ExternalSystemManager.EP_NAME.forEachExtensionSafe { manager: ExternalSystemManager<*, *, *, *, *> ->
systemIds.add(manager.systemId)
}
return systemIds
}
}

0 comments on commit 5855f63

Please sign in to comment.