New J2K: fix IDEA exceptions in post-processings
This commit is contained in:
@@ -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 {
|
} else {
|
||||||
action()
|
runAction(action, element)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
modificationStamp = null
|
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,17 +114,28 @@ 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))
|
).fixNullability(AnalysisScope(file, rangeMarker))
|
||||||
}
|
}
|
||||||
withContext(EDT) {
|
withContext(EDT) {
|
||||||
|
runWriteAction {
|
||||||
|
if (rangeMarker != null) {
|
||||||
|
ShortenReferences.DEFAULT.process(file, rangeMarker.startOffset, rangeMarker.endOffset)
|
||||||
|
} else {
|
||||||
|
ShortenReferences.DEFAULT.process(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
NewJ2KPostProcessingRegistrar.mainProcessings.runProcessings(file, rangeMarker)
|
NewJ2KPostProcessingRegistrar.mainProcessings.runProcessings(file, rangeMarker)
|
||||||
|
withContext(EDT) {
|
||||||
|
runWriteAction {
|
||||||
|
file.commitAndUnblockDocument()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
withContext(EDT) {
|
withContext(EDT) {
|
||||||
runWriteAction {
|
runWriteAction {
|
||||||
@@ -129,7 +152,6 @@ class NewJ2kPostProcessor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private data class ActionData(val element: PsiElement, val action: () -> Unit, val priority: Int, val writeActionNeeded: Boolean)
|
private data class ActionData(val element: PsiElement, val action: () -> Unit, val priority: Int, val writeActionNeeded: Boolean)
|
||||||
|
|||||||
Reference in New Issue
Block a user