User: kyle wood Date: 03 Oct 22 01:44 Revision: e98f08eef9a2ecf49160dbc92f738e24f99d91f5 Summary: Hopefully reduce IDE locking up when opening big NBT files The main cause for the IDE locking up when opening big NBT files isn't mcdev code. IntelliJ's threading model requires a lot of different things, including all writes, to happen on the UI thread. In this commit I move everything I possibly can off the UI thread, but there is still some hanging when the IDE is parsing the NBTT file. Relevant issue: #1893 TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=8142&personal=false Index: src/main/kotlin/nbt/editor/NbtFileEditorProvider.kt =================================================================== --- src/main/kotlin/nbt/editor/NbtFileEditorProvider.kt (revision abf32c6ee060522323f0427c8ddb24e739d2402a) +++ src/main/kotlin/nbt/editor/NbtFileEditorProvider.kt (revision e98f08eef9a2ecf49160dbc92f738e24f99d91f5) @@ -13,12 +13,14 @@ import com.demonwav.mcdev.nbt.NbtVirtualFile import com.demonwav.mcdev.nbt.filetype.NbtFileType import com.demonwav.mcdev.nbt.lang.NbttFile +import com.demonwav.mcdev.util.invokeAndWait import com.demonwav.mcdev.util.invokeLater import com.intellij.ide.actions.SaveAllAction import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.AnActionResult import com.intellij.openapi.actionSystem.ex.AnActionListener +import com.intellij.openapi.application.runReadAction import com.intellij.openapi.command.UndoConfirmationPolicy import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.fileEditor.FileEditor @@ -48,7 +50,11 @@ override fun accept(project: Project, file: VirtualFile) = file.fileType == NbtFileType override fun getPolicy() = FileEditorPolicy.NONE override fun createEditor(project: Project, file: VirtualFile): FileEditor { - val fileEditor = NbtFileEditor(file) { nbtFile -> super.createEditor(project, nbtFile) } + val fileEditor = NbtFileEditor(file) { nbtFile -> + invokeAndWait { + super.createEditor(project, nbtFile) + } + } runAsync { val nbtFile = NbtVirtualFile(file, project) @@ -57,10 +63,8 @@ NonProjectFileWritingAccessProvider.allowWriting(listOf(nbtFile)) } - invokeLater { - fileEditor.ready(nbtFile, project) - } + fileEditor.ready(nbtFile, project) + } - } return fileEditor } @@ -94,7 +98,9 @@ val toolbar = NbtToolbar(nbtFile) nbtFile.toolbar = toolbar - editor = editorProvider(nbtFile) + editor = invokeAndWait { + editorProvider(nbtFile) + } editor?.let { editor -> try { Disposer.register(this, editor) @@ -105,12 +111,17 @@ Disposer.dispose(this) return@let } + invokeLater { - component.add(toolbar.panel, BorderLayout.NORTH) - component.add(editor.component, BorderLayout.CENTER) - } + component.add(toolbar.panel, BorderLayout.NORTH) + component.add(editor.component, BorderLayout.CENTER) + } + } // This can be null if the file is too big to be parsed as a psi file - val psiFile = PsiManager.getInstance(project).findFile(nbtFile) as? NbttFile ?: return + val psiFile = runReadAction { + PsiManager.getInstance(project).findFile(nbtFile) as? NbttFile + } ?: return + WriteCommandAction.writeCommandAction(psiFile) .shouldRecordActionForActiveDocument(false) .withUndoConfirmationPolicy(UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION)