User: kyle wood Date: 14 Nov 25 03:09 Revision: d1a84a9f193a97ba43ef100ff0ccce9dd4ff9e08 Summary: Always refresh files in write context Fixes: MCDEV-D TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10277&personal=false Index: src/main/kotlin/creator/custom/providers/RemoteTemplateProvider.kt =================================================================== --- src/main/kotlin/creator/custom/providers/RemoteTemplateProvider.kt (revision c4824ee26a7e8d79ef46bb092bba7cef73c9bcb4) +++ src/main/kotlin/creator/custom/providers/RemoteTemplateProvider.kt (revision d1a84a9f193a97ba43ef100ff0ccce9dd4ff9e08) @@ -36,7 +36,6 @@ import com.github.kittinunf.result.onError import com.intellij.ide.util.projectWizard.WizardContext import com.intellij.openapi.application.PathManager -import com.intellij.openapi.application.writeAction import com.intellij.openapi.diagnostic.ControlFlowException import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.observable.properties.PropertyGraph @@ -161,7 +160,7 @@ val rootFile = fs.refreshAndFindFileByPath(archiveRoot) ?: return emptyList() val modalityState = context.modalityState - writeAction { rootFile.refreshSync(modalityState) } + rootFile.refreshSync(modalityState) val innerPath = replaceVariables(rawInnerPath) val repoRoot = if (innerPath.isNotBlank()) { Index: src/main/kotlin/creator/custom/providers/TemplateProvider.kt =================================================================== --- src/main/kotlin/creator/custom/providers/TemplateProvider.kt (revision c4824ee26a7e8d79ef46bb092bba7cef73c9bcb4) +++ src/main/kotlin/creator/custom/providers/TemplateProvider.kt (revision d1a84a9f193a97ba43ef100ff0ccce9dd4ff9e08) @@ -46,6 +46,8 @@ import com.intellij.util.xmlb.annotations.Attribute import java.util.ResourceBundle import javax.swing.JComponent +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking /** * Extensions responsible for creating a [TemplateDescriptor] based on whatever data it is provided in its configuration @@ -73,33 +75,36 @@ fun getAllKeys() = EP_NAME.extensionList.mapNotNull { it.key } - fun findTemplates( + suspend fun findTemplates( modalityState: ModalityState, repoRoot: VirtualFile, templates: MutableList = mutableListOf(), - bundle: ResourceBundle? = loadMessagesBundle(modalityState, repoRoot) + bundle: ResourceBundle? = null ): List { + val bundle = bundle ?: loadMessagesBundle(modalityState, repoRoot) val visitor = object : VirtualFileVisitor() { override fun visitFile(file: VirtualFile): Boolean { if (!file.isFile || !file.name.endsWith(".mcdev.template.json")) { return true } + runBlocking(Dispatchers.Unconfined) { - try { - createVfsLoadedTemplate(modalityState, file.parent, file, bundle = bundle) - ?.let(templates::add) - } catch (t: Throwable) { - if (t is ControlFlowException) { - throw t - } + try { + createVfsLoadedTemplate(modalityState, file.parent, file, bundle = bundle) + ?.let(templates::add) + } catch (t: Throwable) { + if (t is ControlFlowException) { + throw t + } - val attachment = runCatching { Attachment(file.name, file.readText()) }.getOrNull() - if (attachment != null) { - thisLogger().error("Failed to load template ${file.path}", t, attachment) - } else { - thisLogger().error("Failed to load template ${file.path}", t) - } - } + val attachment = runCatching { Attachment(file.name, file.readText()) }.getOrNull() + if (attachment != null) { + thisLogger().error("Failed to load template ${file.path}", t, attachment) + } else { + thisLogger().error("Failed to load template ${file.path}", t) + } + } + } return true } @@ -108,7 +113,7 @@ return templates } - fun loadMessagesBundle(modalityState: ModalityState, repoRoot: VirtualFile): ResourceBundle? = try { + suspend fun loadMessagesBundle(modalityState: ModalityState, repoRoot: VirtualFile): ResourceBundle? = try { val locale = DynamicBundle.getLocale() // Simplified bundle resolution, but covers all the most common cases val baseBundle = doLoadMessageBundle( @@ -135,7 +140,7 @@ null } - private fun doLoadMessageBundle( + private suspend fun doLoadMessageBundle( file: VirtualFile?, modalityState: ModalityState, parent: ResourceBundle? @@ -158,7 +163,7 @@ return parent } - fun createVfsLoadedTemplate( + suspend fun createVfsLoadedTemplate( modalityState: ModalityState, templateRoot: VirtualFile, descriptorFile: VirtualFile, Index: src/main/kotlin/util/files.kt =================================================================== --- src/main/kotlin/util/files.kt (revision c4824ee26a7e8d79ef46bb092bba7cef73c9bcb4) +++ src/main/kotlin/util/files.kt (revision d1a84a9f193a97ba43ef100ff0ccce9dd4ff9e08) @@ -20,7 +20,9 @@ package com.demonwav.mcdev.util +import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.application.writeAction import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile @@ -44,7 +46,7 @@ val VirtualFile.manifest: Manifest? get() = try { JarFile(localFile).use { it.manifest } - } catch (e: IOException) { + } catch (_: IOException) { null } @@ -77,7 +79,18 @@ operator fun Manifest.get(attribute: String): String? = mainAttributes.getValue(attribute) operator fun Manifest.get(attribute: Attributes.Name): String? = mainAttributes.getValue(attribute) -fun VirtualFile.refreshSync(modalityState: ModalityState): VirtualFile? { +suspend fun VirtualFile.refreshSync(modalityState: ModalityState): VirtualFile? { + fun refresh() { - RefreshQueue.getInstance().refresh(false, this.isDirectory, null, modalityState, this) + RefreshQueue.getInstance().refresh(false, this.isDirectory, null, modalityState, this) + } + + if (ApplicationManager.getApplication().isWriteAccessAllowed) { + refresh() + } else { + writeAction { + refresh() + } + } + return this.parent?.findOrCreateChildData(this, this.name) }