Skip to content

Commit

Permalink
feat(#161): Use async wrapper for editor build to avoid internal api …
Browse files Browse the repository at this point in the history
…usage
  • Loading branch information
timo-reymann committed Oct 6, 2023
1 parent af7c7ea commit e41f3a8
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 31 deletions.
3 changes: 0 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package de.timo_reymann.mjml_support.editor.provider

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.*
import com.intellij.openapi.progress.runBlockingMaybeCancellable
import com.intellij.openapi.progress.runWithModalProgressBlocking
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import de.timo_reymann.mjml_support.bundle.MjmlBundle
import de.timo_reymann.mjml_support.editor.ui.MjmlFileEditorState
import org.jdom.Element

Expand Down Expand Up @@ -90,34 +86,14 @@ abstract class SplitTextEditorProvider(
private const val SECOND_EDITOR = "second_editor"
private const val SPLIT_LAYOUT = "split_layout"

private fun runsInProjectView() = !ApplicationManager.getApplication().isDispatchThread
private fun runsInEDT() = ApplicationManager.getApplication().isWriteAccessAllowed

fun getBuilderFromEditorProvider(
provider: FileEditorProvider, project: Project, file: VirtualFile
): AsyncFileEditorProvider.Builder {
// in case the provider doesn't provide async functionality -> wrap and create editor
if (provider !is AsyncFileEditorProvider) {
return object : AsyncFileEditorProvider.Builder() {
override fun build(): FileEditor {
return provider.createEditor(project, file)
}
}
}

// called with write context
if (runsInProjectView() || runsInEDT()) {
return runBlockingMaybeCancellable {
provider.createEditorBuilder(project, file)
return object : AsyncFileEditorProvider.Builder() {
override fun build(): FileEditor {
return provider.createEditor(project, file)
}
}

// called from a context without write lock
return runWithModalProgressBlocking(
project, MjmlBundle.message("mjml_preview.opening_editor", file.name)
) {
provider.createEditorBuilder(project, file)
}
}
}
}

0 comments on commit e41f3a8

Please sign in to comment.