Use ModalityState.any() for J2K post processing

#KT-17870 fixed
This commit is contained in:
Simon Ogorodnik
2017-05-12 17:53:50 +03:00
parent 861cac5b52
commit a0a6ef4f2e
2 changed files with 43 additions and 34 deletions
@@ -17,7 +17,9 @@
package org.jetbrains.kotlin.idea.core.util
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import kotlinx.coroutines.experimental.CoroutineDispatcher
import kotlin.coroutines.experimental.AbstractCoroutineContextElement
import kotlin.coroutines.experimental.CoroutineContext
@@ -27,6 +29,11 @@ public object EDT : CoroutineDispatcher() {
}
override fun dispatch(context: CoroutineContext, block: Runnable) {
ApplicationManager.getApplication().invokeLater(block)
val modalityState = context[ModalityStateElement.Key]?.modalityState ?: ModalityState.defaultModalityState()
ApplicationManager.getApplication().invokeLater(block, modalityState)
}
class ModalityStateElement(val modalityState: ModalityState) : AbstractCoroutineContextElement(Key) {
companion object Key : CoroutineContext.Key<ModalityStateElement>
}
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea.j2k
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
@@ -55,46 +56,47 @@ class J2kPostProcessor(private val formatCode: Boolean) : PostProcessor {
PROCESS
}
override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) = runBlocking {
do {
var modificationStamp: Long? = file.modificationStamp
val elementToActions = runReadAction {
collectAvailableActions(file, rangeMarker)
}
run(EDT) {
for ((element, action, _) in elementToActions) {
if (element.isValid) {
action()
override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) =
runBlocking(EDT.ModalityStateElement(ModalityState.any())) {
do {
var modificationStamp: Long? = file.modificationStamp
val elementToActions = runReadAction {
collectAvailableActions(file, rangeMarker)
}
else {
modificationStamp = null
}
}
}
if (modificationStamp == file.modificationStamp) break
}
while (elementToActions.isNotEmpty())
if (formatCode) {
run(EDT) {
runWriteAction {
val codeStyleManager = CodeStyleManager.getInstance(file.project)
if (rangeMarker != null) {
if (rangeMarker.isValid) {
codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset)
run(EDT) {
for ((element, action, _) in elementToActions) {
if (element.isValid) {
action()
}
else {
modificationStamp = null
}
}
}
else {
codeStyleManager.reformat(file)
if (modificationStamp == file.modificationStamp) break
}
while (elementToActions.isNotEmpty())
if (formatCode) {
run(EDT) {
runWriteAction {
val codeStyleManager = CodeStyleManager.getInstance(file.project)
if (rangeMarker != null) {
if (rangeMarker.isValid) {
codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset)
}
}
else {
codeStyleManager.reformat(file)
}
Unit
}
}
Unit
}
}
}
}
private data class ActionData(val element: KtElement, val action: () -> Unit, val priority: Int)