Skip to content

Commit d1a84a9

Browse files
committed
Always refresh files in write context
Fixes: MCDEV-D
1 parent c4824ee commit d1a84a9

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/main/kotlin/creator/custom/providers/RemoteTemplateProvider.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import com.github.kittinunf.result.getOrNull
3636
import com.github.kittinunf.result.onError
3737
import com.intellij.ide.util.projectWizard.WizardContext
3838
import com.intellij.openapi.application.PathManager
39-
import com.intellij.openapi.application.writeAction
4039
import com.intellij.openapi.diagnostic.ControlFlowException
4140
import com.intellij.openapi.diagnostic.thisLogger
4241
import com.intellij.openapi.observable.properties.PropertyGraph
@@ -161,7 +160,7 @@ open class RemoteTemplateProvider : TemplateProvider {
161160
val rootFile = fs.refreshAndFindFileByPath(archiveRoot)
162161
?: return emptyList()
163162
val modalityState = context.modalityState
164-
writeAction { rootFile.refreshSync(modalityState) }
163+
rootFile.refreshSync(modalityState)
165164

166165
val innerPath = replaceVariables(rawInnerPath)
167166
val repoRoot = if (innerPath.isNotBlank()) {

src/main/kotlin/creator/custom/providers/TemplateProvider.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import com.intellij.util.KeyedLazyInstance
4646
import com.intellij.util.xmlb.annotations.Attribute
4747
import java.util.ResourceBundle
4848
import javax.swing.JComponent
49+
import kotlinx.coroutines.Dispatchers
50+
import kotlinx.coroutines.runBlocking
4951

5052
/**
5153
* Extensions responsible for creating a [TemplateDescriptor] based on whatever data it is provided in its configuration
@@ -73,31 +75,34 @@ interface TemplateProvider {
7375

7476
fun getAllKeys() = EP_NAME.extensionList.mapNotNull { it.key }
7577

76-
fun findTemplates(
78+
suspend fun findTemplates(
7779
modalityState: ModalityState,
7880
repoRoot: VirtualFile,
7981
templates: MutableList<VfsLoadedTemplate> = mutableListOf(),
80-
bundle: ResourceBundle? = loadMessagesBundle(modalityState, repoRoot)
82+
bundle: ResourceBundle? = null
8183
): List<VfsLoadedTemplate> {
84+
val bundle = bundle ?: loadMessagesBundle(modalityState, repoRoot)
8285
val visitor = object : VirtualFileVisitor<Unit>() {
8386
override fun visitFile(file: VirtualFile): Boolean {
8487
if (!file.isFile || !file.name.endsWith(".mcdev.template.json")) {
8588
return true
8689
}
8790

88-
try {
89-
createVfsLoadedTemplate(modalityState, file.parent, file, bundle = bundle)
90-
?.let(templates::add)
91-
} catch (t: Throwable) {
92-
if (t is ControlFlowException) {
93-
throw t
94-
}
95-
96-
val attachment = runCatching { Attachment(file.name, file.readText()) }.getOrNull()
97-
if (attachment != null) {
98-
thisLogger().error("Failed to load template ${file.path}", t, attachment)
99-
} else {
100-
thisLogger().error("Failed to load template ${file.path}", t)
91+
runBlocking(Dispatchers.Unconfined) {
92+
try {
93+
createVfsLoadedTemplate(modalityState, file.parent, file, bundle = bundle)
94+
?.let(templates::add)
95+
} catch (t: Throwable) {
96+
if (t is ControlFlowException) {
97+
throw t
98+
}
99+
100+
val attachment = runCatching { Attachment(file.name, file.readText()) }.getOrNull()
101+
if (attachment != null) {
102+
thisLogger().error("Failed to load template ${file.path}", t, attachment)
103+
} else {
104+
thisLogger().error("Failed to load template ${file.path}", t)
105+
}
101106
}
102107
}
103108

@@ -108,7 +113,7 @@ interface TemplateProvider {
108113
return templates
109114
}
110115

111-
fun loadMessagesBundle(modalityState: ModalityState, repoRoot: VirtualFile): ResourceBundle? = try {
116+
suspend fun loadMessagesBundle(modalityState: ModalityState, repoRoot: VirtualFile): ResourceBundle? = try {
112117
val locale = DynamicBundle.getLocale()
113118
// Simplified bundle resolution, but covers all the most common cases
114119
val baseBundle = doLoadMessageBundle(
@@ -135,7 +140,7 @@ interface TemplateProvider {
135140
null
136141
}
137142

138-
private fun doLoadMessageBundle(
143+
private suspend fun doLoadMessageBundle(
139144
file: VirtualFile?,
140145
modalityState: ModalityState,
141146
parent: ResourceBundle?
@@ -158,7 +163,7 @@ interface TemplateProvider {
158163
return parent
159164
}
160165

161-
fun createVfsLoadedTemplate(
166+
suspend fun createVfsLoadedTemplate(
162167
modalityState: ModalityState,
163168
templateRoot: VirtualFile,
164169
descriptorFile: VirtualFile,

src/main/kotlin/util/files.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
package com.demonwav.mcdev.util
2222

23+
import com.intellij.openapi.application.ApplicationManager
2324
import com.intellij.openapi.application.ModalityState
25+
import com.intellij.openapi.application.writeAction
2426
import com.intellij.openapi.vfs.LocalFileSystem
2527
import com.intellij.openapi.vfs.VfsUtilCore
2628
import com.intellij.openapi.vfs.VirtualFile
@@ -44,7 +46,7 @@ val Path.virtualFileOrError: VirtualFile
4446
val VirtualFile.manifest: Manifest?
4547
get() = try {
4648
JarFile(localFile).use { it.manifest }
47-
} catch (e: IOException) {
49+
} catch (_: IOException) {
4850
null
4951
}
5052

@@ -77,7 +79,18 @@ val VirtualFile.mcDomainAndPath: Pair<String, String>?
7779
operator fun Manifest.get(attribute: String): String? = mainAttributes.getValue(attribute)
7880
operator fun Manifest.get(attribute: Attributes.Name): String? = mainAttributes.getValue(attribute)
7981

80-
fun VirtualFile.refreshSync(modalityState: ModalityState): VirtualFile? {
81-
RefreshQueue.getInstance().refresh(false, this.isDirectory, null, modalityState, this)
82+
suspend fun VirtualFile.refreshSync(modalityState: ModalityState): VirtualFile? {
83+
fun refresh() {
84+
RefreshQueue.getInstance().refresh(false, this.isDirectory, null, modalityState, this)
85+
}
86+
87+
if (ApplicationManager.getApplication().isWriteAccessAllowed) {
88+
refresh()
89+
} else {
90+
writeAction {
91+
refresh()
92+
}
93+
}
94+
8295
return this.parent?.findOrCreateChildData(this, this.name)
8396
}

0 commit comments

Comments
 (0)