Skip to content

Commit d411716

Browse files
committed
Fix slow operations errors in local templates
1 parent 84af973 commit d411716

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import com.intellij.openapi.progress.ProgressIndicator
3333
import com.intellij.ui.dsl.builder.bindSelected
3434
import com.intellij.ui.dsl.builder.panel
3535
import javax.swing.JComponent
36+
import kotlinx.coroutines.Dispatchers
37+
import kotlinx.coroutines.withContext
3638

3739
class BuiltinTemplateProvider : RemoteTemplateProvider() {
3840

@@ -59,16 +61,16 @@ class BuiltinTemplateProvider : RemoteTemplateProvider() {
5961
override suspend fun loadTemplates(
6062
context: WizardContext,
6163
repo: MinecraftSettings.TemplateRepo
62-
): Collection<LoadedTemplate> {
64+
): Collection<LoadedTemplate> = withContext(Dispatchers.IO) {
6365
val remoteTemplates = doLoadTemplates(context, repo, builtinTemplatesInnerPath)
6466
if (remoteTemplates.isNotEmpty()) {
65-
return remoteTemplates
67+
return@withContext remoteTemplates
6668
}
6769

6870
val repoRoot = builtinTemplatesPath.virtualFile
69-
?: return emptyList()
71+
?: return@withContext emptyList()
7072
repoRoot.refreshSync(context.modalityState)
71-
return TemplateProvider.findTemplates(context.modalityState, repoRoot)
73+
TemplateProvider.findTemplates(context.modalityState, repoRoot)
7274
}
7375

7476
override fun setupConfigUi(

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import com.intellij.ui.dsl.builder.textValidation
3939
import java.nio.file.Path
4040
import javax.swing.JComponent
4141
import kotlin.io.path.absolute
42+
import kotlinx.coroutines.Dispatchers
43+
import kotlinx.coroutines.withContext
4244

4345
class LocalTemplateProvider : TemplateProvider {
4446

@@ -49,13 +51,13 @@ class LocalTemplateProvider : TemplateProvider {
4951
override suspend fun loadTemplates(
5052
context: WizardContext,
5153
repo: MinecraftSettings.TemplateRepo
52-
): Collection<LoadedTemplate> {
54+
): Collection<LoadedTemplate> = withContext(Dispatchers.IO) {
5355
val rootPath = Path.of(repo.data.trim()).absolute()
5456
val repoRoot = rootPath.virtualFile
55-
?: return emptyList()
57+
?: return@withContext emptyList()
5658
val modalityState = context.modalityState
5759
repoRoot.refreshSync(modalityState)
58-
return TemplateProvider.findTemplates(modalityState, repoRoot)
60+
TemplateProvider.findTemplates(modalityState, repoRoot)
5961
}
6062

6163
override fun setupConfigUi(

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import com.intellij.util.xmlb.annotations.Attribute
4747
import java.util.ResourceBundle
4848
import java.util.concurrent.TimeUnit
4949
import javax.swing.JComponent
50+
import kotlinx.coroutines.Dispatchers
51+
import kotlinx.coroutines.withContext
5052
import org.jetbrains.concurrency.runAsync
5153

5254
/**
@@ -75,12 +77,12 @@ interface TemplateProvider {
7577

7678
fun getAllKeys() = EP_NAME.extensionList.mapNotNull { it.key }
7779

78-
fun findTemplates(
80+
suspend fun findTemplates(
7981
modalityState: ModalityState,
8082
repoRoot: VirtualFile,
8183
templates: MutableList<VfsLoadedTemplate> = mutableListOf(),
8284
bundle: ResourceBundle? = loadMessagesBundle(modalityState, repoRoot)
83-
): List<VfsLoadedTemplate> {
85+
): List<VfsLoadedTemplate> = withContext(Dispatchers.IO) {
8486
val templatesToLoad = mutableListOf<VirtualFile>()
8587
val visitor = object : VirtualFileVisitor<Unit>() {
8688
override fun visitFile(file: VirtualFile): Boolean {
@@ -113,7 +115,7 @@ interface TemplateProvider {
113115
}
114116
}
115117

116-
return templates
118+
templates
117119
}
118120

119121
fun loadMessagesBundle(modalityState: ModalityState, repoRoot: VirtualFile): ResourceBundle? = try {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import com.intellij.ui.dsl.builder.textValidation
3838
import java.nio.file.Path
3939
import javax.swing.JComponent
4040
import kotlin.io.path.isRegularFile
41+
import kotlinx.coroutines.Dispatchers
42+
import kotlinx.coroutines.withContext
4143

4244
class ZipTemplateProvider : TemplateProvider {
4345

@@ -48,14 +50,14 @@ class ZipTemplateProvider : TemplateProvider {
4850
override suspend fun loadTemplates(
4951
context: WizardContext,
5052
repo: MinecraftSettings.TemplateRepo
51-
): Collection<LoadedTemplate> {
53+
): Collection<LoadedTemplate> = withContext(Dispatchers.IO) {
5254
val archiveRoot = repo.data + JarFileSystem.JAR_SEPARATOR
5355
val fs = JarFileSystem.getInstance()
5456
val rootFile = fs.refreshAndFindFileByPath(archiveRoot)
55-
?: return emptyList()
57+
?: return@withContext emptyList()
5658
val modalityState = context.modalityState
5759
rootFile.refreshSync(modalityState)
58-
return TemplateProvider.findTemplates(modalityState, rootFile)
60+
TemplateProvider.findTemplates(modalityState, rootFile)
5961
}
6062

6163
override fun setupConfigUi(

0 commit comments

Comments
 (0)