New J2K: fix IDEA exceptions in post-processings

This commit is contained in:
Ilya Kirillov
2019-04-04 14:00:15 +03:00
parent a5d64bf0b1
commit 99d4accf2c
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.nj2k
import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.RangeMarker import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
@@ -29,7 +28,9 @@ import kotlinx.coroutines.withContext
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
import org.jetbrains.kotlin.idea.conversion.copy.range import org.jetbrains.kotlin.idea.conversion.copy.range
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.util.EDT import org.jetbrains.kotlin.idea.core.util.EDT
import org.jetbrains.kotlin.idea.formatter.commitAndUnblockDocument
import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.idea.util.ImportInsertHelper
import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.idea.util.application.runWriteAction
@@ -65,30 +66,41 @@ class NewJ2kPostProcessor(
PROCESS PROCESS
} }
private fun List<NewJ2kPostProcessing>.runProcessings(file: KtFile, rangeMarker: RangeMarker?): Boolean { private suspend fun List<NewJ2kPostProcessing>.runProcessings(file: KtFile, rangeMarker: RangeMarker?): Boolean {
var modificationStamp: Long? = file.modificationStamp var modificationStamp: Long? = file.modificationStamp
val elementToActions = runReadAction { val elementToActions = runReadAction {
collectAvailableActions(this, file, rangeMarker) collectAvailableActions(this, file, rangeMarker)
} }
withContext(EDT) {
for ((element, action, _, writeActionNeeded) in elementToActions) { for ((element, action, _, writeActionNeeded) in elementToActions) {
if (element.isValid) { if (element.isValid) {
if (writeActionNeeded) { if (writeActionNeeded) {
runWriteAction { runWriteAction {
action() runAction(action, element)
}
} else {
runAction(action, element)
} }
} else { } else {
action() modificationStamp = null
} }
} else {
modificationStamp = null
} }
} }
return modificationStamp != file.modificationStamp && elementToActions.isNotEmpty() return modificationStamp != file.modificationStamp && elementToActions.isNotEmpty()
} }
private fun Processing.runProcessings(file: KtFile, rangeMarker: RangeMarker?) { private inline fun runAction(action: () -> Unit, element: PsiElement) {
try {
action()
} catch (e: IllegalStateException) {
element.containingFile.commitAndUnblockDocument()
action()
}
}
private suspend fun Processing.runProcessings(file: KtFile, rangeMarker: RangeMarker?) {
when (this) { when (this) {
is SingleOneTimeProcessing -> listOf(processing).runProcessings(file, rangeMarker) is SingleOneTimeProcessing -> listOf(processing).runProcessings(file, rangeMarker)
is RepeatableProcessingGroup -> is RepeatableProcessingGroup ->
@@ -102,32 +114,42 @@ class NewJ2kPostProcessor(
override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) { override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) {
CommandProcessor.getInstance().runUndoTransparentAction { runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) {
runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) { withContext(EDT) {
withContext(EDT) { NullabilityAnalysisFacade(
NullabilityAnalysisFacade( getTypeElementNullability = ::nullabilityByUndefinedNullabilityComment,
getTypeElementNullability = ::nullabilityByUndefinedNullabilityComment, prepareTypeElement = ::prepareTypeElementByMakingAllTypesNullableConsideringNullabilityComment,
prepareTypeElement = ::prepareTypeElementByMakingAllTypesNullableConsideringNullabilityComment, debugPrint = false
debugPrint = false ).fixNullability(AnalysisScope(file, rangeMarker))
).fixNullability(AnalysisScope(file)) }
} withContext(EDT) {
withContext(EDT) { runWriteAction {
NewJ2KPostProcessingRegistrar.mainProcessings.runProcessings(file, rangeMarker) if (rangeMarker != null) {
} ShortenReferences.DEFAULT.process(file, rangeMarker.startOffset, rangeMarker.endOffset)
withContext(EDT) { } else {
runWriteAction { ShortenReferences.DEFAULT.process(file)
val codeStyleManager = CodeStyleManager.getInstance(file.project)
if (rangeMarker != null) {
if (rangeMarker.isValid) {
codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset)
}
} else {
codeStyleManager.reformat(file)
}
Unit
} }
} }
} }
NewJ2KPostProcessingRegistrar.mainProcessings.runProcessings(file, rangeMarker)
withContext(EDT) {
runWriteAction {
file.commitAndUnblockDocument()
}
}
withContext(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
}
}
} }
} }