@@ -33,6 +33,7 @@ import com.intellij.ide.wizard.NewProjectWizardBaseData
3333import com.intellij.ide.wizard.NewProjectWizardStep
3434import com.intellij.openapi.application.EDT
3535import com.intellij.openapi.application.asContextElement
36+ import com.intellij.openapi.application.runWriteAction
3637import com.intellij.openapi.diagnostic.logger
3738import com.intellij.openapi.observable.properties.GraphProperty
3839import com.intellij.openapi.observable.util.transform
@@ -54,7 +55,6 @@ import kotlinx.coroutines.Dispatchers
5455import kotlinx.coroutines.Job
5556import kotlinx.coroutines.cancel
5657import kotlinx.coroutines.launch
57- import kotlinx.coroutines.withContext
5858
5959/* *
6060 * The step to select a custom template repo.
@@ -219,13 +219,15 @@ class CustomPlatformStep(
219219 templateProvidersTextProperty.set(MCDevBundle (" creator.step.generic.init_template_providers.message" ))
220220 templateProvidersLoadingProperty.set(true )
221221
222+ // For some reason syncRefresh doesn't play nice with writeAction() coroutines so we do it beforehand
223+ application.invokeAndWait(
224+ { runWriteAction { VirtualFileManager .getInstance().syncRefresh() } },
225+ context.modalityState
226+ )
227+
222228 val dialogCoroutineContext = context.modalityState.asContextElement()
223229 val uiContext = dialogCoroutineContext + Dispatchers .EDT
224- creatorUiScope.launch(dialogCoroutineContext) {
225- withContext(uiContext) {
226- application.runWriteAction { VirtualFileManager .getInstance().syncRefresh() }
227- }
228-
230+ creatorUiScope.launch(uiContext) {
229231 for ((providerKey, repos) in templateRepos.groupBy { it.provider }) {
230232 val provider = TemplateProvider .get(providerKey)
231233 ? : continue
@@ -234,11 +236,9 @@ class CustomPlatformStep(
234236 .getOrLogException(logger<CustomPlatformStep >())
235237 }
236238
237- withContext(uiContext) {
238- templateProvidersLoadingProperty.set(false )
239- // Force refresh to trigger template loading
240- templateRepoProperty.set(templateRepo)
241- }
239+ templateProvidersLoadingProperty.set(false )
240+ // Force refresh to trigger template loading
241+ templateRepoProperty.set(templateRepo)
242242 }
243243 }
244244
@@ -248,23 +248,23 @@ class CustomPlatformStep(
248248 templateLoadingTextProperty.set(MCDevBundle (" creator.step.generic.load_template.message" ))
249249 templateLoadingProperty.set(true )
250250
251+ // For some reason syncRefresh doesn't play nice with writeAction() coroutines so we do it beforehand
252+ application.invokeAndWait(
253+ { runWriteAction { VirtualFileManager .getInstance().syncRefresh() } },
254+ context.modalityState
255+ )
256+
251257 val dialogCoroutineContext = context.modalityState.asContextElement()
252258 val uiContext = dialogCoroutineContext + Dispatchers .EDT
253259 templateLoadingJob?.cancel(" Another template has been selected" )
254- templateLoadingJob = creatorUiScope.launch(dialogCoroutineContext) {
255- withContext(uiContext) {
256- application.runWriteAction { VirtualFileManager .getInstance().syncRefresh() }
257- }
258-
260+ templateLoadingJob = creatorUiScope.launch(uiContext) {
259261 val newTemplates = runCatching { provider() }
260262 .getOrLogException(logger<CustomPlatformStep >())
261263 ? : emptyList()
262264
263- withContext(uiContext) {
264- templateLoadingProperty.set(false )
265- noTemplatesAvailable.visible(newTemplates.isEmpty())
266- availableTemplates = newTemplates
267- }
265+ templateLoadingProperty.set(false )
266+ noTemplatesAvailable.visible(newTemplates.isEmpty())
267+ availableTemplates = newTemplates
268268 }
269269 }
270270
0 commit comments